ox-md.el 27 KB

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