From 4cb6abe3b7d724a310199ec47762d46086c2dec2 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 10 Mar 2017 18:52:06 +0100 Subject: [PATCH] ox-md: Fix internal links * lisp/ox-md.el (org-md-headline): Add an anchor when a link is referring to the headline. (org-md--headline-referred-p): New function. --- lisp/ox-md.el | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lisp/ox-md.el b/lisp/ox-md.el index ad6ee35f6..c8ea1fa04 100644 --- a/lisp/ox-md.el +++ b/lisp/ox-md.el @@ -217,11 +217,6 @@ a communication channel." (and (plist-get info :with-priority) (let ((char (org-element-property :priority headline))) (and char (format "[#%c] " char))))) - (anchor - (and (plist-get info :with-toc) - (format "" - (or (org-element-property :CUSTOM_ID headline) - (org-export-get-reference headline info))))) ;; Headline text without tags. (heading (concat todo priority title)) (style (plist-get info :md-headline-style))) @@ -239,10 +234,29 @@ a communication channel." ".")))) (concat bullet (make-string (- 4 (length bullet)) ?\s) heading tags "\n\n" (and contents (replace-regexp-in-string "^" " " contents))))) - (t (concat (org-md--headline-title style level title anchor tags) contents)))))) - - -;; Headline Title + (t + (let ((anchor + (and (org-md--headline-referred-p headline info) + (format "" + (or (org-element-property :CUSTOM_ID headline) + (org-export-get-reference headline info)))))) + (concat (org-md--headline-title style level title anchor tags) + contents))))))) + + +(defun org-md--headline-referred-p (headline info) + "Non-nil when HEADLINE is being referred to. +INFO is a plist used as a communication channel. Links and table +of contents can refer to headlines." + (or (plist-get info :with-toc) + (org-element-map (plist-get info :parse-tree) 'link + (lambda (link) + (eq headline + (pcase (org-element-property :type link) + ((or "custom-id" "id") (org-export-resolve-id-link link info)) + ("fuzzy" (org-export-resolve-fuzzy-link link info)) + (_ nil)))) + info t))) (defun org-md--headline-title (style level title &optional anchor tags) "Generate a headline title in the preferred Markdown headline style. -- 2.11.4.GIT