Skip to content

Commit e92eb44

Browse files
authored
Keep heading id on sections when Pandoc merges a wrapper div (#15344)
Pandoc 3.10.1 and later merge a Div whose id starts with a heading into one section that takes the Div's id, so in HTML the TOC, anchor links and search point at the wrapper id while crossrefs point at the heading id. Beamer, DocBook, ConTeXt and AsciiDoc drop the heading id and revealjs strips it, so crossrefs to those headings dangle. A finalize filter swaps the two ids where Pandoc merges, so the section keeps the heading id and the wrapper id stays addressable on the heading. - `quarto-finalize/section-ids.lua` swaps the ids only for HTML, EPUB, revealjs, beamer, DocBook, ConTeXt and AsciiDoc. LaTeX, Typst and docx already write both ids, and a swap there would move the crossref label off the heading. - The condition mirrors Pandoc's `makeSections`: Div nesting only, with sections ending at a later heading of the same or higher level. - Divs with the `cell`, `column`, `columns` or `fragment` classes are skipped. An explicit `.section` Div in HTML keeps Pandoc's result. See #15320 for the Pandoc 3.13 update.
1 parent c444133 commit e92eb44

7 files changed

Lines changed: 316 additions & 0 deletions

File tree

‎news/changelog-1.12.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All changes included in 1.12:
1414
- ([jgm/pandoc#11845](https://github.com/jgm/pandoc/issues/11845)): In Word output, cross-reference targets are now hidden bookmarks with generated names instead of their Quarto id (`fig-plot`). Cross-reference links inside the document still work, but a link or script that looks a bookmark up by its Quarto id from outside the document, such as `report.docx#fig-plot` or `officer`, VBA or `python-docx` code, no longer finds it.
1515
- ([jgm/pandoc 3.13](https://github.com/jgm/pandoc/releases/tag/3.13)): A YAML bibliography file with a duplicated key, such as two `title` fields in one reference, now stops the render with an error giving the file, line and column. Before, the last value was kept silently. Remove the duplicate key to render again.
1616
- ([jgm/pandoc@c73a35a](https://github.com/jgm/pandoc/commit/c73a35a2e7ed1144132f1e533b4f0ae4c3648143)): Support Pandoc's `--math-method` option in `quarto render`, so that `--math-method=katex` works with `--embed-resources` and keeps math intact in `llms.md` output, like `--katex` does.
17+
- ([jgm/pandoc#11745](https://github.com/jgm/pandoc/issues/11745)): A Div with an id that starts with a heading now renders as a single section instead of a div around a section, and the wrapper's classes move onto that section (CSS such as `div.myclass > section` no longer matches). Revealjs now shows such slides and dashboards no longer nest such rows or cards. In HTML, EPUB, revealjs, beamer, DocBook, ConTeXt and AsciiDoc, Quarto keeps the heading id on the section, so crossrefs and table of contents links still target the heading; in beamer, DocBook, ConTeXt, AsciiDoc and revealjs the wrapper id is no longer written. An explicit `.section` wrapper in HTML follows Pandoc's new behavior: the section takes the wrapper id.
1718

1819
## Accessibility
1920

‎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)