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?

Francesco Cadei
  • 327
  • 3
  • 17
  • 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. –  Jul 10 '18 at 16:07

3 Answers3

5

I have found a workaround using ox-gfm

Francesco Cadei
  • 327
  • 3
  • 17
3

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

mclear
  • 1,525
  • 9
  • 17
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.

Tobias
  • 32,569
  • 1
  • 34
  • 75