Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): match formatted/attributed <h1> when shifting headings

Elian Doran6fa70adb2ff0
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index fb55a079c8..9e6e0d594e 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -192,7 +192,7 @@ function handleH1(content: string, title: string): string {
// shift every heading down one level (clamping at <h6>) to preserve nesting.
// Demoting <h1>→<h2> alone collapsed distinct <h1>/<h2> levels onto the same
// <h2>, flattening the hierarchy (see #8383).
- if (/<h1[^>]*>[^<]*<\/h1>/i.test(content)) {
+ if (/<h1[^>]*>[\s\S]*?<\/h1>/i.test(content)) {
content = shiftHeadingsDown(content);
}
@@ -201,8 +201,9 @@ function handleH1(content: string, title: string): string {
/** Removes the first `<h1>` when its (decoded) text equals the note title. */
function stripDuplicateTitleHeading(content: string, title: string): string {
- // No `g` flag: only the very first <h1> is a title candidate.
- return content.replace(/<h1[^>]*>([^<]*)<\/h1>/i, (match, text: string) =>
+ // No `g` flag: only the very first <h1> is a title candidate. `[\s\S]*?` (not
+ // `[^<]*`) so headings with inline markup or attributes still match.
+ return content.replace(/<h1[^>]*>([\s\S]*?)<\/h1>/i, (match, text: string) =>
unescapeHtml(text).trim() === title.trim() ? "" : match
);
}
@@ -210,16 +211,18 @@ function stripDuplicateTitleHeading(content: string, title: string): string {
/**
* Shifts every heading down one level so the content hierarchy fits the editor's
* <h2>–<h6> range: <h2>–<h5> become <h3>–<h6>, the remaining <h1> become <h2>
- * (decoding their text to stay consistent with the title comparison), and <h6>
- * stays <h6> since there is no <h7> to shift into.
+ * (preserving attributes and decoding their text to stay consistent with the title
+ * comparison), and <h6> stays <h6> since there is no <h7> to shift into.
*/
function shiftHeadingsDown(content: string): string {
// Shift the sub-headings first so the <h1>→<h2> demotion below isn't re-shifted.
+ // `[\s\S]*?` matches headings containing inline markup; the captured attributes
+ // are carried over to the demoted <h2>.
return content
.replace(/<(\/?)h([2-5])\b([^>]*)>/gi, (_match, slash, level, rest) =>
`<${slash}h${Number(level) + 1}${rest}>`)
- .replace(/<h1[^>]*>([^<]*)<\/h1>/gi, (_match, text) =>
- `<h2>${unescapeHtml(text)}</h2>`);
+ .replace(/<h1([^>]*)>([\s\S]*?)<\/h1>/gi, (_match, attrs, text) =>
+ `<h2${attrs}>${unescapeHtml(text)}</h2>`);
}
export function extractCodeBlocks(text: string): { processedText: string; placeholderMap: Map<string, string> } {
diff --git a/packages/trilium-core/src/services/import/utils.spec.ts b/packages/trilium-core/src/services/import/utils.spec.ts
index 7959456b0c..5deafdd768 100644
--- a/packages/trilium-core/src/services/import/utils.spec.ts
+++ b/packages/trilium-core/src/services/import/utils.spec.ts
@@ -110,6 +110,16 @@ describe("#handleH1", () => {
"w/ headings deeper than <h2>, the shift should clamp at <h6>",
["<h1>A</h1><h2>B</h2><h6>C</h6>", "Title"],
"<h2>A</h2><h3>B</h3><h6>C</h6>"
+ ],
+ [
+ "w/ a content <h1> containing inline markup, it should still be demoted and shifted",
+ ["<h1>Chapter <em>One</em></h1><h2>Intro</h2>", "Doc"],
+ "<h2>Chapter <em>One</em></h2><h3>Intro</h3>"
+ ],
+ [
+ "w/ a content <h1> carrying attributes, the attributes should be preserved on the demoted <h2>",
+ [`<h1 id="top">Main</h1><h2>Sub</h2>`, "Doc"],
+ `<h2 id="top">Main</h2><h3>Sub</h3>`
]
];
diff --git a/packages/trilium-core/src/services/import/utils.ts b/packages/trilium-core/src/services/import/utils.ts
index a3bcab5b80..01f77b04d4 100644
--- a/packages/trilium-core/src/services/import/utils.ts
+++ b/packages/trilium-core/src/services/import/utils.ts
@@ -10,7 +10,7 @@ function handleH1(content: string, title: string) {
// shift every heading down one level (clamping at <h6>) to preserve nesting.
// Demoting <h1>→<h2> alone collapsed distinct <h1>/<h2> levels onto the same
// <h2>, flattening the hierarchy (see #8383).
- if (/<h1[^>]*>[^<]*<\/h1>/i.test(content)) {
+ if (/<h1[^>]*>[\s\S]*?<\/h1>/i.test(content)) {
content = shiftHeadingsDown(content);
}
@@ -19,8 +19,9 @@ function handleH1(content: string, title: string) {
/** Removes the first `<h1>` when its (decoded) text equals the note title. */
function stripDuplicateTitleHeading(content: string, title: string) {
- // No `g` flag: only the very first <h1> is a title candidate.
- return content.replace(/<h1[^>]*>([^<]*)<\/h1>/i, (match, text) =>
+ // No `g` flag: only the very first <h1> is a title candidate. `[\s\S]*?` (not
+ // `[^<]*`) so headings with inline markup or attributes still match.
+ return content.replace(/<h1[^>]*>([\s\S]*?)<\/h1>/i, (match, text) =>
unescapeHtml(text).trim() === title.trim() ? "" : match
);
}
@@ -28,16 +29,18 @@ function stripDuplicateTitleHeading(content: string, title: string) {
/**
* Shifts every heading down one level so the content hierarchy fits the editor's
* <h2>–<h6> range: <h2>–<h5> become <h3>–<h6>, the remaining <h1> become <h2>
- * (decoding their text to stay consistent with the title comparison), and <h6>
- * stays <h6> since there is no <h7> to shift into.
+ * (preserving attributes and decoding their text to stay consistent with the title
+ * comparison), and <h6> stays <h6> since there is no <h7> to shift into.
*/
function shiftHeadingsDown(content: string) {
// Shift the sub-headings first so the <h1>→<h2> demotion below isn't re-shifted.
+ // `[\s\S]*?` matches headings containing inline markup; the captured attributes
+ // are carried over to the demoted <h2>.
return content
.replace(/<(\/?)h([2-5])\b([^>]*)>/gi, (_match, slash, level, rest) =>
`<${slash}h${Number(level) + 1}${rest}>`)
- .replace(/<h1[^>]*>([^<]*)<\/h1>/gi, (_match, text) =>
- `<h2>${unescapeHtml(text)}</h2>`);
+ .replace(/<h1([^>]*)>([\s\S]*?)<\/h1>/gi, (_match, attrs, text) =>
+ `<h2${attrs}>${unescapeHtml(text)}</h2>`);
}
function extractHtmlTitle(content: string): string | null {