Skip to content

Commit 9c87848

Browse files
committed
add title canonicalization html postprocessor
1 parent cc96751 commit 9c87848

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/command/render/pandoc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ import { cacheCodePage, clearCodePageCache } from "../../core/windows.ts";
173173
import { textHighlightThemePath } from "../../quarto-core/text-highlighting.ts";
174174
import { resolveAndFormatDate, resolveDate } from "../../core/date.ts";
175175
import { katexPostProcessor } from "../../format/html/format-html-math.ts";
176+
// import { canonicalizeTitlePostprocessor } from "../../format/html/format-html-title.ts";
176177
import {
177178
readAndInjectDependencies,
178179
writeDependencies,
@@ -201,6 +202,7 @@ import {
201202
MarkdownPipelineHandler,
202203
} from "../../core/markdown-pipeline.ts";
203204
import { getEnv } from "../../../package/src/util/utils.ts";
205+
import { canonicalizeTitlePostprocessor } from "../../format/html/format-html-title.ts";
204206

205207
// in case we are running multiple pandoc processes
206208
// we need to make sure we capture all of the trace files
@@ -431,6 +433,9 @@ export async function runPandoc(
431433
// record postprocessors
432434
postprocessors.push(...(extras.postprocessors || []));
433435

436+
// Fix H1 title inconsistency
437+
htmlPostprocessors.push(canonicalizeTitlePostprocessor);
438+
434439
// add a keep-source post processor if we need one
435440
if (
436441
options.format?.render[kKeepSource] || formatHasCodeTools(options.format)

src/format/html/format-html-title.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ import { existsSync } from "../../deno_ral/fs.ts";
88
import { dirname, isAbsolute, join } from "../../deno_ral/path.ts";
99
import { kDateFormat, kTocLocation } from "../../config/constants.ts";
1010
import { Format, Metadata, PandocFlags } from "../../config/types.ts";
11-
import { Document } from "../../core/deno-dom.ts";
11+
import { Document, Element } from "../../core/deno-dom.ts";
1212
import { formatResourcePath } from "../../core/resources.ts";
1313
import { sassLayer } from "../../core/sass.ts";
1414
import { TempContext } from "../../core/temp-types.ts";
1515
import { MarkdownPipeline } from "../../core/markdown-pipeline.ts";
16+
import {
17+
HtmlPostProcessResult,
18+
PandocInputTraits,
19+
RenderedFormat,
20+
} from "../../command/render/types.ts";
21+
import { InternalError } from "../../core/lib/error.ts";
1622

1723
export const kTitleBlockStyle = "title-block-style";
1824
const kTitleBlockBanner = "title-block-banner";
@@ -177,6 +183,45 @@ export function documentTitlePartial(
177183
}
178184
}
179185

186+
export async function canonicalizeTitlePostprocessor(
187+
doc: Document,
188+
_options: {
189+
inputMetadata: Metadata;
190+
inputTraits: PandocInputTraits;
191+
renderedFormats: RenderedFormat[];
192+
quiet?: boolean;
193+
},
194+
): Promise<HtmlPostProcessResult> {
195+
// https://github.com/quarto-dev/quarto-cli/issues/10567
196+
const titleBlock = doc.querySelector("header.quarto-title-block");
197+
if (!titleBlock) {
198+
const main = doc.querySelector("main");
199+
if (!main) {
200+
throw new InternalError("No main element found in document");
201+
}
202+
const header = doc.createElement("header");
203+
header.id = "title-block-header";
204+
header.classList.add("quarto-title-block");
205+
main.insertBefore(header, main.firstChild);
206+
const h1s = Array.from(doc.querySelectorAll("h1"));
207+
for (const h1n of h1s) {
208+
const h1 = h1n as Element;
209+
if (!h1.classList.contains("quarto-secondary-nav-title")) {
210+
const div = doc.createElement("div");
211+
div.classList.add("quarto-title-banner");
212+
h1.classList.add("title");
213+
header.appendChild(h1);
214+
break;
215+
}
216+
}
217+
}
218+
219+
return {
220+
resources: [],
221+
supporting: [],
222+
};
223+
}
224+
180225
export function processDocumentTitle(
181226
input: string,
182227
format: Format,

0 commit comments

Comments
 (0)