diff options
| author | Robert Günzler <r@gnzler.io> | 2026-10-02 10:29:20 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2026-10-02 10:39:48 +0200 |
| commit | f8a7a9ab00efe774f28f8f3211aef90a9abfc6be (patch) | |
| tree | fa96bf687ff8a35dfcaea6fd85e4f5516f267d66 /src | |
| parent | 872661982d4051c62c3e7f96277ca78914fd1df8 (diff) | |
| download | site-f8a7a9ab00efe774f28f8f3211aef90a9abfc6be.tar.gz | |
better support h1 headings in markdown files
we don't want to include them in the generated toc Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
| -rw-r--r-- | src/preproc.lua | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/src/preproc.lua b/src/preproc.lua index 76f0678..4d58053 100644 --- a/src/preproc.lua +++ b/src/preproc.lua @@ -9,50 +9,58 @@ return { return link end, Pandoc = function(doc) - local front = {} + local front = pandoc.List() + local doc_filename = pandoc.path.filename(PANDOC_STATE.input_files[1]) -- add a comment - table.insert( - front, - pandoc.RawBlock( - "html5", - "<!-- Generated file, you may edit and discard " - .. pandoc.path.filename(PANDOC_STATE.input_files[1]) - .. " :) -->" - ) - ) + front:extend({ + pandoc.RawBlock("html5", "<!-- Generated file, you may edit and discard " .. doc_filename .. " :) -->"), + }) local toc = pandoc.structure.table_of_contents(doc) if doc.meta.toc and toc then - table.insert(front, pandoc.RawBlock("html5", '<nav class="table-of-contents capital">')) - table.insert(front, pandoc.RawBlock("html5", "<ol>")) - -- insert jump to top - table.insert(front, pandoc.RawBlock("html5", "<li>")) - table.insert(front, pandoc.Link("back to top", "#top")) - table.insert(front, pandoc.RawBlock("html5", "</li>")) + front:extend({ + pandoc.RawBlock("html5", '<nav class="table-of-contents capital">'), + pandoc.RawBlock("html5", "<ol>"), + -- insert jump to top + pandoc.RawBlock("html5", "<li>"), + pandoc.Link("back to top", "#top"), + pandoc.RawBlock("html5", "</li>"), + }) -- create custom toc toc:walk({ Link = function(e) - table.insert(front, pandoc.RawBlock("html5", "<li>")) - table.insert(front, e) - table.insert(front, pandoc.RawBlock("html5", "</li>")) + -- check if the section title matches the doc title and dont + -- add it to the TOC, it's probably the h1 heading + if string.find(doc_filename, pandoc.utils.stringify(e.content) .. ".md", 1, true) == 1 then + return + end + + front:extend({ + pandoc.RawBlock("html5", "<li>"), + e, + pandoc.RawBlock("html5", "</li>"), + }) end, }) - table.insert(front, pandoc.RawBlock("html5", "</ol>")) - table.insert(front, pandoc.RawBlock("html5", "</nav>")) + front:extend({ + pandoc.RawBlock("html5", "</ol>"), + pandoc.RawBlock("html5", "</nav>"), + }) end -- insert date blocks if doc.meta.date then - table.insert(front, pandoc.RawBlock("html5", '<span class="date">')) - table.insert(front, doc.meta.date) - table.insert(front, pandoc.RawBlock("html5", "</span>")) + front:extend({ + pandoc.RawBlock("html5", '<span class="date">'), + doc.meta.date, + pandoc.RawBlock("html5", "</span>"), + }) end -- prepend front stuffs - for i = #front, 1, -1 do - table.insert(doc.blocks, 1, front[i]) - end + front:extend(doc.blocks) + doc.blocks = front -- return finished ast return doc |