ox-md.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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-expand "org-attach" (file))
  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. (raw-type (org-element-property :type link))
  337. (type (if (string= raw-type "attachment")
  338. ;; Attachments are simplified representations of
  339. ;; file links. When exporting, expose attachments
  340. ;; as if they were file links.
  341. "file"
  342. raw-type))
  343. (raw-path (org-element-property :path link))
  344. (path (cond
  345. ((member type '("http" "https" "ftp" "mailto"))
  346. (concat type ":" raw-path))
  347. ((string= type "file")
  348. (when (string= raw-type "attachment")
  349. (setq raw-path (file-relative-name
  350. (org-with-point-at (org-element-property :begin link)
  351. (org-attach-expand raw-path)))))
  352. (org-export-file-uri (funcall link-org-files-as-md raw-path)))
  353. (t raw-path))))
  354. (cond
  355. ;; Link type is handled by a special function.
  356. ((org-export-custom-protocol-maybe link desc 'md))
  357. ((member type '("custom-id" "id" "fuzzy"))
  358. (let ((destination (if (string= type "fuzzy")
  359. (org-export-resolve-fuzzy-link link info)
  360. (org-export-resolve-id-link link info))))
  361. (pcase (org-element-type destination)
  362. (`plain-text ; External file.
  363. (let ((path (funcall link-org-files-as-md destination)))
  364. (if (not desc) (format "<%s>" path)
  365. (format "[%s](%s)" desc path))))
  366. (`headline
  367. (format
  368. "[%s](#%s)"
  369. ;; Description.
  370. (cond ((org-string-nw-p desc))
  371. ((org-export-numbered-headline-p destination info)
  372. (mapconcat #'number-to-string
  373. (org-export-get-headline-number destination info)
  374. "."))
  375. (t (org-export-data (org-element-property :title destination)
  376. info)))
  377. ;; Reference.
  378. (or (org-element-property :CUSTOM_ID destination)
  379. (org-export-get-reference destination info))))
  380. (_
  381. (let ((description
  382. (or (org-string-nw-p desc)
  383. (let ((number (org-export-get-ordinal destination info)))
  384. (cond
  385. ((not number) nil)
  386. ((atom number) (number-to-string number))
  387. (t (mapconcat #'number-to-string number ".")))))))
  388. (when description
  389. (format "[%s](#%s)"
  390. description
  391. (org-export-get-reference destination info))))))))
  392. ((org-export-inline-image-p link org-html-inline-image-rules)
  393. (let ((path (cond ((not (equal "file" type)) (concat type ":" raw-path))
  394. ((not (file-name-absolute-p raw-path)) raw-path)
  395. (t (expand-file-name raw-path))))
  396. (caption (org-export-data
  397. (org-export-get-caption
  398. (org-export-get-parent-element link)) info)))
  399. (format "![img](%s)"
  400. (if (not (org-string-nw-p caption)) path
  401. (format "%s \"%s\"" path caption)))))
  402. ((string= type "coderef")
  403. (format (org-export-get-coderef-format path desc)
  404. (org-export-resolve-coderef path info)))
  405. ((equal type "radio") desc)
  406. (t (if (not desc) (format "<%s>" path)
  407. (format "[%s](%s)" desc path))))))
  408. ;;;; Node Property
  409. (defun org-md-node-property (node-property _contents _info)
  410. "Transcode a NODE-PROPERTY element into Markdown syntax.
  411. CONTENTS is nil. INFO is a plist holding contextual
  412. information."
  413. (format "%s:%s"
  414. (org-element-property :key node-property)
  415. (let ((value (org-element-property :value node-property)))
  416. (if value (concat " " value) ""))))
  417. ;;;; Paragraph
  418. (defun org-md-paragraph (paragraph contents _info)
  419. "Transcode PARAGRAPH element into Markdown format.
  420. CONTENTS is the paragraph contents. INFO is a plist used as
  421. a communication channel."
  422. (let ((first-object (car (org-element-contents paragraph))))
  423. ;; If paragraph starts with a #, protect it.
  424. (if (and (stringp first-object) (string-prefix-p "#" first-object))
  425. (concat "\\" contents)
  426. contents)))
  427. ;;;; Plain List
  428. (defun org-md-plain-list (_plain-list contents _info)
  429. "Transcode PLAIN-LIST element into Markdown format.
  430. CONTENTS is the plain-list contents. INFO is a plist used as
  431. a communication channel."
  432. contents)
  433. ;;;; Plain Text
  434. (defun org-md-plain-text (text info)
  435. "Transcode a TEXT string into Markdown format.
  436. TEXT is the string to transcode. INFO is a plist holding
  437. contextual information."
  438. (when (plist-get info :with-smart-quotes)
  439. (setq text (org-export-activate-smart-quotes text :html info)))
  440. ;; The below series of replacements in `text' is order sensitive.
  441. ;; Protect `, *, _, and \
  442. (setq text (replace-regexp-in-string "[`*_\\]" "\\\\\\&" text))
  443. ;; Protect ambiguous #. This will protect # at the beginning of
  444. ;; a line, but not at the beginning of a paragraph. See
  445. ;; `org-md-paragraph'.
  446. (setq text (replace-regexp-in-string "\n#" "\n\\\\#" text))
  447. ;; Protect ambiguous !
  448. (setq text (replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil 1))
  449. ;; Handle special strings, if required.
  450. (when (plist-get info :with-special-strings)
  451. (setq text (org-html-convert-special-strings text)))
  452. ;; Handle break preservation, if required.
  453. (when (plist-get info :preserve-breaks)
  454. (setq text (replace-regexp-in-string "[ \t]*\n" " \n" text)))
  455. ;; Return value.
  456. text)
  457. ;;;; Property Drawer
  458. (defun org-md-property-drawer (_property-drawer contents _info)
  459. "Transcode a PROPERTY-DRAWER element into Markdown format.
  460. CONTENTS holds the contents of the drawer. INFO is a plist
  461. holding contextual information."
  462. (and (org-string-nw-p contents)
  463. (replace-regexp-in-string "^" " " contents)))
  464. ;;;; Quote Block
  465. (defun org-md-quote-block (_quote-block contents _info)
  466. "Transcode QUOTE-BLOCK element into Markdown format.
  467. CONTENTS is the quote-block contents. INFO is a plist used as
  468. a communication channel."
  469. (replace-regexp-in-string
  470. "^" "> "
  471. (replace-regexp-in-string "\n\\'" "" contents)))
  472. ;;;; Section
  473. (defun org-md-section (_section contents _info)
  474. "Transcode SECTION element into Markdown format.
  475. CONTENTS is the section contents. INFO is a plist used as
  476. a communication channel."
  477. contents)
  478. ;;;; Template
  479. (defun org-md--build-toc (info &optional n _keyword scope)
  480. "Return a table of contents.
  481. INFO is a plist used as a communication channel.
  482. Optional argument N, when non-nil, is an integer specifying the
  483. depth of the table.
  484. When optional argument SCOPE is non-nil, build a table of
  485. contents according to the specified element."
  486. (concat
  487. (unless scope
  488. (let ((style (plist-get info :md-headline-style))
  489. (title (org-html--translate "Table of Contents" info)))
  490. (org-md--headline-title style 1 title nil)))
  491. (mapconcat
  492. (lambda (headline)
  493. (let* ((indentation
  494. (make-string
  495. (* 4 (1- (org-export-get-relative-level headline info)))
  496. ?\s))
  497. (bullet
  498. (if (not (org-export-numbered-headline-p headline info)) "- "
  499. (let ((prefix
  500. (format "%d." (org-last (org-export-get-headline-number
  501. headline info)))))
  502. (concat prefix (make-string (max 1 (- 4 (length prefix)))
  503. ?\s)))))
  504. (title
  505. (format "[%s](#%s)"
  506. (org-export-data-with-backend
  507. (org-export-get-alt-title headline info)
  508. (org-export-toc-entry-backend 'md)
  509. info)
  510. (or (org-element-property :CUSTOM_ID headline)
  511. (org-export-get-reference headline info))))
  512. (tags (and (plist-get info :with-tags)
  513. (not (eq 'not-in-toc (plist-get info :with-tags)))
  514. (org-make-tag-string
  515. (org-export-get-tags headline info)))))
  516. (concat indentation bullet title tags)))
  517. (org-export-collect-headlines info n scope) "\n")
  518. "\n"))
  519. (defun org-md--footnote-formatted (footnote info)
  520. "Formats a single footnote entry FOOTNOTE.
  521. FOOTNOTE is a cons cell of the form (number . definition).
  522. INFO is a plist with contextual information."
  523. (let* ((fn-num (car footnote))
  524. (fn-text (cdr footnote))
  525. (fn-format (plist-get info :md-footnote-format))
  526. (fn-anchor (format "fn.%d" fn-num))
  527. (fn-href (format " href=\"#fnr.%d\"" fn-num))
  528. (fn-link-to-ref (org-html--anchor fn-anchor fn-num fn-href info)))
  529. (concat (format fn-format fn-link-to-ref) " " fn-text "\n")))
  530. (defun org-md--footnote-section (info)
  531. "Format the footnote section.
  532. INFO is a plist used as a communication channel."
  533. (let* ((fn-alist (org-export-collect-footnote-definitions info))
  534. (fn-alist (cl-loop for (n _type raw) in fn-alist collect
  535. (cons n (org-trim (org-export-data raw info)))))
  536. (headline-style (plist-get info :md-headline-style))
  537. (section-title (org-html--translate "Footnotes" info)))
  538. (when fn-alist
  539. (format (plist-get info :md-footnotes-section)
  540. (org-md--headline-title headline-style 1 section-title)
  541. (mapconcat (lambda (fn) (org-md--footnote-formatted fn info))
  542. fn-alist
  543. "\n")))))
  544. (defun org-md-inner-template (contents info)
  545. "Return body of document after converting it to Markdown syntax.
  546. CONTENTS is the transcoded contents string. INFO is a plist
  547. holding export options."
  548. ;; Make sure CONTENTS is separated from table of contents and
  549. ;; footnotes with at least a blank line.
  550. (concat
  551. ;; Table of contents.
  552. (let ((depth (plist-get info :with-toc)))
  553. (when depth
  554. (concat (org-md--build-toc info (and (wholenump depth) depth)) "\n")))
  555. ;; Document contents.
  556. contents
  557. "\n"
  558. ;; Footnotes section.
  559. (org-md--footnote-section info)))
  560. (defun org-md-template (contents _info)
  561. "Return complete document string after Markdown conversion.
  562. CONTENTS is the transcoded contents string. INFO is a plist used
  563. as a communication channel."
  564. contents)
  565. ;;; Interactive function
  566. ;;;###autoload
  567. (defun org-md-export-as-markdown (&optional async subtreep visible-only)
  568. "Export current buffer to a Markdown buffer.
  569. If narrowing is active in the current buffer, only export its
  570. narrowed part.
  571. If a region is active, export that region.
  572. A non-nil optional argument ASYNC means the process should happen
  573. asynchronously. The resulting buffer should be accessible
  574. through the `org-export-stack' interface.
  575. When optional argument SUBTREEP is non-nil, export the sub-tree
  576. at point, extracting information from the headline properties
  577. first.
  578. When optional argument VISIBLE-ONLY is non-nil, don't export
  579. contents of hidden elements.
  580. Export is done in a buffer named \"*Org MD Export*\", which will
  581. be displayed when `org-export-show-temporary-export-buffer' is
  582. non-nil."
  583. (interactive)
  584. (org-export-to-buffer 'md "*Org MD Export*"
  585. async subtreep visible-only nil nil (lambda () (text-mode))))
  586. ;;;###autoload
  587. (defun org-md-convert-region-to-md ()
  588. "Assume the current region has Org syntax, and convert it to Markdown.
  589. This can be used in any buffer. For example, you can write an
  590. itemized list in Org syntax in a Markdown buffer and use
  591. this command to convert it."
  592. (interactive)
  593. (org-export-replace-region-by 'md))
  594. ;;;###autoload
  595. (defun org-md-export-to-markdown (&optional async subtreep visible-only)
  596. "Export current buffer to a Markdown file.
  597. If narrowing is active in the current buffer, only export its
  598. narrowed part.
  599. If a region is active, export that region.
  600. A non-nil optional argument ASYNC means the process should happen
  601. asynchronously. The resulting file should be accessible through
  602. the `org-export-stack' interface.
  603. When optional argument SUBTREEP is non-nil, export the sub-tree
  604. at point, extracting information from the headline properties
  605. first.
  606. When optional argument VISIBLE-ONLY is non-nil, don't export
  607. contents of hidden elements.
  608. Return output file's name."
  609. (interactive)
  610. (let ((outfile (org-export-output-file-name ".md" subtreep)))
  611. (org-export-to-file 'md outfile async subtreep visible-only)))
  612. ;;;###autoload
  613. (defun org-md-publish-to-md (plist filename pub-dir)
  614. "Publish an org file to Markdown.
  615. FILENAME is the filename of the Org file to be published. PLIST
  616. is the property list for the given project. PUB-DIR is the
  617. publishing directory.
  618. Return output file name."
  619. (org-publish-org-to 'md filename ".md" plist pub-dir))
  620. (provide 'ox-md)
  621. ;; Local variables:
  622. ;; generated-autoload-file: "org-loaddefs.el"
  623. ;; End:
  624. ;;; ox-md.el ends here