6

I can export Markdown from Org (using C-c C-e) but #+BEGIN_SRC lang ... #+END_SRC is not exported as i expected as: ```lang ... ```.

I would like that the code source will be replaced with ``` (markdown syntax), any suggestion?

1
  • I don't know whether tagging source code blocks with languages is standardized in markdown, and whether language names are consistent. Your best bet is probably looking through ox-md.el and looking whether there's any option to customize it the way you like it.
    – user12563
    Jul 10, 2018 at 16:07

3 Answers 3

5

I have found a workaround using ox-gfm

3

You could also just use ox-pandoc, which does all this properly. Though you need to have pandoc installed.

1

It is relatively simple to derive a new exporter backend (e.g., mymd) from the md backend and only override the translation function for example blocks.

The function org-mymd-example-block formats its elements as you like it.

All other functions in the following elisp code are almost verbatim copies from ox-md.el and one only needs to steal a small part of ox-md.el. All other stuff is automatically inherited from the md backend.

(org-export-define-derived-backend 'mymd 'md
  :menu-entry
  '(?y "Export to My Markdown"
       ((?M "To temporary buffer"
            (lambda (a s v b) (org-mymd-export-as-markdown a s v)))
        (?m "To file" (lambda (a s v b) (org-mymd-export-to-markdown a s v)))
        (?o "To file and open"
            (lambda (a s v b)
              (if a (org-mymd-export-to-markdown t s v)
                (org-open-file (org-mymd-export-to-markdown nil s v)))))))
  :translate-alist '((example-block . org-mymd-example-block)
                     (fixed-width . org-mymd-example-block)
                     (src-block . org-mymd-example-block)))

(defun org-mymd-example-block (example-block _content info)
  "Transcode element EXAMPLE-BLOCK as ```lang ...'''."
  (format "```%s\n%s\n'''"
          (org-element-property :language example-block)
          (org-remove-indentation
           (org-export-format-code-default example-block info))))

;;;###autoload
(defun org-mymd-export-as-markdown (&optional async subtreep visible-only)
  "See `org-md-export-as-markdown'."
  (interactive)
  (org-export-to-buffer 'mymd "*Org My MD Export*"
    async subtreep visible-only nil nil (lambda () (text-mode))))

;;;###autoload
(defun org-mymd-convert-region-to-md ()
  "See `org-md-convert-region-to-md'."
  (interactive)
  (org-export-replace-region-by 'mymd))

;;;###autoload
(defun org-mymd-export-to-markdown (&optional async subtreep visible-only)
  "See `org-md-export-to-markdown'."
  (interactive)
  (let ((outfile (org-export-output-file-name ".md" subtreep)))
    (org-export-to-file 'mymd outfile async subtreep visible-only)))

;;;###autoload
(defun org-mymd-publish-to-md (plist filename pub-dir)
  "Analogous to `org-md-publish-to-md'."
  (org-publish-org-to 'mymd filename ".md" plist pub-dir))

An alternative would be to write an :override advice for org-md-example-block. You can use org-mymd-example-block above as replacement of org-md-example-block. But in that way you loose the original md backend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.