Преглед изворни кода

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.
Nicolas Goaziou пре 8 година
родитељ
комит
4cb6abe3b7
1 измењених фајлова са 23 додато и 9 уклоњено
  1. 23 9
      lisp/ox-md.el

+ 23 - 9
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 "<a id=\"%s\"></a>"
-			 (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 "<a id=\"%s\"></a>"
+			    (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.