ox-md.el 27 KB

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