Skip to content

Commit 3c32d32

Browse files
committed
tests: check docx crossref ids map to their bookmarks and links
Since Pandoc 3.12 (jgm/pandoc#11845) every docx bookmark name starts with `_`, which hides it from screen readers, and an id Word would reject as a bookmark name is replaced by a SHA-1 hash of the id. Quarto crossref ids all contain `-`, so `fig-elephant` no longer appears in word/document.xml. Compute each expected name the way Pandoc does and assert that the bookmark exists and the cross-reference link points at it, so the test still checks each id rather than only that some target exists.
1 parent fb99471 commit 3c32d32

1 file changed

Lines changed: 46 additions & 24 deletions

File tree

‎tests/smoke/crossref/docx.test.ts‎

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,55 @@
55
*
66
*/
77

8-
import { ensureDocxRegexMatches } from "../../verify.ts";
8+
import { assert } from "testing/asserts";
9+
import { ensureDocxRegexMatches, verifyDocXDocument } from "../../verify.ts";
910
import { testRender } from "../render/render.ts";
1011
import { crossref } from "./utils.ts";
1112

1213
const allQmd = crossref("all-docx.qmd", "docx");
1314

14-
function bookmarkStart(name: string) {
15-
return RegExp(
16-
`<w:bookmarkStart[\\s]*w:id="[0-9]+"[\\s]*w:name="${name}"[\\s]*\\/>`,
15+
// Mirrors Pandoc's toBookmarkName (jgm/pandoc#11845): an id under 40 chars of
16+
// alphanumerics/underscores becomes "_" + id, anything else "_" + the SHA-1 hex
17+
// of the id without its first character.
18+
async function bookmarkName(id: string) {
19+
if (id.length < 40 && /^[A-Za-z0-9_]+$/.test(id)) {
20+
return `_${id}`;
21+
}
22+
const digest = await crypto.subtle.digest(
23+
"SHA-1",
24+
new TextEncoder().encode(id),
1725
);
26+
const hex = [...new Uint8Array(digest)]
27+
.map((b) => b.toString(16).padStart(2, "0"))
28+
.join("");
29+
return `_${hex.slice(1)}`;
1830
}
1931

20-
function anchor(name: string) {
21-
return RegExp(
22-
`<w:hyperlink w:anchor="${name}">`,
23-
);
32+
function ensureLinksResolveToBookmarks(
33+
file: string,
34+
refs: { id: string; linkText: string }[],
35+
) {
36+
return verifyDocXDocument(async (xml) => {
37+
const bookmarks = [
38+
...xml.matchAll(/<w:bookmarkStart[^>]*w:name="([^"]+)"/g),
39+
].map((m) => m[1]);
40+
for (const { id, linkText } of refs) {
41+
const name = await bookmarkName(id);
42+
assert(
43+
bookmarks.includes(name),
44+
`No bookmark named '${name}' for crossref id '${id}'.`,
45+
);
46+
const link = RegExp(
47+
`<w:hyperlink w:anchor="([^"]+)">(?:(?!</w:hyperlink>).)*?>${linkText}<`,
48+
"s",
49+
).exec(xml);
50+
assert(link, `No internal link with text '${linkText}' (id '${id}').`);
51+
assert(
52+
link[1] === name,
53+
`Link '${linkText}' for crossref id '${id}' has anchor '${link[1]}', expected '${name}'.`,
54+
);
55+
}
56+
}, "Inspecting Docx for crossref links resolving to their bookmarks")(file);
2457
}
2558

2659
function text(text: string) {
@@ -30,8 +63,6 @@ function text(text: string) {
3063
}
3164

3265
const simpleFigRegexes = [
33-
bookmarkStart("fig-elephant"),
34-
anchor("fig-elephant"),
3566
// These tests no longer pass after we started introducing non-breaking
3667
// spaces between 'Figure' and '1' (although upon cracking the file
3768
// it seems like there are no non-breaking spaces and these strings
@@ -41,35 +72,21 @@ const simpleFigRegexes = [
4172
];
4273

4374
const tableRegexes = [
44-
bookmarkStart("tbl-letters"),
45-
anchor("tbl-letters"),
4675
// (see comment above re: non-breaking spaces)
4776
text("Table\\u00A01: My Caption"),
4877
];
4978

5079
const mathRegexes = [
5180
text("Theorem 1 \\(Line\\)"),
52-
bookmarkStart("thm-line"),
53-
anchor("thm-line"),
5481
];
5582

5683
const _subTableRegexes = [
57-
bookmarkStart("tbl-first"),
58-
bookmarkStart("tbl-second"),
59-
anchor("tbl-first"),
60-
anchor("tbl-second"),
6184
text("Table 2: Main Caption"),
6285
text("(a) First Table"),
6386
text("(b) Second Table"),
6487
];
6588

6689
const _subFigRegexes = [
67-
bookmarkStart("fig-elephants"),
68-
bookmarkStart("fig-abbas"),
69-
bookmarkStart("fig-surus"),
70-
anchor("fig-elephants"),
71-
anchor("fig-abbas"),
72-
anchor("fig-surus"),
7390
text("Figure 2: Famous Elephants"),
7491
text("Figure 2"),
7592
text("Figure 2 (b)"),
@@ -83,4 +100,9 @@ testRender(allQmd.input, "docx", true, [
83100
// ...subFigRegexes,
84101
// ...subTableRegexes,
85102
]),
103+
ensureLinksResolveToBookmarks(allQmd.output.outputPath, [
104+
{ id: "fig-elephant", linkText: "Figure 1" },
105+
{ id: "tbl-letters", linkText: "Table 1" },
106+
{ id: "thm-line", linkText: "Theorem 1" },
107+
]),
86108
]);

0 commit comments

Comments
 (0)