Skip to content

Commit 114b79c

Browse files
committed
more robust title element detection
1 parent d89153b commit 114b79c

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,33 @@ export async function canonicalizeTitlePostprocessor(
210210
const h1s = Array.from(doc.querySelectorAll("h1"));
211211
for (const h1n of h1s) {
212212
const h1 = h1n as Element;
213-
if (!h1.classList.contains("quarto-secondary-nav-title")) {
214-
const div = doc.createElement("div");
215-
div.classList.add("quarto-title-banner");
216-
h1.classList.add("title");
217-
header.appendChild(h1);
218-
break;
213+
if (h1.classList.contains("quarto-secondary-nav-title")) {
214+
continue;
219215
}
216+
217+
// Now we need to check whether this is a plausible title element.
218+
if (h1.parentElement?.tagName === "SECTION") {
219+
// If the parent element is a section, then we need to check if there's
220+
// any content before the section. If there is, then this is not a title
221+
if (
222+
h1.parentElement?.parentElement?.firstElementChild !==
223+
h1.parentElement
224+
) {
225+
continue;
226+
}
227+
} else {
228+
// If the parent element is not a section, then we need to check if there's
229+
// any content before the h1. If there is, then this is not a title
230+
if (h1.parentElement?.firstElementChild !== h1) {
231+
continue;
232+
}
233+
}
234+
235+
const div = doc.createElement("div");
236+
div.classList.add("quarto-title-banner");
237+
h1.classList.add("title");
238+
header.appendChild(h1);
239+
break;
220240
}
221241
}
222242

0 commit comments

Comments
 (0)