diff --git a/ox-hugo.el b/ox-hugo.el index 5343d97..db48ccd 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -1025,12 +1025,21 @@ When optional argument LOCAL is non-nil, build a table of contents according to the current headline." (let* ((toc-headline (unless local - (format "\n
%s
\n\n" + (format "\n
%s
\n" (org-html--translate "Table of Contents" info)))) + (current-level nil) (toc-items (mapconcat (lambda (headline) - (let* ((level (org-export-get-relative-level headline info)) + (let* ((level-raw (org-export-get-relative-level headline info)) + (current-level-inner (progn + (unless current-level + (setq current-level level-raw)) + current-level)) + (relative-level (1+ (- level-raw current-level-inner))) + (level (if local + relative-level + level-raw)) (indentation (make-string (* 4 (1- level)) ?\s)) (todo (and (org-hugo--plist-get-true-p info :with-todo-keywords) (org-element-property :todo-keyword headline))) @@ -1060,7 +1069,10 @@ contents according to the current headline." (and tags (format ":%s:" (mapconcat #'identity tags ":"))))))) + ;; (message "[ox-hugo build-toc DBG] current-level-inner:%d relative-level:%d" + ;; current-level-inner relative-level) ;; (message "[ox-hugo build-toc DBG] level:%d, number:%s" level number) + ;; (message "[ox-hugo build-toc DBG] indentation: %S" indentation) ;; (message "[ox-hugo build-toc DBG] todo: %s | %s" todo todo-str) (concat indentation "- " number toc-entry tags))) (org-export-collect-headlines info n (and local keyword)) @@ -1080,9 +1092,13 @@ contents according to the current headline." " list-style: none;\n" " }\n" "\n")) - "
\n" ;This is a nasty workaround - "
\n" ;till Hugo/Blackfriday support - toc-headline ;wrapping Markdown in HTML div's. + (format "
\n" + (if local + (format " local level-%d" current-level) + "")) + "
\n" ;This is a nasty workaround till Hugo/Blackfriday support + toc-headline ;wrapping Markdown in HTML div's. + "\n" toc-items ;https://github.com/kaushalmodi/ox-hugo/issues/93 "\n\n" "
\n" diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index 7eaaf9a..cfc5c8b 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -4620,6 +4620,36 @@ This post should also be created in =content/section-a/=. :END: #+toc: headlines 6 #+include: "./all-posts.org::#nested-sections-example" :only-contents t +*** TOC Local :local: +:PROPERTIES: +:EXPORT_FILE_NAME: toc-local +:END: +Below, TOC is exported with only level-1 headings in this post. +#+toc: headlines 1 +**** Post sub-heading 1 +Below, TOC is exported with only level-1 headings *relative to* this +"Post sub-heading 1" section. +#+toc: headlines 1 local +***** Post sub-heading 1.1 +****** Post sub-heading 1.1.1 +***** Post sub-heading 1.2 +***** Post sub-heading 1.3 +**** Post sub-heading 2 +***** Post sub-heading 2.1 +***** Post sub-heading 2.2 +Below, TOC is exported with only up to level-2 headings *relative to* +this "Post sub-heading 2.2" section. +#+toc: headlines 2 local +****** Post sub-heading 2.2.1 +****** Post sub-heading 2.2.2 +****** Post sub-heading 2.2.3 +Below, TOC is exported with only level-1 headings *relative to* this +"Post sub-heading 2.2.3" section. +#+toc: headlines 1 local +******* Post sub-heading 2.2.3.1 +******* Post sub-heading 2.2.3.2 +**** Post sub-heading 3 +***** Post sub-heading 3.1 * Pandoc Citations :pandoc:citations: :PROPERTIES: :EXPORT_HUGO_PANDOC_CITATIONS: t diff --git a/test/site/content/posts/toc-local.md b/test/site/content/posts/toc-local.md new file mode 100644 index 0000000..2725c98 --- /dev/null +++ b/test/site/content/posts/toc-local.md @@ -0,0 +1,104 @@ ++++ +title = "TOC Local" +tags = ["keyword", "toc", "local"] +draft = false ++++ + +Below, TOC is exported with only level-1 headings in this post. + +
+
+ +
Table of Contents
+ +- [Post sub-heading 1](#post-sub-heading-1) +- [Post sub-heading 2](#post-sub-heading-2) +- [Post sub-heading 3](#post-sub-heading-3) + +
+ + + +## Post sub-heading 1 {#post-sub-heading-1} + +Below, TOC is exported with only level-1 headings **relative to** this +"Post sub-heading 1" section. + +
+
+ +- [Post sub-heading 1.1](#post-sub-heading-1-dot-1) +- [Post sub-heading 1.2](#post-sub-heading-1-dot-2) +- [Post sub-heading 1.3](#post-sub-heading-1-dot-3) + +
+ + + +### Post sub-heading 1.1 {#post-sub-heading-1-dot-1} + + +#### Post sub-heading 1.1.1 {#post-sub-heading-1-dot-1-dot-1} + + +### Post sub-heading 1.2 {#post-sub-heading-1-dot-2} + + +### Post sub-heading 1.3 {#post-sub-heading-1-dot-3} + + +## Post sub-heading 2 {#post-sub-heading-2} + + +### Post sub-heading 2.1 {#post-sub-heading-2-dot-1} + + +### Post sub-heading 2.2 {#post-sub-heading-2-dot-2} + +Below, TOC is exported with only up to level-2 headings **relative to** +this "Post sub-heading 2.2" section. + +
+
+ +- [Post sub-heading 2.2.1](#post-sub-heading-2-dot-2-dot-1) +- [Post sub-heading 2.2.2](#post-sub-heading-2-dot-2-dot-2) +- [Post sub-heading 2.2.3](#post-sub-heading-2-dot-2-dot-3) + - [Post sub-heading 2.2.3.1](#post-sub-heading-2-dot-2-dot-3-dot-1) + - [Post sub-heading 2.2.3.2](#post-sub-heading-2-dot-2-dot-3-dot-2) + +
+ + + +#### Post sub-heading 2.2.1 {#post-sub-heading-2-dot-2-dot-1} + + +#### Post sub-heading 2.2.2 {#post-sub-heading-2-dot-2-dot-2} + + +#### Post sub-heading 2.2.3 {#post-sub-heading-2-dot-2-dot-3} + +Below, TOC is exported with only level-1 headings **relative to** this +"Post sub-heading 2.2.3" section. + +
+
+ +- [Post sub-heading 2.2.3.1](#post-sub-heading-2-dot-2-dot-3-dot-1) +- [Post sub-heading 2.2.3.2](#post-sub-heading-2-dot-2-dot-3-dot-2) + +
+ + + +##### Post sub-heading 2.2.3.1 {#post-sub-heading-2-dot-2-dot-3-dot-1} + + +##### Post sub-heading 2.2.3.2 {#post-sub-heading-2-dot-2-dot-3-dot-2} + + +## Post sub-heading 3 {#post-sub-heading-3} + + +### Post sub-heading 3.1 {#post-sub-heading-3-dot-1}