|
| 1 | +/* |
| 2 | + * render-sidebar-markdown-titles.test.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2020-2024 Posit Software, PBC |
| 5 | + * |
| 6 | + */ |
| 7 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 8 | +import { ensureDirSync, safeRemoveSync } from "../../../src/deno_ral/fs.ts"; |
| 9 | +import { testQuartoCmd } from "../../test.ts"; |
| 10 | +import { ensureFileRegexMatches, noErrors } from "../../verify.ts"; |
| 11 | + |
| 12 | +// Regression test for GH #14576 / upstream jgm/pandoc#11687. |
| 13 | +// |
| 14 | +// Sidebar titles are rendered through the navigation "envelope": each title |
| 15 | +// becomes an inline span, the spans are joined into a single markdown document, |
| 16 | +// pandoc renders it, and the results are read back. When many sidebar titles |
| 17 | +// wrap text around a dunder name (e.g. `Transcript.__getitem__()`), joining the |
| 18 | +// spans into one paragraph made pandoc's markdown reader backtrack |
| 19 | +// exponentially over the unresolved `_`-emphasis candidates, hanging the |
| 20 | +// render. The fix joins each span as its own paragraph so the parse stays |
| 21 | +// linear. Pre-fix this render hangs (the test fails by timing out); post-fix it |
| 22 | +// completes quickly. |
| 23 | +// |
| 24 | +// The project is generated on the fly rather than stored as fixtures: the many |
| 25 | +// dunder pages would otherwise bloat the repo and each be picked up as its own |
| 26 | +// smoke-all render target. |
| 27 | + |
| 28 | +const dunderTitles = [ |
| 29 | + "__init__", "__call__", "__repr__", "__str__", "__len__", |
| 30 | + "__getitem__", "__setitem__", "__delitem__", "__iter__", "__next__", |
| 31 | + "__enter__", "__exit__", "__add__", "__sub__", "__mul__", |
| 32 | + "__eq__", "__hash__", "__new__", "__del__", "__contains__", |
| 33 | +]; |
| 34 | + |
| 35 | +const pageName = (i: number) => `page${String(i + 1).padStart(2, "0")}.qmd`; |
| 36 | + |
| 37 | +const projectDir = Deno.makeTempDirSync({ prefix: "quarto-sidebar-titles" }); |
| 38 | +const outputDir = join(projectDir, "_site"); |
| 39 | + |
| 40 | +const writeProject = () => { |
| 41 | + ensureDirSync(projectDir); |
| 42 | + |
| 43 | + const sidebarContents = [ |
| 44 | + "index.qmd", |
| 45 | + ...dunderTitles.map((_, i) => pageName(i)), |
| 46 | + "bold-page.qmd", |
| 47 | + "code-page.qmd", |
| 48 | + ].map((p) => ` - ${p}`).join("\n"); |
| 49 | + |
| 50 | + Deno.writeTextFileSync( |
| 51 | + join(projectDir, "_quarto.yml"), |
| 52 | + `project: |
| 53 | + type: website |
| 54 | +website: |
| 55 | + title: "Sidebar Titles" |
| 56 | + sidebar: |
| 57 | + contents: |
| 58 | +${sidebarContents} |
| 59 | +format: |
| 60 | + html: |
| 61 | + theme: cosmo |
| 62 | +`, |
| 63 | + ); |
| 64 | + |
| 65 | + Deno.writeTextFileSync( |
| 66 | + join(projectDir, "index.qmd"), |
| 67 | + `---\ntitle: "Home"\n---\n\nSidebar markdown-title regression guard for #14576.\n`, |
| 68 | + ); |
| 69 | + |
| 70 | + dunderTitles.forEach((dunder, i) => { |
| 71 | + Deno.writeTextFileSync( |
| 72 | + join(projectDir, pageName(i)), |
| 73 | + `---\ntitle: "Transcript.${dunder}()"\n---\n\nPage for \`Transcript.${dunder}()\`.\n`, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + Deno.writeTextFileSync( |
| 78 | + join(projectDir, "bold-page.qmd"), |
| 79 | + `---\ntitle: "A **bold** word"\n---\n\nBold title page.\n`, |
| 80 | + ); |
| 81 | + Deno.writeTextFileSync( |
| 82 | + join(projectDir, "code-page.qmd"), |
| 83 | + `---\ntitle: "A \`code\` word"\n---\n\nCode title page.\n`, |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +testQuartoCmd( |
| 88 | + "render", |
| 89 | + [projectDir], |
| 90 | + [ |
| 91 | + noErrors, |
| 92 | + ensureFileRegexMatches( |
| 93 | + join(outputDir, "index.html"), |
| 94 | + [ |
| 95 | + // Dunder titles render literally (intraword_underscores keeps `__x__` |
| 96 | + // out of emphasis), and reaching this assertion at all proves the |
| 97 | + // render did not hang. |
| 98 | + /Transcript\.__getitem__\(\)/, |
| 99 | + // Markdown inside titles still renders correctly through the envelope. |
| 100 | + /A <strong>bold<\/strong> word/, |
| 101 | + /A <code>code<\/code> word/, |
| 102 | + ], |
| 103 | + [ |
| 104 | + // A dunder name must not be turned into spurious emphasis. |
| 105 | + /<strong>getitem<\/strong>/, |
| 106 | + ], |
| 107 | + ), |
| 108 | + ], |
| 109 | + { |
| 110 | + // Performance budget: the healthy render finishes in well under this. |
| 111 | + // Pre-fix the exponential parse blows far past it, so the test fails fast |
| 112 | + // (instead of riding the default 10-minute timeout) when the fix regresses. |
| 113 | + timeout: 120000, |
| 114 | + setup: async () => { |
| 115 | + writeProject(); |
| 116 | + }, |
| 117 | + teardown: async () => { |
| 118 | + safeRemoveSync(projectDir, { recursive: true }); |
| 119 | + }, |
| 120 | + }, |
| 121 | +); |
0 commit comments