|
| 1 | +-- section-ids.lua |
| 2 | +-- |
| 3 | +-- Pandoc >= 3.10.1 merges a Div with an id that starts with a heading into |
| 4 | +-- the heading's section and gives the section the Div's id |
| 5 | +-- (jgm/pandoc#11745). Swapping the two ids keeps the heading id, which is |
| 6 | +-- the crossref target, on the section. |
| 7 | +-- |
| 8 | +-- Only formats where the merge changes ids are swapped: LaTeX, Typst and |
| 9 | +-- docx write both ids, so a swap there would move the label off the |
| 10 | +-- heading. The condition mirrors makeSections (Div nesting only, sections |
| 11 | +-- split by later headings). The revealjs/beamer/docbook/context/asciidoc |
| 12 | +-- group works around jgm/pandoc#12070 (writers dropping the heading id) |
| 13 | +-- and Quarto's revealjs heading-attribute stripping; beamer, docbook, |
| 14 | +-- context and asciidoc can leave it once a fixed Pandoc is bundled |
| 15 | +-- (re-check their output first). |
| 16 | +-- |
| 17 | +-- Copyright (C) 2026 Posit Software, PBC |
| 18 | + |
| 19 | +function section_ids() |
| 20 | + -- "swap_section": also swap Divs with class `section` |
| 21 | + local function format_group() |
| 22 | + local format = _quarto.format |
| 23 | + if format.isRevealJsOutput() or format.isBeamerOutput() |
| 24 | + or FORMAT:match("^docbook") or FORMAT == "context" |
| 25 | + or format.isAsciiDocOutput() then |
| 26 | + return "swap_section" |
| 27 | + elseif format.isHtmlOutput() then |
| 28 | + return "skip_section" |
| 29 | + end |
| 30 | + return nil |
| 31 | + end |
| 32 | + |
| 33 | + -- pandoc's headerLtEq: a Header of level <= `level`, or a Div whose |
| 34 | + -- first block is one (recursively) |
| 35 | + local function lt_eq(block, level) |
| 36 | + if block.t == "Header" then |
| 37 | + return block.level <= level |
| 38 | + elseif block.t == "Div" and block.content[1] then |
| 39 | + return lt_eq(block.content[1], level) |
| 40 | + end |
| 41 | + return false |
| 42 | + end |
| 43 | + |
| 44 | + local function should_swap(div, group) |
| 45 | + local header = div.content[1] |
| 46 | + if div.identifier == "" or not header or header.t ~= "Header" |
| 47 | + or header.identifier == "" or header.identifier == div.identifier then |
| 48 | + return false |
| 49 | + end |
| 50 | + for i = 2, #div.content do |
| 51 | + if lt_eq(div.content[i], header.level) then |
| 52 | + return false |
| 53 | + end |
| 54 | + end |
| 55 | + if div.classes:includes("section") then |
| 56 | + return group == "swap_section" |
| 57 | + end |
| 58 | + for _, class in ipairs({ "column", "columns", "fragment", "cell" }) do |
| 59 | + if div.classes:includes(class) then |
| 60 | + return false |
| 61 | + end |
| 62 | + end |
| 63 | + return true |
| 64 | + end |
| 65 | + |
| 66 | + return { |
| 67 | + Pandoc = function(doc) |
| 68 | + local group = format_group() |
| 69 | + if group == nil then |
| 70 | + return nil |
| 71 | + end |
| 72 | + local changed = false |
| 73 | + local function walk(blocks) |
| 74 | + for _, block in ipairs(blocks) do |
| 75 | + if block.t == "Div" then |
| 76 | + if should_swap(block, group) then |
| 77 | + local header = block.content[1] |
| 78 | + block.identifier, header.identifier = header.identifier, block.identifier |
| 79 | + changed = true |
| 80 | + end |
| 81 | + walk(block.content) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + walk(doc.blocks) |
| 86 | + if changed then |
| 87 | + return doc |
| 88 | + end |
| 89 | + end |
| 90 | + } |
| 91 | +end |
0 commit comments