-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Use stable anchors in table of contents. #37
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine | ||
|
||
;; Copyright (C) 2014-2017 Lars Tveito | ||
;; Copyright (C) 2012-2018 Free Software Foundation, Inc. | ||
;; Copyright (C) 2018 Aaron Muir Hamilton | ||
|
||
;; Author: Lars Tveito | ||
;; Keywords: org, wp, markdown, github | ||
|
@@ -53,7 +55,8 @@ | |
(lambda (a s v b) | ||
(if a (org-gfm-export-to-markdown t s v) | ||
(org-open-file (org-gfm-export-to-markdown nil s v))))))) | ||
:translate-alist '((inner-template . org-gfm-inner-template) | ||
:translate-alist '((headline . org-gfm-headline) | ||
(inner-template . org-gfm-inner-template) | ||
(paragraph . org-gfm-paragraph) | ||
(strike-through . org-gfm-strike-through) | ||
(src-block . org-gfm-src-block) | ||
|
@@ -65,6 +68,47 @@ | |
|
||
;;; Transcode Functions | ||
|
||
;;;; Headline | ||
(defun org-gfm-headline (headline contents info) | ||
"Transcode HEADLINE element into Markdown format. | ||
CONTENTS is the headline contents. INFO is a plist used as | ||
a communication channel." | ||
(unless (org-element-property :footnote-section-p headline) | ||
(let* ((level (org-export-get-relative-level headline info)) | ||
(title (org-export-data (org-element-property :title headline) info)) | ||
(todo (and (plist-get info :with-todo-keywords) | ||
(let ((todo (org-element-property :todo-keyword | ||
headline))) | ||
(and todo (concat (org-export-data todo info) " "))))) | ||
(tags (and (plist-get info :with-tags) | ||
(let ((tag-list (org-export-get-tags headline info))) | ||
(and tag-list | ||
(format " :%s:" | ||
(mapconcat 'identity tag-list ":")))))) | ||
(priority | ||
(and (plist-get info :with-priority) | ||
(let ((char (org-element-property :priority headline))) | ||
(and char (format "[#%c] " char))))) | ||
;; Headline text without tags. | ||
(heading (concat todo priority title)) | ||
(style (plist-get info :md-headline-style))) | ||
(cond | ||
;; Cannot create a headline. Fall-back to a list. | ||
((or (org-export-low-level-p headline info) | ||
(not (memq style '(atx setext))) | ||
(and (eq style 'atx) (> level 6)) | ||
(and (eq style 'setext) (> level 2))) | ||
(let ((bullet | ||
(if (not (org-export-numbered-headline-p headline info)) "-" | ||
(concat (number-to-string | ||
(car (last (org-export-get-headline-number | ||
headline info)))) | ||
".")))) | ||
(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 heading tags) contents)))))) | ||
|
||
;;;; Paragraph | ||
|
||
(defun org-gfm-paragraph (paragraph contents info) | ||
|
@@ -229,15 +273,33 @@ contextual information." | |
|
||
;;;; Table of contents | ||
|
||
(defun org-gfm-format-toc (headline) | ||
(defun org-gfm-anchor-from-title (title) | ||
(replace-regexp-in-string "[^a-z\-_]" "" (downcase (replace-regexp-in-string "[ ]" "-" title)))) | ||
|
||
(defun org-gfm-anchor-from-headline (headline) | ||
(org-gfm-anchor-from-title (org-export-data (org-export-get-alt-title headline info) info))) | ||
|
||
(defun org-gfm-put-headline-dup (acc headline anchor counter) | ||
(if (assoc (concat anchor "-" (number-to-string counter)) acc) | ||
(org-gfm-put-headline-dup acc headline anchor (1+ counter)) | ||
(cons (cons (concat anchor "-" (number-to-string counter)) headline) acc))) | ||
|
||
(defun org-gfm-put-headline (acc headline) | ||
(let* ((anchor (org-gfm-anchor-from-headline headline))) | ||
(if (assoc anchor acc) | ||
(org-gfm-put-headline-dup acc headline anchor 1) | ||
(cons (cons anchor headline) acc)))) | ||
|
||
|
||
(defun org-gfm-format-toc (anchor-and-headline) | ||
"Return an appropriate table of contents entry for HEADLINE. INFO is a | ||
plist used as a communication channel." | ||
(let* ((title (org-export-data | ||
(let* ((headline (cdr anchor-and-headline)) | ||
(title (org-export-data | ||
(org-export-get-alt-title headline info) info)) | ||
(level (1- (org-element-property :level headline))) | ||
(indent (concat (make-string (* level 2) ? ))) | ||
(anchor (or (org-element-property :CUSTOM_ID headline) | ||
(org-export-get-reference headline info)))) | ||
(anchor (car anchor-and-headline))) | ||
(concat indent "- [" title "]" "(#" anchor ")"))) | ||
|
||
|
||
|
@@ -281,7 +343,8 @@ CONTENTS is the transcoded contents string. INFO is a plist | |
holding export options." | ||
(let* ((depth (plist-get info :with-toc)) | ||
(headlines (and depth (org-export-collect-headlines info depth))) | ||
(toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") "")) | ||
(anchors-to-headlines (reverse (seq-reduce 'org-gfm-put-headline headlines '()))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This causes a warning: "the function ‘seq-reduce’ is not known to be defined." |
||
(toc-string (or (mapconcat 'org-gfm-format-toc anchors-to-headlines "\n") "")) | ||
(toc-tail (if headlines "\n\n" ""))) | ||
(org-trim (concat toc-string toc-tail contents "\n" (org-gfm-footnote-section info))))) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this could use "0-9" for sections with numbers in their name.