Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): don't alter h1 and h2

Elian Dorandee9959d5b40
diff --git a/apps/client/src/stylesheets/theme-next/notes/text.css b/apps/client/src/stylesheets/theme-next/notes/text.css
index 252a3da6b2..5e58895ef5 100644
--- a/apps/client/src/stylesheets/theme-next/notes/text.css
+++ b/apps/client/src/stylesheets/theme-next/notes/text.css
@@ -635,6 +635,7 @@ html .note-detail-editable-text :not(figure, .include-note, hr):first-child {
inset-inline-end: 0.35em;
}
+.ck-content h1,
.ck-content h2,
.ck-content h3,
.ck-content h4,
@@ -694,7 +695,7 @@ html .note-detail-editable-text :not(figure, .include-note, hr):first-child {
outline: none;
box-shadow: 0 0 0 2px var(--link-selection-outline-color);
background: var(--link-hover-background);
-}
+}
/* Reference link */
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
index 61cf206417..0addb5037a 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts
@@ -49,6 +49,11 @@ describe("renderWithSourceLines", () => {
expect(html).toContain("<li>item</li>");
});
+ it("keeps H1 as H1 in the preview (no title-row context to avoid)", () => {
+ const html = renderWithSourceLines("# Top level");
+ expect(html).toContain("<h1>Top level</h1>");
+ });
+
it("preserves reference-style links across per-block parsing", () => {
const src = [
"[trilium][t]", // 1
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index ace105e7a5..dec85ae6e1 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -151,7 +151,8 @@ export function renderWithSourceLines(src: string): string {
const html = renderToHtml(src, "", {
sanitize: (h) => DOMPurify.sanitize(h),
- wikiLink: { formatHref: (id) => `#root/${id}` }
+ wikiLink: { formatHref: (id) => `#root/${id}` },
+ demoteH1: false
});
if (!html) return "";
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index 453c39d420..d6d3f38a99 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -40,6 +40,14 @@ export interface RenderToHtmlOptions {
wikiLink?: WikiLinkOptions;
/** Same as {@link wikiLink}, for `![[noteId]]` transclusions. */
transclusion?: TransclusionOptions;
+ /**
+ * If `true` (default), strip the first `<h1>` that matches {@link title}
+ * and demote any remaining `<h1>` to `<h2>` — notes render the title as a
+ * separate H1 above the content, so double-H1 would otherwise result.
+ * Set to `false` when there's no surrounding title (e.g. a live editor
+ * preview) so authored H1s are shown as-is.
+ */
+ demoteH1?: boolean;
}
function escapeHtml(str: string): string {
@@ -281,7 +289,9 @@ export function renderToHtml(content: string, title: string, options: RenderToHt
html = restoreFromMap(html, formulaMap);
// h1 handling needs to come before sanitization.
- html = handleH1(html, title);
+ if (options.demoteH1 !== false) {
+ html = handleH1(html, title);
+ }
html = options.sanitize(html);
// Add a trailing semicolon to CSS styles.