1 ;;; ox-md.el --- Markdown Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp, markdown
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements a Markdown back-end (vanilla flavor) for
26 ;; Org exporter, based on `html' back-end. See Org manual for more
36 ;;; User-Configurable Variables
38 (defgroup org-export-md nil
39 "Options specific to Markdown export back-end."
43 :package-version
'(Org .
"8.0"))
45 (defcustom org-md-headline-style
'atx
46 "Style used to format headlines.
47 This variable can be set to either `atx' or `setext'."
50 (const :tag
"Use \"atx\" style" atx
)
51 (const :tag
"Use \"Setext\" style" setext
)))
56 (defcustom org-md-footnotes-section
"%s%s"
57 "Format string for the footnotes section.
58 The first %s placeholder will be replaced with the localized Footnotes section
59 heading, the second with the contents of the Footnotes section."
63 :package-version
'(Org .
"9.0"))
65 (defcustom org-md-footnote-format
"<sup>%s</sup>"
66 "Format string for the footnote reference.
67 The %s will be replaced by the footnote reference itself."
71 :package-version
'(Org .
"9.0"))
76 (org-export-define-derived-backend 'md
'html
77 :filters-alist
'((:filter-parse-tree . org-md-separate-elements
))
79 '(?m
"Export to Markdown"
80 ((?M
"To temporary buffer"
81 (lambda (a s v b
) (org-md-export-as-markdown a s v
)))
82 (?m
"To file" (lambda (a s v b
) (org-md-export-to-markdown a s v
)))
83 (?o
"To file and open"
85 (if a
(org-md-export-to-markdown t s v
)
86 (org-open-file (org-md-export-to-markdown nil s v
)))))))
87 :translate-alist
'((bold . org-md-bold
)
88 (code . org-md-verbatim
)
89 (example-block . org-md-example-block
)
90 (export-block . org-md-export-block
)
91 (fixed-width . org-md-example-block
)
92 (headline . org-md-headline
)
93 (horizontal-rule . org-md-horizontal-rule
)
94 (inline-src-block . org-md-verbatim
)
95 (inner-template . org-md-inner-template
)
96 (italic . org-md-italic
)
98 (keyword . org-md-keyword
)
99 (line-break . org-md-line-break
)
101 (node-property . org-md-node-property
)
102 (paragraph . org-md-paragraph
)
103 (plain-list . org-md-plain-list
)
104 (plain-text . org-md-plain-text
)
105 (property-drawer . org-md-property-drawer
)
106 (quote-block . org-md-quote-block
)
107 (section . org-md-section
)
108 (src-block . org-md-example-block
)
109 (template . org-md-template
)
110 (verbatim . org-md-verbatim
))
112 '((:md-footnote-format nil nil org-md-footnote-format
)
113 (:md-footnotes-section nil nil org-md-footnotes-section
)
114 (:md-headline-style nil nil org-md-headline-style
)))
119 (defun org-md-separate-elements (tree _backend info
)
120 "Fix blank lines between elements.
122 TREE is the parse tree being exported. BACKEND is the export
123 back-end used. INFO is a plist used as a communication channel.
125 Enforce a blank line between elements. There are two exceptions
128 1. Preserve blank lines between sibling items in a plain list,
130 2. In an item, remove any blank line before the very first
131 paragraph and the next sub-list when the latter ends the
134 Assume BACKEND is `md'."
135 (org-element-map tree
(remq 'item org-element-all-elements
)
137 (org-element-put-property
139 (if (and (eq (org-element-type e
) 'paragraph
)
140 (eq (org-element-type (org-element-property :parent e
)) 'item
)
141 (org-export-first-sibling-p e info
)
142 (let ((next (org-export-get-next-element e info
)))
143 (and (eq (org-element-type next
) 'plain-list
)
144 (not (org-export-get-next-element next info
)))))
147 ;; Return updated tree.
152 ;;; Transcode Functions
156 (defun org-md-bold (_bold contents _info
)
157 "Transcode BOLD object into Markdown format.
158 CONTENTS is the text within bold markup. INFO is a plist used as
159 a communication channel."
160 (format "**%s**" contents
))
163 ;;;; Code and Verbatim
165 (defun org-md-verbatim (verbatim _contents _info
)
166 "Transcode VERBATIM object into Markdown format.
167 CONTENTS is nil. INFO is a plist used as a communication
169 (let ((value (org-element-property :value verbatim
)))
170 (format (cond ((not (string-match "`" value
)) "`%s`")
171 ((or (string-prefix-p "`" value
)
172 (string-suffix-p "`" value
))
178 ;;;; Example Block, Src Block and export Block
180 (defun org-md-example-block (example-block _contents info
)
181 "Transcode EXAMPLE-BLOCK element into Markdown format.
182 CONTENTS is nil. INFO is a plist used as a communication
184 (replace-regexp-in-string
186 (org-remove-indentation
187 (org-export-format-code-default example-block info
))))
189 (defun org-md-export-block (export-block contents info
)
190 "Transcode a EXPORT-BLOCK element from Org to Markdown.
191 CONTENTS is nil. INFO is a plist holding contextual information."
192 (if (member (org-element-property :type export-block
) '("MARKDOWN" "MD"))
193 (org-remove-indentation (org-element-property :value export-block
))
194 ;; Also include HTML export blocks.
195 (org-export-with-backend 'html export-block contents info
)))
200 (defun org-md-headline (headline contents info
)
201 "Transcode HEADLINE element into Markdown format.
202 CONTENTS is the headline contents. INFO is a plist used as
203 a communication channel."
204 (unless (org-element-property :footnote-section-p headline
)
205 (let* ((level (org-export-get-relative-level headline info
))
206 (title (org-export-data (org-element-property :title headline
) info
))
207 (todo (and (plist-get info
:with-todo-keywords
)
208 (let ((todo (org-element-property :todo-keyword
210 (and todo
(concat (org-export-data todo info
) " ")))))
211 (tags (and (plist-get info
:with-tags
)
212 (let ((tag-list (org-export-get-tags headline info
)))
215 (mapconcat 'identity tag-list
":"))))))
217 (and (plist-get info
:with-priority
)
218 (let ((char (org-element-property :priority headline
)))
219 (and char
(format "[#%c] " char
)))))
220 ;; Headline text without tags.
221 (heading (concat todo priority title
))
222 (style (plist-get info
:md-headline-style
)))
224 ;; Cannot create a headline. Fall-back to a list.
225 ((or (org-export-low-level-p headline info
)
226 (not (memq style
'(atx setext
)))
227 (and (eq style
'atx
) (> level
6))
228 (and (eq style
'setext
) (> level
2)))
230 (if (not (org-export-numbered-headline-p headline info
)) "-"
231 (concat (number-to-string
232 (car (last (org-export-get-headline-number
235 (concat bullet
(make-string (- 4 (length bullet
)) ?\s
) heading tags
"\n\n"
236 (and contents
(replace-regexp-in-string "^" " " contents
)))))
239 (and (org-md--headline-referred-p headline info
)
240 (format "<a id=\"%s\"></a>"
241 (or (org-element-property :CUSTOM_ID headline
)
242 (org-export-get-reference headline info
))))))
243 (concat (org-md--headline-title style level title anchor tags
)
247 (defun org-md--headline-referred-p (headline info
)
248 "Non-nil when HEADLINE is being referred to.
249 INFO is a plist used as a communication channel. Links and table
250 of contents can refer to headlines."
251 (or (plist-get info
:with-toc
)
252 (org-element-map (plist-get info
:parse-tree
) 'link
255 (pcase (org-element-property :type link
)
256 ((or "custom-id" "id") (org-export-resolve-id-link link info
))
257 ("fuzzy" (org-export-resolve-fuzzy-link link info
))
261 (defun org-md--headline-title (style level title
&optional anchor tags
)
262 "Generate a headline title in the preferred Markdown headline style.
263 STYLE is the preferred style (`atx' or `setext'). LEVEL is the
264 header level. TITLE is the headline title. ANCHOR is the HTML
265 anchor tag for the section as a string. TAGS are the tags set on
267 (let ((anchor-lines (and anchor
(concat anchor
"\n\n"))))
268 ;; Use "Setext" style
269 (if (and (eq style
'setext
) (< level
3))
270 (let* ((underline-char (if (= level
1) ?
= ?-
))
271 (underline (concat (make-string (length title
) underline-char
)
273 (concat "\n" anchor-lines title tags
"\n" underline
"\n"))
275 (let ((level-mark (make-string level ?
#)))
276 (concat "\n" anchor-lines level-mark
" " title tags
"\n\n")))))
280 (defun org-md-horizontal-rule (_horizontal-rule _contents _info
)
281 "Transcode HORIZONTAL-RULE element into Markdown format.
282 CONTENTS is the horizontal rule contents. INFO is a plist used
283 as a communication channel."
289 (defun org-md-italic (_italic contents _info
)
290 "Transcode ITALIC object into Markdown format.
291 CONTENTS is the text within italic markup. INFO is a plist used
292 as a communication channel."
293 (format "*%s*" contents
))
298 (defun org-md-item (item contents info
)
299 "Transcode ITEM element into Markdown format.
300 CONTENTS is the item contents. INFO is a plist used as
301 a communication channel."
302 (let* ((type (org-element-property :type
(org-export-get-parent item
)))
303 (struct (org-element-property :structure item
))
304 (bullet (if (not (eq type
'ordered
)) "-"
305 (concat (number-to-string
306 (car (last (org-list-get-item-number
307 (org-element-property :begin item
)
309 (org-list-prevs-alist struct
)
310 (org-list-parents-alist struct
)))))
313 (make-string (- 4 (length bullet
)) ?
)
314 (pcase (org-element-property :checkbox item
)
318 (let ((tag (org-element-property :tag item
)))
319 (and tag
(format "**%s:** "(org-export-data tag info
))))
321 (org-trim (replace-regexp-in-string "^" " " contents
))))))
327 (defun org-md-keyword (keyword contents info
)
328 "Transcode a KEYWORD element into Markdown format.
329 CONTENTS is nil. INFO is a plist used as a communication
331 (if (member (org-element-property :key keyword
) '("MARKDOWN" "MD"))
332 (org-element-property :value keyword
)
333 (org-export-with-backend 'html keyword contents info
)))
338 (defun org-md-line-break (_line-break _contents _info
)
339 "Transcode LINE-BREAK object into Markdown format.
340 CONTENTS is nil. INFO is a plist used as a communication
347 (defun org-md-link (link contents info
)
348 "Transcode LINE-BREAK object into Markdown format.
349 CONTENTS is the link's description. INFO is a plist used as
350 a communication channel."
351 (let ((link-org-files-as-md
353 ;; Treat links to `file.org' as links to `file.md'.
354 (if (string= ".org" (downcase (file-name-extension raw-path
".")))
355 (concat (file-name-sans-extension raw-path
) ".md")
357 (type (org-element-property :type link
)))
359 ;; Link type is handled by a special function.
360 ((org-export-custom-protocol-maybe link contents
'md
))
361 ((member type
'("custom-id" "id" "fuzzy"))
362 (let ((destination (if (string= type
"fuzzy")
363 (org-export-resolve-fuzzy-link link info
)
364 (org-export-resolve-id-link link info
))))
365 (pcase (org-element-type destination
)
366 (`plain-text
; External file.
367 (let ((path (funcall link-org-files-as-md destination
)))
368 (if (not contents
) (format "<%s>" path
)
369 (format "[%s](%s)" contents path
))))
374 (cond ((org-string-nw-p contents
))
375 ((org-export-numbered-headline-p destination info
)
376 (mapconcat #'number-to-string
377 (org-export-get-headline-number destination info
)
379 (t (org-export-data (org-element-property :title destination
)
382 (or (org-element-property :CUSTOM_ID destination
)
383 (org-export-get-reference destination info
))))
386 (or (org-string-nw-p contents
)
387 (let ((number (org-export-get-ordinal destination info
)))
390 ((atom number
) (number-to-string number
))
391 (t (mapconcat #'number-to-string number
".")))))))
395 (org-export-get-reference destination info
))))))))
396 ((org-export-inline-image-p link org-html-inline-image-rules
)
397 (let ((path (let ((raw-path (org-element-property :path link
)))
398 (cond ((not (equal "file" type
)) (concat type
":" raw-path
))
399 ((not (file-name-absolute-p raw-path
)) raw-path
)
400 (t (expand-file-name raw-path
)))))
401 (caption (org-export-data
402 (org-export-get-caption
403 (org-export-get-parent-element link
)) info
)))
405 (if (not (org-string-nw-p caption
)) path
406 (format "%s \"%s\"" path caption
)))))
407 ((string= type
"coderef")
408 (let ((ref (org-element-property :path link
)))
409 (format (org-export-get-coderef-format ref contents
)
410 (org-export-resolve-coderef ref info
))))
411 ((equal type
"radio") contents
)
412 (t (let* ((raw-path (org-element-property :path link
))
415 ((member type
'("http" "https" "ftp" "mailto" "irc"))
416 (concat type
":" raw-path
))
417 ((string= type
"file")
418 (org-export-file-uri (funcall link-org-files-as-md raw-path
)))
420 (if (not contents
) (format "<%s>" path
)
421 (format "[%s](%s)" contents path
)))))))
426 (defun org-md-node-property (node-property _contents _info
)
427 "Transcode a NODE-PROPERTY element into Markdown syntax.
428 CONTENTS is nil. INFO is a plist holding contextual
431 (org-element-property :key node-property
)
432 (let ((value (org-element-property :value node-property
)))
433 (if value
(concat " " value
) ""))))
438 (defun org-md-paragraph (paragraph contents _info
)
439 "Transcode PARAGRAPH element into Markdown format.
440 CONTENTS is the paragraph contents. INFO is a plist used as
441 a communication channel."
442 (let ((first-object (car (org-element-contents paragraph
))))
443 ;; If paragraph starts with a #, protect it.
444 (if (and (stringp first-object
) (string-prefix-p "#" first-object
))
445 (concat "\\" contents
)
451 (defun org-md-plain-list (_plain-list contents _info
)
452 "Transcode PLAIN-LIST element into Markdown format.
453 CONTENTS is the plain-list contents. INFO is a plist used as
454 a communication channel."
460 (defun org-md-plain-text (text info
)
461 "Transcode a TEXT string into Markdown format.
462 TEXT is the string to transcode. INFO is a plist holding
463 contextual information."
464 (when (plist-get info
:with-smart-quotes
)
465 (setq text
(org-export-activate-smart-quotes text
:html info
)))
466 ;; Protect ambiguous #. This will protect # at the beginning of
467 ;; a line, but not at the beginning of a paragraph. See
468 ;; `org-md-paragraph'.
469 (setq text
(replace-regexp-in-string "\n#" "\n\\\\#" text
))
470 ;; Protect ambiguous !
471 (setq text
(replace-regexp-in-string "\\(!\\)\\[" "\\\\!" text nil nil
1))
472 ;; Protect `, *, _ and \
473 (setq text
(replace-regexp-in-string "[`*_\\]" "\\\\\\&" text
))
474 ;; Handle special strings, if required.
475 (when (plist-get info
:with-special-strings
)
476 (setq text
(org-html-convert-special-strings text
)))
477 ;; Handle break preservation, if required.
478 (when (plist-get info
:preserve-breaks
)
479 (setq text
(replace-regexp-in-string "[ \t]*\n" " \n" text
)))
486 (defun org-md-property-drawer (_property-drawer contents _info
)
487 "Transcode a PROPERTY-DRAWER element into Markdown format.
488 CONTENTS holds the contents of the drawer. INFO is a plist
489 holding contextual information."
490 (and (org-string-nw-p contents
)
491 (replace-regexp-in-string "^" " " contents
)))
496 (defun org-md-quote-block (_quote-block contents _info
)
497 "Transcode QUOTE-BLOCK element into Markdown format.
498 CONTENTS is the quote-block contents. INFO is a plist used as
499 a communication channel."
500 (replace-regexp-in-string
502 (replace-regexp-in-string "\n\\'" "" contents
)))
507 (defun org-md-section (_section contents _info
)
508 "Transcode SECTION element into Markdown format.
509 CONTENTS is the section contents. INFO is a plist used as
510 a communication channel."
516 (defun org-md--footnote-formatted (footnote info
)
517 "Formats a single footnote entry FOOTNOTE.
518 FOOTNOTE is a cons cell of the form (number . definition).
519 INFO is a plist with contextual information."
520 (let* ((fn-num (car footnote
))
521 (fn-text (cdr footnote
))
522 (fn-format (plist-get info
:md-footnote-format
))
523 (fn-anchor (format "fn.%d" fn-num
))
524 (fn-href (format " href=\"#fnr.%d\"" fn-num
))
525 (fn-link-to-ref (org-html--anchor fn-anchor fn-num fn-href info
)))
526 (concat (format fn-format fn-link-to-ref
) " " fn-text
"\n")))
528 (defun org-md--footnote-section (info)
529 "Format the footnote section.
530 INFO is a plist used as a communication channel."
531 (let* ((fn-alist (org-export-collect-footnote-definitions info
))
532 (fn-alist (cl-loop for
(n _type raw
) in fn-alist collect
533 (cons n
(org-trim (org-export-data raw info
)))))
534 (headline-style (plist-get info
:md-headline-style
))
535 (section-title (org-html--translate "Footnotes" info
)))
537 (format (plist-get info
:md-footnotes-section
)
538 (org-md--headline-title headline-style
1 section-title
)
539 (mapconcat (lambda (fn) (org-md--footnote-formatted fn info
))
543 (defun org-md-inner-template (contents info
)
544 "Return body of document after converting it to Markdown syntax.
545 CONTENTS is the transcoded contents string. INFO is a plist
546 holding export options."
547 ;; Make sure CONTENTS is separated from table of contents and
548 ;; footnotes with at least a blank line.
550 ;; Table of contents.
551 (let ((depth (plist-get info
:with-toc
)))
552 (when depth
(org-html-toc depth info
)))
553 ;; Document contents.
556 ;; Footnotes section.
557 (org-md--footnote-section info
)))
559 (defun org-md-template (contents _info
)
560 "Return complete document string after Markdown conversion.
561 CONTENTS is the transcoded contents string. INFO is a plist used
562 as a communication channel."
567 ;;; Interactive function
570 (defun org-md-export-as-markdown (&optional async subtreep visible-only
)
571 "Export current buffer to a Markdown buffer.
573 If narrowing is active in the current buffer, only export its
576 If a region is active, export that region.
578 A non-nil optional argument ASYNC means the process should happen
579 asynchronously. The resulting buffer should be accessible
580 through the `org-export-stack' interface.
582 When optional argument SUBTREEP is non-nil, export the sub-tree
583 at point, extracting information from the headline properties
586 When optional argument VISIBLE-ONLY is non-nil, don't export
587 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
593 (org-export-to-buffer 'md
"*Org MD Export*"
594 async subtreep visible-only nil nil
(lambda () (text-mode))))
597 (defun org-md-convert-region-to-md ()
598 "Assume the current region has Org syntax, and convert it to Markdown.
599 This can be used in any buffer. For example, you can write an
600 itemized list in Org syntax in a Markdown buffer and use
601 this command to convert it."
603 (org-export-replace-region-by 'md
))
607 (defun org-md-export-to-markdown (&optional async subtreep visible-only
)
608 "Export current buffer to a Markdown file.
610 If narrowing is active in the current buffer, only export its
613 If a region is active, export that region.
615 A non-nil optional argument ASYNC means the process should happen
616 asynchronously. The resulting file should be accessible through
617 the `org-export-stack' interface.
619 When optional argument SUBTREEP is non-nil, export the sub-tree
620 at point, extracting information from the headline properties
623 When optional argument VISIBLE-ONLY is non-nil, don't export
624 contents of hidden elements.
626 Return output file's name."
628 (let ((outfile (org-export-output-file-name ".md" subtreep
)))
629 (org-export-to-file 'md outfile async subtreep visible-only
)))
632 (defun org-md-publish-to-md (plist filename pub-dir
)
633 "Publish an org file to Markdown.
635 FILENAME is the filename of the Org file to be published. PLIST
636 is the property list for the given project. PUB-DIR is the
637 publishing directory.
639 Return output file name."
640 (org-publish-org-to 'md filename
".md" plist pub-dir
))
645 ;; generated-autoload-file: "org-loaddefs.el"
648 ;;; ox-md.el ends here