ox-md.el 25 KB

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