ox-md.el 26 KB

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