Org-mode mailing list
 help / color / mirror / Atom feed
From: Rodrigo Morales <>
Subject: [PATCH] ox-md.el export code blocks using grave accents
Date: Thu, 28 Jan 2021 22:43:26 -0500
Message-ID: <> (raw)

[PATCH] ox-md.el export code blocks using grave accents.

This patch includes the following changes in =ox-md.el=

+ =org-md-example-block= now exports code blocks using triple grave
  accents instead of four spaces of indentation. This has been done
  for two main reasons:
  1. To be able to include the language so that Markdown engines can
     syntax highlight the content of code blocks
  2. To be able to put the source code and the results of evaluation
     in different code blocks. When using indentation, both the source
     code and the results are shown in the same code block by Markdown
+ The variable =org-md-lang-export= is now included in order to map
  Org Mode language names to Markdown language names.

The file contains a minimal reproducible example; ,
the resulting file when exporting using the current version; and, the resulting file when exporting with the changes
of this patch applied.

The patch is shown below.

#+begin_src dash :dir (progn default-directory) :epilogue ":"
diff -u ox-md.el ox-md-patched.el

--- ox-md.el	2021-01-28 22:18:51.566067501 -0500
+++ ox-md-patched.el	2021-01-28 22:14:34.762735829 -0500
@@ -50,6 +50,14 @@
 	  (const :tag "Use \"atx\" style" atx)
 	  (const :tag "Use \"Setext\" style" setext)))
+(defcustom org-md-lang-export
+  '(("dash" . "sh"))
+  "Alist mapping languages to the corresponding language names in Markdown."
+  :group 'org-export-md
+  :type '(repeat
+	  (cons
+	   (string "Org Mode language name")
+	   (string "Markdown language name"))))
 ;;;; Footnotes
@@ -181,10 +189,24 @@
   "Transcode EXAMPLE-BLOCK element into Markdown format.
 CONTENTS is nil.  INFO is a plist used as a communication
-  (replace-regexp-in-string
-   "^" "    "
-   (org-remove-indentation
-    (org-export-format-code-default example-block info))))
+  (let* (language
+	 (org-language
+	  (plist-get (car (cdr example-block)) :language))
+	 (markdown-language
+	  (cdr (assoc org-language org-md-lang-export))) ;
+	 (content
+	  (org-remove-indentation
+	   (org-export-format-code-default example-block info))))
+    (if markdown-language
+	(setq language markdown-language)
+      (setq language org-language))
+    (setq content (replace-regexp-in-string
+                   "\\`" (concat "```" language "\n")
+                   content))
+    (replace-regexp-in-string "\\'" "```" content)))
 (defun org-md-export-block (export-block contents info)
   "Transcode a EXPORT-BLOCK element from Org to Markdown.

Rodrigo Morales.

             reply	other threads:[~2021-01-31  3:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-29  3:43 Rodrigo Morales [this message]
2021-01-29  3:47 ` Rodrigo Morales
2021-01-31 19:55   ` Rodrigo Morales
2021-01-30 21:57 Rodrigo Morales
2021-01-30 22:51 ` Tim Cross
2021-01-30 23:53   ` Rodrigo Morales

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Org-mode mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror list/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 list list/ \
	public-inbox-index list

Example config snippet for mirrors.
Newsgroups are available over NNTP:

AGPL code for this site: git clone