Skip to content

Instantly share code, notes, and snippets.

@iravid
Created July 2, 2018 11:51
Show Gist options
  • Save iravid/e5f1b84495a97fc9a0936f29d019f086 to your computer and use it in GitHub Desktop.
Save iravid/e5f1b84495a97fc9a0936f29d019f086 to your computer and use it in GitHub Desktop.
(with-eval-after-load 'org
(progn
(setq org-src-lang-modes
(-cons* '("tut" . scala)
'("tut:silent" . scala)
'("tut:book" . scala)
org-src-lang-modes))
;;(advice-add 'org-md-link :override
(defun org-modified-md-link (link contents info)
(let ((link-org-files-as-md (lambda (raw-path)
;; Treat links to `file.org' as links to `file.md'.
(if (string= ".org" (downcase (file-name-extension raw-path ".")))
(concat (file-name-sans-extension raw-path) ".md")
raw-path)))
(type (org-element-property :type link)))
(message "In modified func")
(cond
;; Link type is handled by a special function.
((org-export-custom-protocol-maybe link contents 'md))
((member type '("custom-id" "id" "fuzzy"))
(let ((destination (if (string= type "fuzzy")
(org-export-resolve-fuzzy-link link info)
(org-export-resolve-id-link link info))))
(pcase (org-element-type destination)
(`plain-text ; External file.
(let ((path (funcall link-org-files-as-md destination)))
(if (not contents) (format "<%s>" path)
(format "[%s](%s)" contents path))))
(`headline
(format
"[%s](#%s)"
;; Description.
(cond ((org-string-nw-p contents))
((org-export-numbered-headline-p destination info)
(mapconcat #'number-to-string
(org-export-get-headline-number destination info)
"."))
(t (org-export-data (org-element-property :title destination)
info)))
;; Reference.
(or (org-element-property :CUSTOM_ID destination)
(org-export-get-reference destination info))))
(_
(let ((description
(or (org-string-nw-p contents)
(let ((number (org-export-get-ordinal destination info)))
(cond
((not number) nil)
((atom number) (number-to-string number))
(t (mapconcat #'number-to-string number ".")))))))
(when description
(format "[%s](#%s)"
description
(org-export-get-reference destination info))))))))
((org-export-inline-image-p link org-html-inline-image-rules)
(let ((path (let ((raw-path (org-element-property :path link)))
(cond ((not (equal "file" type)) (concat type ":" raw-path))
((not (file-name-absolute-p raw-path)) raw-path)
(t (expand-file-name raw-path)))))
(caption (org-export-data
(org-export-get-caption
(org-export-get-parent-element link)) info)))
(format "![%s](%s)"
(if (org-string-nw-p caption) caption "img")
path)))
((string= type "coderef")
(let ((ref (org-element-property :path link)))
(format (org-export-get-coderef-format ref contents)
(org-export-resolve-coderef ref info))))
((equal type "radio") contents)
(t (let* ((raw-path (org-element-property :path link))
(path
(cond
((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path))
((string= type "file")
(org-export-file-uri (funcall link-org-files-as-md raw-path)))
(t raw-path))))
(if (not contents) (format "<%s>" path)
(format "[%s](%s)" contents path)))))))
(defun org-export-to-modified-gfm (&optional async subtreep visible-only)
(interactive)
(let ((outfile (org-export-output-file-name ".md" subtreep)))
(org-export-to-file 'modified-gfm outfile async subtreep visible-only)))
(defun org-export-as-modified-gfm (&optional async subtreep visible-only)
(interactive)
(org-export-to-buffer 'modified-gfm "*Org GFM Export*"
async subtreep visible-only nil nil (lambda () (text-mode))))
(org-export-define-derived-backend 'modified-gfm 'gfm
:filters-alist '((:filter-parse-tree . org-md-separate-elements))
:menu-entry
'(?i "Export to Modified Markdown"
((?I "To temporary buffer"
(lambda (a s v b) (org-export-as-modified-gfm a s v)))
(?i "To file" (lambda (a s v b) (org-export-to-modified-gfm a s v)))
(?o "To file and open"
(lambda (a s v b)
(if a (org-export-to-modified-gfm t s v)
(org-open-file (org-export-to-modified-gfm nil s v)))))))
:translate-alist '((link . org-modified-md-link)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment