fix: an index lists the levels it declares, not all five

The TOC node regenerated over every heading level and minted a title of its
own. A file asking for level 1 only (text:outline-level, Word's TOC \o range)
got a 43-entry index instead of 12 — a full page where LibreOffice needs a
quarter, plus a second "Table of Contents" under the file's own heading.

maxLevel and an empty title now round-trip through both formats.
This commit is contained in:
Steffen Becker committed 2026-08-09 14:54:58 +00:00
1 parent 2dcf8b8a20
commit 84eb7d6bf3
7 files changed
+97 -30

No files matched your search

+10 -6
View File
@@ -984,12 +984,16 @@ function blocksToDocx(content: TiptapNode[], num: Numbering, contentWidthCm: num
textBoxes.push(textBoxDocxDescriptor(node));
out.push(new Paragraph({ children: [new TextRun({ text: `${TBX}${i}${TBX}` })] }));
} else if (node.type === 'tableOfContents') {
// A real, recognized TOC field (levels 1–3, hyperlinked); Word/LibreOffice populate
// + link it on field update (features.updateFields does this on open). Title is a
// plain bold paragraph so it isn't itself listed; our importer regenerates the node.
const tocTitle = typeof node.attrs?.title === 'string' && node.attrs.title ? node.attrs.title : 'Table of Contents';
out.push(new Paragraph({ children: [new TextRun({ text: tocTitle, bold: true, size: 32 })], spacing: { after: cmToTwip(0.3) } }));
out.push(new TableOfContents(tocTitle, { hyperlink: true, headingStyleRange: `1-${MAX_HEADING_LEVEL}` }));
// A real, recognized TOC field (hyperlinked, over the index's own heading levels);
// Word/LibreOffice populate + link it on field update (features.updateFields does
// this on open). Title is a plain bold paragraph so it isn't itself listed, and is
// omitted where the index has none; our importer regenerates the node.
const rawTitle = node.attrs?.title;
const tocTitle = typeof rawTitle === 'string' ? rawTitle : 'Table of Contents';
const depth = Number(node.attrs?.maxLevel);
const maxLevel = depth >= 1 ? Math.min(MAX_HEADING_LEVEL, depth) : MAX_HEADING_LEVEL;
if (tocTitle) out.push(new Paragraph({ children: [new TextRun({ text: tocTitle, bold: true, size: 32 })], spacing: { after: cmToTwip(0.3) } }));
out.push(new TableOfContents(tocTitle, { hyperlink: true, headingStyleRange: `1-${maxLevel}` }));
}
}
return out;
+15 -8
View File
@@ -673,7 +673,7 @@ function replaceColumns(doc: TiptapNode, cols: ColumnsExport[]): TiptapNode {
// One generated table of contents, collected by replaceTableOfContents and emitted by
// applyToc. Entries are the cached heading→page rows (the node view keeps them current).
type TocEntry = { text: string; level: number; page: number };
type TocExport = { entries: TocEntry[]; title: string | null };
type TocExport = { entries: TocEntry[]; title: string | null; maxLevel: number };
// Swap each top-level tableOfContents node for a marker paragraph carrying the TOC
// sentinel and collect its cached entries. Top-level only (like replacePageBreaks): a
@@ -692,7 +692,12 @@ function replaceTableOfContents(doc: TiptapNode, tocs: TocExport[]): TiptapNode
page: Math.max(1, Number(e.page) || 1),
}));
const rawTitle = child.attrs?.title;
tocs.push({ entries, title: typeof rawTitle === 'string' && rawTitle ? rawTitle : null });
const depth = Number(child.attrs?.maxLevel);
tocs.push({
entries,
title: typeof rawTitle === 'string' ? rawTitle : null,
maxLevel: depth >= 1 ? Math.min(MAX_HEADING_LEVEL, depth) : MAX_HEADING_LEVEL,
});
content.push({ type: 'paragraph', content: [{ type: 'text', text: `${TOC_SENT}${tocs.length - 1}${TOC_SENT}` }] });
continue;
}
@@ -3118,9 +3123,9 @@ function tocXml(toc: TocExport, index: number): string {
const title = toc.title ?? 'Table of Contents';
const name = `Table of Contents${index + 1}`;
const source =
`<text:table-of-content-source text:outline-level="${MAX_HEADING_LEVEL}" text:use-index-marks="false" text:use-index-source-styles="false">` +
`<text:index-title-template text:style-name="Contents_20_Heading">${escapeXml(title)}</text:index-title-template>` +
HEADING_LEVELS
`<text:table-of-content-source text:outline-level="${toc.maxLevel}" text:use-index-marks="false" text:use-index-source-styles="false">` +
(title ? `<text:index-title-template text:style-name="Contents_20_Heading">${escapeXml(title)}</text:index-title-template>` : '') +
HEADING_LEVELS.filter(l => l <= toc.maxLevel)
.map(
l =>
`<text:table-of-content-entry-template text:outline-level="${l}" text:style-name="Contents_20_${l}">` +
@@ -3135,9 +3140,11 @@ function tocXml(toc: TocExport, index: number): string {
`</text:table-of-content-source>`;
const body =
`<text:index-body>` +
`<text:index-title text:name="${escapeXml(name)}_Head">` +
`<text:p text:style-name="Contents_20_Heading">${escapeXml(title)}</text:p>` +
`</text:index-title>` +
(title
? `<text:index-title text:name="${escapeXml(name)}_Head">` +
`<text:p text:style-name="Contents_20_Heading">${escapeXml(title)}</text:p>` +
`</text:index-title>`
: '') +
toc.entries
.map(e => `<text:p text:style-name="Contents_20_${e.level}">${escapeXml(e.text)}<text:tab/>${e.page}</text:p>`)
.join('') +