feature/table-of-content-docx #2
@ -1,6 +1,3 @@
|
|||||||
-- custom-toc.lua
|
|
||||||
-- This filter builds a custom TOC without images in the link text,
|
|
||||||
-- leaving the actual header content (with images) untouched.
|
|
||||||
|
|
||||||
-- Helper function: remove all image inlines from a list of inlines.
|
-- Helper function: remove all image inlines from a list of inlines.
|
||||||
local function remove_images(inlines)
|
local function remove_images(inlines)
|
||||||
@ -18,11 +15,9 @@ local function build_toc(doc)
|
|||||||
local toc_items = {}
|
local toc_items = {}
|
||||||
for _, block in ipairs(doc.blocks) do
|
for _, block in ipairs(doc.blocks) do
|
||||||
if block.t == "Header" then
|
if block.t == "Header" then
|
||||||
-- Remove images from the header content to use for the TOC.
|
|
||||||
local clean_inlines = remove_images(block.content)
|
local clean_inlines = remove_images(block.content)
|
||||||
local header_text = pandoc.utils.stringify(clean_inlines)
|
local header_text = pandoc.utils.stringify(clean_inlines)
|
||||||
if header_text ~= "" then
|
if header_text ~= "" then
|
||||||
-- Create a link using the cleaned inlines and the header’s identifier.
|
|
||||||
local link = pandoc.Link(clean_inlines, "#" .. block.identifier)
|
local link = pandoc.Link(clean_inlines, "#" .. block.identifier)
|
||||||
table.insert(toc_items, { link })
|
table.insert(toc_items, { link })
|
||||||
end
|
end
|
||||||
@ -33,7 +28,6 @@ end
|
|||||||
|
|
||||||
-- The Pandoc function runs after the document is fully constructed.
|
-- The Pandoc function runs after the document is fully constructed.
|
||||||
function Pandoc(doc)
|
function Pandoc(doc)
|
||||||
-- Build the custom TOC.
|
|
||||||
local toc = build_toc(doc)
|
local toc = build_toc(doc)
|
||||||
-- Insert the TOC at the very beginning of the document.
|
-- Insert the TOC at the very beginning of the document.
|
||||||
table.insert(doc.blocks, 1, toc)
|
table.insert(doc.blocks, 1, toc)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user