ox-md.el 25 KB

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