ox-md.el 28 KB

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