ox-md.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. ;;; ox-md.el --- Markdown Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2020 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: org, wp, markdown
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This library implements a Markdown back-end (vanilla flavor) for
  18. ;; Org exporter, based on `html' back-end. See Org manual for more
  19. ;; information.
  20. ;;; Code:
  21. (require 'cl-lib)
  22. (require 'ox-html)
  23. (require 'ox-publish)
  24. ;;; Function Declarations
  25. ;;; User-Configurable Variables
  26. (defgroup org-export-md nil
  27. "Options specific to Markdown export back-end."
  28. :tag "Org Markdown"
  29. :group 'org-export
  30. :version "24.4"
  31. :package-version '(Org . "8.0"))
  32. (defcustom org-md-headline-style 'atx
  33. "Style used to format headlines.
  34. This variable can be set to either `atx' or `setext'."
  35. :group 'org-export-md
  36. :type '(choice
  37. (const :tag "Use \"atx\" style" atx)
  38. (const :tag "Use \"Setext\" style" setext)))
  39. ;;;; Footnotes
  40. (defcustom org-md-footnotes-section "%s%s"
  41. "Format string for the footnotes section.
  42. The first %s placeholder will be replaced with the localized Footnotes section
  43. heading, the second with the contents of the Footnotes section."
  44. :group 'org-export-md
  45. :type 'string
  46. :version "26.1"
  47. :package-version '(Org . "9.0"))
  48. (defcustom org-md-footnote-format "<sup>%s</sup>"
  49. "Format string for the footnote reference.
  50. The %s will be replaced by the footnote reference itself."
  51. :group 'org-export-md
  52. :type 'string
  53. :version "26.1"
  54. :package-version '(Org . "9.0"))
  55. ;;; Define Back-End
  56. (org-export-define-derived-backend 'md 'html
  57. :filters-alist '((:filter-parse-tree . org-md-separate-elements))
  58. :menu-entry
  59. '(?m "Export to Markdown"
  60. ((?M "To temporary buffer"
  61. (lambda (a s v b) (org-md-export-as-markdown a s v)))
  62. (?m "To file" (lambda (a s v b) (org-md-export-to-markdown a s v)))
  63. (?o "To file and open"
  64. (lambda (a s v b)
  65. (if a (org-md-export-to-markdown t s v)
  66. (org-open-file (org-md-export-to-markdown nil s v)))))))
  67. :translate-alist '((bold . org-md-bold)
  68. (code . org-md-verbatim)
  69. (example-block . org-md-example-block)
  70. (export-block . org-md-export-block)
  71. (fixed-width . org-md-example-block)
  72. (headline . org-md-headline)
  73. (horizontal-rule . org-md-horizontal-rule)
  74. (inline-src-block . org-md-verbatim)
  75. (inner-template . org-md-inner-template)
  76. (italic . org-md-italic)
  77. (item . org-md-item)
  78. (keyword . org-md-keyword)
  79. (line-break . org-md-line-break)
  80. (link . org-md-link)
  81. (node-property . org-md-node-property)
  82. (paragraph . org-md-paragraph)
  83. (plain-list . org-md-plain-list)
  84. (plain-text . org-md-plain-text)
  85. (property-drawer . org-md-property-drawer)
  86. (quote-block . org-md-quote-block)
  87. (section . org-md-section)
  88. (src-block . org-md-example-block)
  89. (template . org-md-template)
  90. (verbatim . org-md-verbatim))
  91. :options-alist
  92. '((:md-footnote-format nil nil org-md-footnote-format)
  93. (:md-footnotes-section nil nil org-md-footnotes-section)
  94. (:md-headline-style nil nil org-md-headline-style)))
  95. ;;; Filters
  96. (defun org-md-separate-elements (tree _backend info)
  97. "Fix blank lines between elements.
  98. TREE is the parse tree being exported. BACKEND is the export
  99. back-end used. INFO is a plist used as a communication channel.
  100. Enforce a blank line between elements. There are two exceptions
  101. to this rule:
  102. 1. Preserve blank lines between sibling items in a plain list,
  103. 2. In an item, remove any blank line before the very first
  104. paragraph and the next sub-list when the latter ends the
  105. current item.
  106. Assume BACKEND is `md'."
  107. (org-element-map tree (remq 'item org-element-all-elements)
  108. (lambda (e)
  109. (org-element-put-property
  110. e :post-blank
  111. (if (and (eq (org-element-type e) 'paragraph)
  112. (eq (org-element-type (org-element-property :parent e)) 'item)
  113. (org-export-first-sibling-p e info)
  114. (let ((next (org-export-get-next-element e info)))
  115. (and (eq (org-element-type next) 'plain-list)
  116. (not (org-export-get-next-element next info)))))
  117. 0
  118. 1))))
  119. ;; Return updated tree.
  120. tree)
  121. ;;; Transcode Functions
  122. ;;;; Bold
  123. (defun org-md-bold (_bold contents _info)
  124. "Transcode BOLD object into Markdown format.
  125. CONTENTS is the text within bold markup. INFO is a plist used as
  126. a communication channel."
  127. (format "**%s**" contents))
  128. ;;;; Code and Verbatim
  129. (defun org-md-verbatim (verbatim _contents _info)
  130. "Transcode VERBATIM object into Markdown format.
  131. CONTENTS is nil. INFO is a plist used as a communication
  132. channel."
  133. (let ((value (org-element-property :value verbatim)))
  134. (format (cond ((not (string-match "`" value)) "`%s`")
  135. ((or (string-prefix-p "`" value)
  136. (string-suffix-p "`" value))
  137. "`` %s ``")
  138. (t "``%s``"))
  139. value)))
  140. ;;;; Example Block, Src Block and Export Block
  141. (defun org-md-example-block (example-block _contents info)
  142. "Transcode EXAMPLE-BLOCK element into Markdown format.
  143. CONTENTS is nil. INFO is a plist used as a communication
  144. channel."
  145. (replace-regexp-in-string
  146. "^" " "
  147. (org-remove-indentation
  148. (org-export-format-code-default example-block info))))
  149. (defun org-md-export-block (export-block contents info)
  150. "Transcode a EXPORT-BLOCK element from Org to Markdown.
  151. CONTENTS is nil. INFO is a plist holding contextual information."
  152. (if (member (org-element-property :type export-block) '("MARKDOWN" "MD"))
  153. (org-remove-indentation (org-element-property :value export-block))
  154. ;; Also include HTML export blocks.
  155. (org-export-with-backend 'html export-block contents info)))
  156. ;;;; Headline
  157. (defun org-md-headline (headline contents info)
  158. "Transcode HEADLINE element into Markdown format.
  159. CONTENTS is the headline contents. INFO is a plist used as
  160. a communication channel."
  161. (unless (org-element-property :footnote-section-p headline)
  162. (let* ((level (org-export-get-relative-level headline info))
  163. (title (org-export-data (org-element-property :title headline) info))
  164. (todo (and (plist-get info :with-todo-keywords)
  165. (let ((todo (org-element-property :todo-keyword
  166. headline)))
  167. (and todo (concat (org-export-data todo info) " ")))))
  168. (tags (and (plist-get info :with-tags)
  169. (let ((tag-list (org-export-get-tags headline info)))
  170. (and tag-list
  171. (concat " " (org-make-tag-string tag-list))))))
  172. (priority
  173. (and (plist-get info :with-priority)
  174. (let ((char (org-element-property :priority headline)))
  175. (and char (format "[#%c] " char)))))
  176. ;; Headline text without tags.
  177. (heading (concat todo priority title))
  178. (style (plist-get info :md-headline-style)))
  179. (cond
  180. ;; Cannot create a headline. Fall-back to a list.
  181. ((or (org-export-low-level-p headline info)
  182. (not (memq style '(atx setext)))
  183. (and (eq style 'atx) (> level 6))
  184. (and (eq style 'setext) (> level 2)))
  185. (let ((bullet
  186. (if (not (org-export-numbered-headline-p headline info)) "-"
  187. (concat (number-to-string
  188. (car (last (org-export-get-headline-number
  189. headline info))))
  190. "."))))
  191. (concat bullet (make-string (- 4 (length bullet)) ?\s) heading tags "\n\n"
  192. (and contents (replace-regexp-in-string "^" " " contents)))))
  193. (t
  194. (let ((anchor
  195. (and (org-md--headline-referred-p headline info)
  196. (format "<a id=\"%s\"></a>"
  197. (or (org-element-property :CUSTOM_ID headline)
  198. (org-export-get-reference headline info))))))
  199. (concat (org-md--headline-title style level heading anchor tags)
  200. contents)))))))
  201. (defun org-md--headline-referred-p (headline info)
  202. "Non-nil when HEADLINE is being referred to.
  203. INFO is a plist used as a communication channel. Links and table
  204. of contents can refer to headlines."
  205. (unless (org-element-property :footnote-section-p headline)
  206. (or
  207. ;; Global table of contents includes HEADLINE.
  208. (and (plist-get info :with-toc)
  209. (memq headline
  210. (org-export-collect-headlines info (plist-get info :with-toc))))
  211. ;; A local table of contents includes HEADLINE.
  212. (cl-some
  213. (lambda (h)
  214. (let ((section (car (org-element-contents h))))
  215. (and
  216. (eq 'section (org-element-type section))
  217. (org-element-map section 'keyword
  218. (lambda (keyword)
  219. (when (equal "TOC" (org-element-property :key keyword))
  220. (let ((case-fold-search t)
  221. (value (org-element-property :value keyword)))
  222. (and (string-match-p "\\<headlines\\>" value)
  223. (let ((n (and
  224. (string-match "\\<[0-9]+\\>" value)
  225. (string-to-number (match-string 0 value))))
  226. (local? (string-match-p "\\<local\\>" value)))
  227. (memq headline
  228. (org-export-collect-headlines
  229. info n (and local? keyword))))))))
  230. info t))))
  231. (org-element-lineage headline))
  232. ;; A link refers internally to HEADLINE.
  233. (org-element-map (plist-get info :parse-tree) 'link
  234. (lambda (link)
  235. (eq headline
  236. (pcase (org-element-property :type link)
  237. ((or "custom-id" "id") (org-export-resolve-id-link link info))
  238. ("fuzzy" (org-export-resolve-fuzzy-link link info))
  239. (_ nil))))
  240. info t))))
  241. (defun org-md--headline-title (style level title &optional anchor tags)
  242. "Generate a headline title in the preferred Markdown headline style.
  243. STYLE is the preferred style (`atx' or `setext'). LEVEL is the
  244. header level. TITLE is the headline title. ANCHOR is the HTML
  245. anchor tag for the section as a string. TAGS are the tags set on
  246. the section."
  247. (let ((anchor-lines (and anchor (concat anchor "\n\n"))))
  248. ;; Use "Setext" style
  249. (if (and (eq style 'setext) (< level 3))
  250. (let* ((underline-char (if (= level 1) ?= ?-))
  251. (underline (concat (make-string (length title) underline-char)
  252. "\n")))
  253. (concat "\n" anchor-lines title tags "\n" underline "\n"))
  254. ;; Use "Atx" style
  255. (let ((level-mark (make-string level ?#)))
  256. (concat "\n" anchor-lines level-mark " " title tags "\n\n")))))
  257. ;;;; Horizontal Rule
  258. (defun org-md-horizontal-rule (_horizontal-rule _contents _info)
  259. "Transcode HORIZONTAL-RULE element into Markdown format.
  260. CONTENTS is the horizontal rule contents. INFO is a plist used
  261. as a communication channel."
  262. "---")
  263. ;;;; Italic
  264. (defun org-md-italic (_italic contents _info)
  265. "Transcode ITALIC object into Markdown format.
  266. CONTENTS is the text within italic markup. INFO is a plist used
  267. as a communication channel."
  268. (format "*%s*" contents))
  269. ;;;; Item
  270. (defun org-md-item (item contents info)
  271. "Transcode ITEM element into Markdown format.
  272. CONTENTS is the item contents. INFO is a plist used as
  273. a communication channel."
  274. (let* ((type (org-element-property :type (org-export-get-parent item)))
  275. (struct (org-element-property :structure item))
  276. (bullet (if (not (eq type 'ordered)) "-"
  277. (concat (number-to-string
  278. (car (last (org-list-get-item-number
  279. (org-element-property :begin item)
  280. struct
  281. (org-list-prevs-alist struct)
  282. (org-list-parents-alist struct)))))
  283. "."))))
  284. (concat bullet
  285. (make-string (- 4 (length bullet)) ? )
  286. (pcase (org-element-property :checkbox item)
  287. (`on "[X] ")
  288. (`trans "[-] ")
  289. (`off "[ ] "))
  290. (let ((tag (org-element-property :tag item)))
  291. (and tag (format "**%s:** "(org-export-data tag info))))
  292. (and contents
  293. (org-trim (replace-regexp-in-string "^" " " contents))))))
  294. ;;;; Keyword
  295. (defun org-md-keyword (keyword contents info)
  296. "Transcode a KEYWORD element into Markdown format.
  297. CONTENTS is nil. INFO is a plist used as a communication
  298. channel."
  299. (pcase (org-element-property :key keyword)
  300. ((or "MARKDOWN" "MD") (org-element-property :value keyword))
  301. ("TOC"
  302. (let ((case-fold-search t)
  303. (value (org-element-property :value keyword)))
  304. (cond
  305. ((string-match-p "\\<headlines\\>" value)
  306. (let ((depth (and (string-match "\\<[0-9]+\\>" value)
  307. (string-to-number (match-string 0 value))))
  308. (scope
  309. (cond
  310. ((string-match ":target +\\(\".+?\"\\|\\S-+\\)" value) ;link
  311. (org-export-resolve-link
  312. (org-strip-quotes (match-string 1 value)) info))
  313. ((string-match-p "\\<local\\>" value) keyword)))) ;local
  314. (org-remove-indentation
  315. (org-md--build-toc info depth keyword scope)))))))
  316. (_ (org-export-with-backend 'html keyword contents info))))
  317. ;;;; Line Break
  318. (defun org-md-line-break (_line-break _contents _info)
  319. "Transcode LINE-BREAK object into Markdown format.
  320. CONTENTS is nil. INFO is a plist used as a communication
  321. channel."
  322. " \n")
  323. ;;;; Link
  324. (defun org-md-link (link desc info)
  325. "Transcode LINE-BREAK object into Markdown format.
  326. DESC is the description part of the link, or the empty string.
  327. INFO is a plist holding contextual information. See
  328. `org-export-data'."
  329. (let* ((link-org-files-as-md
  330. (lambda (raw-path)
  331. ;; Treat links to `file.org' as links to `file.md'.
  332. (if (string= ".org" (downcase (file-name-extension raw-path ".")))
  333. (concat (file-name-sans-extension raw-path) ".md")
  334. raw-path)))
  335. (type (org-element-property :type link))
  336. (raw-path (org-element-property :path link))
  337. (path (cond
  338. ((member type '("http" "https" "ftp" "mailto"))
  339. (concat type ":" raw-path))
  340. ((member type '("file" "attachment"))
  341. (when (string= type "attachment")
  342. (setq raw-path (org-element-property :attachment-path link)))
  343. (org-export-file-uri (funcall link-org-files-as-md raw-path)))
  344. (t raw-path))))
  345. (cond
  346. ;; Link type is handled by a special function.
  347. ((org-export-custom-protocol-maybe link desc 'md))
  348. ((member type '("custom-id" "id" "fuzzy"))
  349. (let ((destination (if (string= type "fuzzy")
  350. (org-export-resolve-fuzzy-link link info)
  351. (org-export-resolve-id-link link info))))
  352. (pcase (org-element-type destination)
  353. (`plain-text ; External file.
  354. (let ((path (funcall link-org-files-as-md destination)))
  355. (if (not desc) (format "<%s>" path)
  356. (format "[%s](%s)" desc path))))
  357. (`headline
  358. (format
  359. "[%s](#%s)"
  360. ;; Description.
  361. (cond ((org-string-nw-p desc))
  362. ((org-export-numbered-headline-p destination info)
  363. (mapconcat #'number-to-string
  364. (org-export-get-headline-number destination info)
  365. "."))
  366. (t (org-export-data (org-element-property :title destination)
  367. info)))
  368. ;; Reference.
  369. (or (org-element-property :CUSTOM_ID destination)
  370. (org-export-get-reference destination info))))
  371. (_
  372. (let ((description
  373. (or (org-string-nw-p desc)
  374. (let ((number (org-export-get-ordinal destination info)))
  375. (cond
  376. ((not number) nil)
  377. ((atom number) (number-to-string number))
  378. (t (mapconcat #'number-to-string number ".")))))))
  379. (when description
  380. (format "[%s](#%s)"
  381. description
  382. (org-export-get-reference destination info))))))))
  383. ((org-export-inline-image-p link org-html-inline-image-rules)
  384. (let ((path (cond ((not (member type '("file" "attachment")))
  385. (concat type ":" raw-path))
  386. ((not (file-name-absolute-p raw-path)) raw-path)
  387. (t (expand-file-name raw-path))))
  388. (caption (org-export-data
  389. (org-export-get-caption
  390. (org-export-get-parent-element link)) info)))
  391. (format "![img](%s)"
  392. (if (not (org-string-nw-p caption)) path
  393. (format "%s \"%s\"" path caption)))))
  394. ((string= type "coderef")
  395. (format (org-export-get-coderef-format path desc)
  396. (org-export-resolve-coderef path info)))
  397. ((equal type "radio") desc)
  398. (t (if (not desc) (format "<%s>" path)
  399. (format "[%s](%s)" desc path))))))
  400. ;;;; Node Property
  401. (defun org-md-node-property (node-property _contents _info)
  402. "Transcode a NODE-PROPERTY element into Markdown syntax.
  403. CONTENTS is nil. INFO is a plist holding contextual
  404. information."
  405. (format "%s:%s"
  406. (org-element-property :key node-property)
  407. (let ((value (org-element-property :value node-property)))
  408. (if value (concat " " value) ""))))
  409. ;;;; Paragraph
  410. (defun org-md-paragraph (paragraph contents _info)
  411. "Transcode PARAGRAPH element into Markdown format.
  412. CONTENTS is the paragraph contents. INFO is a plist used as
  413. a communication channel."
  414. (let ((first-object (car (org-element-contents paragraph))))
  415. ;; If paragraph starts with a #, protect it.
  416. (if (and (stringp first-object) (string-prefix-p "#" first-object))
  417. (concat "\\" contents)
  418. contents)))
  419. ;;;; Plain List
  420. (defun org-md-plain-list (_plain-list contents _info)
  421. "Transcode PLAIN-LIST element into Markdown format.
  422. CONTENTS is the plain-list contents. INFO is a plist used as
  423. a communication channel."
  424. contents)
  425. ;;;; Plain Text
  426. (defun org-md-plain-text (text info)
  427. "Transcode a TEXT string into Markdown format.
  428. TEXT is the string to transcode. INFO is a plist holding
  429. contextual information."
  430. (when (plist-get info :with-smart-quotes)
  431. (setq text (org-export-activate-smart-quotes text :html info)))
  432. ;; The below series of replacements in `text' is order sensitive.
  433. ;; Protect `, *, _, and \
  434. (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
  435. ;; Protect ambiguous #. This will protect # at the beginning of
  436. ;; a line, but not at the beginning of a paragraph. See
  437. ;; `org-md-paragraph'.
  438. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  439. ;; Protect ambiguous !
  440. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  441. ;; Handle special strings, if required.
  442. (when (plist-get info :with-special-strings)
  443. (setq text (org-html-convert-special-strings text)))
  444. ;; Handle break preservation, if required.
  445. (when (plist-get info :preserve-breaks)
  446. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  447. ;; Return value.
  448. text)
  449. ;;;; Property Drawer
  450. (defun org-md-property-drawer (_property-drawer contents _info)
  451. "Transcode a PROPERTY-DRAWER element into Markdown format.
  452. CONTENTS holds the contents of the drawer. INFO is a plist
  453. holding contextual information."
  454. (and (org-string-nw-p contents)
  455. (replace-regexp-in-string "^" " " contents)))
  456. ;;;; Quote Block
  457. (defun org-md-quote-block (_quote-block contents _info)
  458. "Transcode QUOTE-BLOCK element into Markdown format.
  459. CONTENTS is the quote-block contents. INFO is a plist used as
  460. a communication channel."
  461. (replace-regexp-in-string
  462. "^" "> "
  463. (replace-regexp-in-string "\n\\'" "" contents)))
  464. ;;;; Section
  465. (defun org-md-section (_section contents _info)
  466. "Transcode SECTION element into Markdown format.
  467. CONTENTS is the section contents. INFO is a plist used as
  468. a communication channel."
  469. contents)
  470. ;;;; Template
  471. (defun org-md--build-toc (info &optional n _keyword scope)
  472. "Return a table of contents.
  473. INFO is a plist used as a communication channel.
  474. Optional argument N, when non-nil, is an integer specifying the
  475. depth of the table.
  476. When optional argument SCOPE is non-nil, build a table of
  477. contents according to the specified element."
  478. (concat
  479. (unless scope
  480. (let ((style (plist-get info :md-headline-style))
  481. (title (org-html--translate "Table of Contents" info)))
  482. (org-md--headline-title style 1 title nil)))
  483. (mapconcat
  484. (lambda (headline)
  485. (let* ((indentation
  486. (make-string
  487. (* 4 (1- (org-export-get-relative-level headline info)))
  488. ?\s))
  489. (bullet
  490. (if (not (org-export-numbered-headline-p headline info)) "- "
  491. (let ((prefix
  492. (format "%d." (org-last (org-export-get-headline-number
  493. headline info)))))
  494. (concat prefix (make-string (max 1 (- 4 (length prefix)))
  495. ?\s)))))
  496. (title
  497. (format "[%s](#%s)"
  498. (org-export-data-with-backend
  499. (org-export-get-alt-title headline info)
  500. (org-export-toc-entry-backend 'md)
  501. info)
  502. (or (org-element-property :CUSTOM_ID headline)
  503. (org-export-get-reference headline info))))
  504. (tags (and (plist-get info :with-tags)
  505. (not (eq 'not-in-toc (plist-get info :with-tags)))
  506. (org-make-tag-string
  507. (org-export-get-tags headline info)))))
  508. (concat indentation bullet title tags)))
  509. (org-export-collect-headlines info n scope) "\n")
  510. "\n"))
  511. (defun org-md--footnote-formatted (footnote info)
  512. "Formats a single footnote entry FOOTNOTE.
  513. FOOTNOTE is a cons cell of the form (number . definition).
  514. INFO is a plist with contextual information."
  515. (let* ((fn-num (car footnote))
  516. (fn-text (cdr footnote))
  517. (fn-format (plist-get info :md-footnote-format))
  518. (fn-anchor (format "fn.%d" fn-num))
  519. (fn-href (format " href=\"#fnr.%d\"" fn-num))
  520. (fn-link-to-ref (org-html--anchor fn-anchor fn-num fn-href info)))
  521. (concat (format fn-format fn-link-to-ref) " " fn-text "\n")))
  522. (defun org-md--footnote-section (info)
  523. "Format the footnote section.
  524. INFO is a plist used as a communication channel."
  525. (let* ((fn-alist (org-export-collect-footnote-definitions info))
  526. (fn-alist (cl-loop for (n _type raw) in fn-alist collect
  527. (cons n (org-trim (org-export-data raw info)))))
  528. (headline-style (plist-get info :md-headline-style))
  529. (section-title (org-html--translate "Footnotes" info)))
  530. (when fn-alist
  531. (format (plist-get info :md-footnotes-section)
  532. (org-md--headline-title headline-style 1 section-title)
  533. (mapconcat (lambda (fn) (org-md--footnote-formatted fn info))
  534. fn-alist
  535. "\n")))))
  536. (defun org-md-inner-template (contents info)
  537. "Return body of document after converting it to Markdown syntax.
  538. CONTENTS is the transcoded contents string. INFO is a plist
  539. holding export options."
  540. ;; Make sure CONTENTS is separated from table of contents and
  541. ;; footnotes with at least a blank line.
  542. (concat
  543. ;; Table of contents.
  544. (let ((depth (plist-get info :with-toc)))
  545. (when depth
  546. (concat (org-md--build-toc info (and (wholenump depth) depth)) "\n")))
  547. ;; Document contents.
  548. contents
  549. "\n"
  550. ;; Footnotes section.
  551. (org-md--footnote-section info)))
  552. (defun org-md-template (contents _info)
  553. "Return complete document string after Markdown conversion.
  554. CONTENTS is the transcoded contents string. INFO is a plist used
  555. as a communication channel."
  556. contents)
  557. ;;; Interactive function
  558. ;;;###autoload
  559. (defun org-md-export-as-markdown (&optional async subtreep visible-only)
  560. "Export current buffer to a Markdown buffer.
  561. If narrowing is active in the current buffer, only export its
  562. narrowed part.
  563. If a region is active, export that region.
  564. A non-nil optional argument ASYNC means the process should happen
  565. asynchronously. The resulting buffer should be accessible
  566. through the `org-export-stack' interface.
  567. When optional argument SUBTREEP is non-nil, export the sub-tree
  568. at point, extracting information from the headline properties
  569. first.
  570. When optional argument VISIBLE-ONLY is non-nil, don't export
  571. contents of hidden elements.
  572. Export is done in a buffer named \"*Org MD Export*\", which will
  573. be displayed when `org-export-show-temporary-export-buffer' is
  574. non-nil."
  575. (interactive)
  576. (org-export-to-buffer 'md "*Org MD Export*"
  577. async subtreep visible-only nil nil (lambda () (text-mode))))
  578. ;;;###autoload
  579. (defun org-md-convert-region-to-md ()
  580. "Assume the current region has Org syntax, and convert it to Markdown.
  581. This can be used in any buffer. For example, you can write an
  582. itemized list in Org syntax in a Markdown buffer and use
  583. this command to convert it."
  584. (interactive)
  585. (org-export-replace-region-by 'md))
  586. ;;;###autoload
  587. (defun org-md-export-to-markdown (&optional async subtreep visible-only)
  588. "Export current buffer to a Markdown file.
  589. If narrowing is active in the current buffer, only export its
  590. narrowed part.
  591. If a region is active, export that region.
  592. A non-nil optional argument ASYNC means the process should happen
  593. asynchronously. The resulting file should be accessible through
  594. the `org-export-stack' interface.
  595. When optional argument SUBTREEP is non-nil, export the sub-tree
  596. at point, extracting information from the headline properties
  597. first.
  598. When optional argument VISIBLE-ONLY is non-nil, don't export
  599. contents of hidden elements.
  600. Return output file's name."
  601. (interactive)
  602. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  603. (org-export-to-file 'md outfile async subtreep visible-only)))
  604. ;;;###autoload
  605. (defun org-md-publish-to-md (plist filename pub-dir)
  606. "Publish an org file to Markdown.
  607. FILENAME is the filename of the Org file to be published. PLIST
  608. is the property list for the given project. PUB-DIR is the
  609. publishing directory.
  610. Return output file name."
  611. (org-publish-org-to 'md filename ".md" plist pub-dir))
  612. (provide 'ox-md)
  613. ;; Local variables:
  614. ;; generated-autoload-file: "org-loaddefs.el"
  615. ;; End:
  616. ;;; ox-md.el ends here