Skip to content

Commit a04d87a

Browse files
authored
filters: keep heading id on sections Pandoc 3.13 merges with a wrapper div
Pandoc 3.10.1+ merges a Div with an id that starts with a heading into the heading's section and gives the section the Div's id (jgm/pandoc#11745). In HTML the TOC, anchor links, search and the crossref preview then use the wrapper id while crossrefs use the heading id. Beamer, DocBook, ConTeXt and AsciiDoc drop the heading id entirely (jgm/pandoc#12070), and revealjs strips slide heading ids, so crossrefs to such headings dangle. A finalize filter swaps the two ids exactly where Pandoc merges, keeping the single section and its classes. It follows makeSections' own walk (Div nesting only, sections split by nested headings) and runs only for the formats where the merge changes ids: LaTeX, Typst and docx already write both ids, and a swap there moves the crossref label off the heading.
1 parent c54b99e commit a04d87a

6 files changed

Lines changed: 315 additions & 0 deletions

File tree

‎src/resources/filters/main.lua‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import("./quarto-finalize/meta-cleanup.lua")
100100
-- import("./quarto-finalize/coalesceraw.lua")
101101
-- import("./quarto-finalize/descaffold.lua")
102102
import("./quarto-finalize/finalize-combined-1.lua")
103+
import("./quarto-finalize/section-ids.lua")
103104
import("./quarto-finalize/typst.lua")
104105

105106
import("./normalize/flags.lua")
@@ -619,6 +620,10 @@ local quarto_finalize_filters = {
619620
filter = finalize_combined_1(),
620621
traverser = 'jog',
621622
},
623+
{ name = "finalize-section-ids",
624+
filter = section_ids(),
625+
traverser = 'jog',
626+
},
622627
-- { name = "finalize-coalesce-raw",
623628
-- filters = coalesce_raw(),
624629
-- traverser = 'jog',
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Dash
3+
format: dashboard
4+
_quarto:
5+
tests:
6+
dashboard:
7+
ensureHtmlElements:
8+
- []
9+
- ['.card .card']
10+
---
11+
12+
This is a characterization test, not a test of the section id filter. It
13+
pins Pandoc 3.13's flat dashboard: wrapped rows and cards are not nested
14+
inside other cards. Dashboard sections are `.section` Divs by finalize, so
15+
the filter does not act here.
16+
17+
## Row {height=40%}
18+
19+
### Card A {#card-a}
20+
21+
Alpha.
22+
23+
::: {#wrapped-row}
24+
## Row 2
25+
26+
### Card B
27+
28+
Beta.
29+
:::
30+
31+
## Row 3
32+
33+
::: {#wrapped-card .myc}
34+
### Card C {#card-c}
35+
36+
Gamma.
37+
:::
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Section wrapper ids, no merge
3+
format: html
4+
_quarto:
5+
tests:
6+
html:
7+
ensureHtmlElements:
8+
- - 'div#outer > section#sec-outer'
9+
- 'section#sec-inner > h2#inner'
10+
- 'blockquote div#bq > h2#sec-q'
11+
- - 'section#outer'
12+
- 'div#sec-q'
13+
---
14+
15+
Pandoc only merges a wrapper Div into its section when the heading is the
16+
Div's first block and no later heading in the Div closes that section, and
17+
only for Divs reached through Div nesting. This document pins three shapes
18+
where Quarto's id swap must follow that rule: a nested same-level heading
19+
splits `#outer` (no merge, no swap) while `#inner` merges (swapped), and a
20+
Div inside a BlockQuote is never merged (no swap).
21+
22+
## First {#sec-first}
23+
24+
::: {#outer}
25+
## Outer {#sec-outer}
26+
27+
Outer text.
28+
29+
::: {#inner}
30+
## Inner {#sec-inner}
31+
32+
Inner text.
33+
:::
34+
:::
35+
36+
> ::: {#bq}
37+
> ## InQuote {#sec-q}
38+
>
39+
> Quoted.
40+
> :::
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Section wrapper ids, revealjs
3+
format: revealjs
4+
_quarto:
5+
tests:
6+
revealjs:
7+
ensureHtmlElements:
8+
- - 'section#sec-s2.slide.bg'
9+
- 'section#sec-x.slide'
10+
- 'a.quarto-xref[href="#/sec-s2"]'
11+
- 'div#frag.fragment'
12+
- - 'section#myslide'
13+
- 'section#explicit'
14+
---
15+
16+
Revealjs strips slide heading ids, so a crossref to a heading wrapped in an
17+
id'd Div must resolve to the slide through the heading id moved onto the
18+
section. This covers a plain wrapper, an explicit `.section` wrapper (also
19+
swapped in revealjs), and a `.fragment` wrapper, which is never merged.
20+
21+
## S1 {#sec-s1}
22+
23+
See @sec-s2 and @sec-x.
24+
25+
::: {#myslide .bg}
26+
## S2 {#sec-s2}
27+
28+
Content 2
29+
30+
::: {#frag .fragment}
31+
### Sub {#sec-sub}
32+
33+
Fragment text
34+
:::
35+
:::
36+
37+
::: {#explicit .section}
38+
## X {#sec-x}
39+
40+
Content X
41+
:::
42+
43+
## S4 {#sec-s4}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Section wrapper ids
3+
number-sections: true
4+
toc: true
5+
format:
6+
html: default
7+
epub: default
8+
typst:
9+
keep-typ: true
10+
asciidoc: default
11+
docbook:
12+
output-ext: xml
13+
pdf:
14+
keep-tex: true
15+
beamer:
16+
output-ext: tex
17+
context:
18+
output-ext: tex
19+
_quarto:
20+
tests:
21+
html:
22+
ensureHtmlElements:
23+
- - 'section#sec-second.level2 > h2#sec-other'
24+
- 'section#sec-third.level2.myclass'
25+
- 'nav#TOC a[href="#sec-second"]'
26+
- 'h2[data-anchor-id="sec-second"]'
27+
- 'section#explicit > h2#sec-sixth'
28+
- 'div#nomerge > section#sec-seventh'
29+
- - 'section#sec-other'
30+
- 'section#sec-sixth'
31+
epub:
32+
ensureEpubFileRegexMatches:
33+
- path: EPUB/nav.xhtml
34+
regexes:
35+
- ['ch001\.xhtml#sec-second', 'ch001\.xhtml#sec-third']
36+
- ['ch001\.xhtml#sec-other']
37+
beamer:
38+
ensureFileRegexMatches:
39+
- ['\\label\{sec-second\}', '\\label\{sec-third\}', '\\label\{sec-sixth\}']
40+
- []
41+
docbook:
42+
ensureFileRegexMatches:
43+
- ['xml:id="sec-second"', 'xml:id="sec-third"', 'xml:id="sec-sixth"']
44+
- []
45+
context:
46+
ensureFileRegexMatches:
47+
- ['reference=\{sec-second\}', 'reference=\{sec-third\}', 'reference=\{sec-sixth\}']
48+
- []
49+
asciidoc:
50+
ensureFileRegexMatches:
51+
- ['\[\[sec-second\]\]', '\[\[sec-third\]\]', '\[\[sec-sixth\]\]']
52+
- []
53+
pdf:
54+
ensureLatexFileRegexMatches:
55+
- ['\\section\{Second\}\\label\{sec-second\}', '\\phantomsection\\label\{sec-other\}']
56+
- ['\\phantomsection\\label\{sec-second\}']
57+
typst:
58+
ensureTypstFileRegexMatches:
59+
- ['= Second(\r\n?|\n)<sec-second>']
60+
- ['\] <sec-second>']
61+
---
62+
63+
Pandoc 3.13 merges a Div with an id that starts with a heading into the
64+
heading's section, giving the section the Div's id. This document checks
65+
that Quarto keeps the heading id on the section (the crossref target) for
66+
the formats where the merge would otherwise change ids, and leaves pdf and
67+
typst alone because they write both ids.
68+
69+
## First {#sec-first}
70+
71+
See @sec-second, @sec-third and @sec-sixth.
72+
73+
::: {#sec-other}
74+
## Second {#sec-second}
75+
76+
Body of second.
77+
:::
78+
79+
::: {#wrap3 .myclass}
80+
## Third {#sec-third}
81+
82+
Body of third.
83+
:::
84+
85+
::: {#explicit .section}
86+
## Sixth {#sec-sixth}
87+
88+
Explicit section div.
89+
:::
90+
91+
::: {#nomerge}
92+
## Seventh {#sec-seventh}
93+
94+
Seven.
95+
96+
## Eighth {#sec-eighth}
97+
98+
Eight.
99+
:::

0 commit comments

Comments
 (0)