ox-md.el 26 KB

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