fix: an index's page numbers end at its own tab stop

A table-of-content entry template names the stop its page numbers are set
against — the deputy-standards guidance puts it 45mm inside the column,
and every number of its index hung out at the margin instead.
This commit is contained in:
Steffen Becker committed 2026-08-10 12:24:47 +00:00
1 parent 1de879b490
commit 9d4820f0f4
3 files changed
+33 -5

No files matched your search

@@ -55,6 +55,13 @@ export const TableOfContents = Node.create({
parseHTML: el => (el as HTMLElement).getAttribute('data-toc-leader') || null,
renderHTML: attrs => (attrs.leader ? { 'data-toc-leader': String(attrs.leader) } : {}),
},
// Where the page number ends, in cm from the text margin — the file's own stop.
// null = the end of the column, which is where a fresh index puts it.
tabPosCm: {
default: null,
parseHTML: el => Number((el as HTMLElement).getAttribute('data-toc-tab')) || null,
renderHTML: attrs => (attrs.tabPosCm ? { 'data-toc-tab': String(attrs.tabPosCm) } : {}),
},
entries: {
default: [] as TocEntry[],
parseHTML: el => {
@@ -245,9 +252,20 @@ class TocView {
}
this.dom.appendChild(row);
});
this.stopAtTab();
this.fillLeaders();
}
// Pull the rows' right edge in to the index's own tab stop, so the page numbers end
// where the file puts them rather than at the column's edge.
private stopAtTab(): void {
const cm = Number(this.node()?.attrs?.tabPosCm);
this.dom.style.paddingRight = '';
if (!(cm > 0)) return;
const inset = this.dom.clientWidth - (cm * 96) / 2.54;
if (inset > 1) this.dom.style.paddingRight = `${Math.round(inset)}px`;
}
// As many leader dots as the gap holds. The row clips the rest on screen, but nothing
// else does: 200 of them reach the PDF, the clipboard and every measurement as text.
private fillLeaders(): void {
+5 -2
View File
@@ -679,7 +679,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; maxLevel: number; leader: string | null };
type TocExport = { entries: TocEntry[]; title: string | null; maxLevel: number; leader: string | null; tabPosCm: number | null };
// 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
@@ -704,6 +704,7 @@ function replaceTableOfContents(doc: TiptapNode, tocs: TocExport[]): TiptapNode
title: typeof rawTitle === 'string' ? rawTitle : null,
maxLevel: depth >= 1 ? Math.min(MAX_HEADING_LEVEL, depth) : MAX_HEADING_LEVEL,
leader: normalizeLeader(child.attrs?.leader),
tabPosCm: typeof child.attrs?.tabPosCm === 'number' ? child.attrs.tabPosCm : null,
});
content.push({ type: 'paragraph', content: [{ type: 'text', text: `${TOC_SENT}${tocs.length - 1}${TOC_SENT}` }] });
continue;
@@ -3171,7 +3172,9 @@ function tocXml(toc: TocExport, index: number): string {
`<text:table-of-content-entry-template text:outline-level="${l}" text:style-name="Contents_20_${l}">` +
`<text:index-entry-link-start/>` +
`<text:index-entry-text/>` +
`<text:index-entry-tab-stop style:type="right"${toc.leader ? ` style:leader-char="${escapeXml(toc.leader)}"` : ''}/>` +
`<text:index-entry-tab-stop style:type="right"` +
`${toc.tabPosCm ? ` style:position="${toc.tabPosCm}cm"` : ''}` +
`${toc.leader ? ` style:leader-char="${escapeXml(toc.leader)}"` : ''}/>` +
`<text:index-entry-page-number/>` +
`<text:index-entry-link-end/>` +
`</text:table-of-content-entry-template>`,
+10 -3
View File
@@ -855,7 +855,7 @@ function convertBlocks(elements: Element[], ctx: Ctx, kind: BlockKind, boldByDef
else out.push(...inner);
} else if (el.localName === 'table-of-content' && kind === 'body') {
// Generated table of contents → a tableOfContents node (regenerated live).
out.push(convertToc(el));
out.push(convertToc(el, ctx));
} else if (/-index$|^table-of-content$|^bibliography$/.test(el.localName)) {
// Other generated indexes (bibliography, …) or a TOC nested in a cell: keep the
// rendered text from index-body.
@@ -899,7 +899,7 @@ function convertBlocks(elements: Element[], ctx: Ctx, kind: BlockKind, boldByDef
// A <text:table-of-content> → a tableOfContents node. Entries (text + level + page) are
// parsed from the cached index-body as a starting cache; the node view recomputes page
// numbers live after mount, so parse fidelity isn't critical.
function convertToc(el: Element): Node {
function convertToc(el: Element, ctx: Ctx): Node {
const indexBody = el.getElementsByTagNameNS(NS.text, 'index-body')[0];
const entries: { text: string; level: number; page: number }[] = [];
if (indexBody) {
@@ -925,7 +925,14 @@ function convertToc(el: Element): Node {
const stop = source?.getElementsByTagNameNS(NS.style, 'index-entry-tab-stop')[0]
?? source?.getElementsByTagNameNS(NS.text, 'index-entry-tab-stop')[0];
const leader = source ? normalizeLeader(stop?.getAttributeNS(NS.style, 'leader-char')) : '.';
return { type: 'tableOfContents', attrs: { entries, title, maxLevel, leader } };
// Where the page number ends: the entry template's own stop, else the right stop of
// the paragraph style its entries use. Word and LibreOffice both put it short of the
// text width in some templates, and the number then hangs 45mm out of place.
const template = source?.getElementsByTagNameNS(NS.text, 'table-of-content-entry-template')[0];
const styled = ctx.resolver.tabStops(template?.getAttributeNS(NS.text, 'style-name') ?? null);
const tabPosCm = lengthToCm(stop?.getAttributeNS(NS.style, 'position'))
?? [...styled].reverse().find(t => t.align === 'right')?.pos ?? null;
return { type: 'tableOfContents', attrs: { entries, title, maxLevel, leader, tabPosCm } };
}
// Split a TOC entry paragraph around its last <text:tab/>: the text before it is the