ox-md.el 28 KB

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