Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): support ==highlight== outside of Obsidian imports

Elian Dorana172cabe5f3e
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
index abe6c8d6a2..bb1d8d0221 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
@@ -245,4 +245,9 @@ describe("renderWithSourceLines", () => {
expect(h.text).not.toMatch(/javascript:/i);
}
});
+
+ it("renders ==text== as a highlight, surviving sanitization", () => {
+ expect(html("==hi==")).toBe('<p><span style="background-color:hsl(60, 75%, 60%);">hi</span></p>');
+ expect(html("a == b")).toBe("<p>a == b</p>");
+ });
});
diff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.ts
index 5eb0774ad3..42306e0f0f 100644
--- a/packages/commons/src/lib/markdown_renderer.spec.ts
+++ b/packages/commons/src/lib/markdown_renderer.spec.ts
@@ -563,29 +563,34 @@ describe("renderToHtml", () => {
});
});
- describe("Obsidian syntax (obsidian option)", () => {
- it("renders ==text== as a background-coloured span only when the obsidian flag is set", () => {
- expect(render("==hi==", "", { obsidian: true }))
- .toBe('<p><span style="background-color:hsl(60, 75%, 60%);">hi</span></p>');
- // Off by default so generic Markdown is untouched.
- expect(render("==hi==")).toBe("<p>==hi==</p>");
+ describe("highlights (==text==)", () => {
+ const HL = '<span style="background-color:hsl(60, 75%, 60%);">';
+
+ it("renders ==text== as a background-coloured span without any flag", () => {
+ expect(render("==hi==")).toBe(`<p>${HL}hi</span></p>`);
+ expect(render("==hi==", "", { obsidian: true })).toBe(`<p>${HL}hi</span></p>`);
});
it("parses inner markdown inside a highlight", () => {
- expect(render("==**bold**==", "", { obsidian: true }))
- .toBe('<p><span style="background-color:hsl(60, 75%, 60%);"><strong>bold</strong></span></p>');
+ expect(render("==**bold**==")).toBe(`<p>${HL}<strong>bold</strong></span></p>`);
});
it("leaves ==== and spaced == as literal text", () => {
- expect(render("====", "", { obsidian: true })).toBe("<p>====</p>");
- expect(render("a == b", "", { obsidian: true })).toBe("<p>a == b</p>");
+ expect(render("====")).toBe("<p>====</p>");
+ expect(render("a == b")).toBe("<p>a == b</p>");
});
- it("does not highlight == inside inline code", () => {
- expect(render("`==x==`", "", { obsidian: true }))
- .toBe('<p><code spellcheck="false">==x==</code></p>');
+ it("does not highlight == inside code", () => {
+ expect(render("`==x==`")).toBe('<p><code spellcheck="false">==x==</code></p>');
+ expect(render("```\na ==x== b\n```")).toContain("a ==x== b");
});
+ it("leaves a setext heading underline alone", () => {
+ expect(render("Title\n===", "", { demoteH1: false })).toBe("<h1>Title</h1>");
+ });
+ });
+
+ describe("Obsidian syntax (obsidian option)", () => {
it("turns %% comment %% into an HTML comment only when the obsidian flag is set", () => {
expect(render("a %%secret%% b", "", { obsidian: true })).toBe("<p>a <!-- secret --> b</p>");
// Off by default so generic Markdown is untouched.
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index e1ad43a64b..ccb2dcb057 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -183,10 +183,12 @@ export interface RenderToHtmlOptions {
*/
taskStates?: TaskStateDef[];
/**
- * Enable Obsidian-specific Markdown syntax: `==highlight==` → `<mark>` and
- * `%% comment %%` → an HTML comment. Off by default so generic Markdown import,
- * paste and the live editor preview are unaffected — only the Obsidian importer
- * opts in.
+ * Enable Obsidian-specific Markdown syntax: `%% comment %%` → an HTML comment,
+ * plus Obsidian callouts. Off by default so generic Markdown import, paste and
+ * the live editor preview are unaffected — only the Obsidian importer opts in.
+ *
+ * Note that `==highlight==` is *not* gated on this: it is supported everywhere,
+ * since it is the de-facto standard highlight syntax outside of GFM.
*/
obsidian?: boolean;
}
@@ -580,10 +582,11 @@ export function renderToHtml(content: string, title: string, options: RenderToHt
// Order is important, especially for wikilinks.
const extensions = [
options.transclusion ? createTransclusionExtension(options.transclusion) : transclusionExtension,
- options.wikiLink ? createWikiLinkExtension(options.wikiLink) : wikiLinkExtension
+ options.wikiLink ? createWikiLinkExtension(options.wikiLink) : wikiLinkExtension,
+ createHighlightExtension()
];
if (options.obsidian) {
- extensions.push(createHighlightExtension(), createCommentExtension());
+ extensions.push(createCommentExtension());
}
marked.use({ extensions });
diff --git a/packages/commons/src/lib/marked_extensions.ts b/packages/commons/src/lib/marked_extensions.ts
index be096692c6..4cc054f92b 100644
--- a/packages/commons/src/lib/marked_extensions.ts
+++ b/packages/commons/src/lib/marked_extensions.ts
@@ -101,16 +101,20 @@ export function createTransclusionExtension(options: TransclusionOptions = {}):
}
/**
- * Background colour for imported highlights: CKEditor's default yellow highlight marker
- * (Obsidian highlights carry no colour of their own). Matches the `<span
- * style="background-color:…">` markup CKEditor's Font Background Color feature emits, so the
- * imported highlight round-trips as an editable highlight.
+ * Background colour for highlights: CKEditor's default yellow highlight marker (`==…==`
+ * carries no colour of its own). Matches the `<span style="background-color:…">` markup
+ * CKEditor's Font Background Color feature emits, so a highlight rendered into a text note
+ * round-trips as an editable highlight. Deliberately not `<mark>`: the editor has no
+ * Highlight plugin, so it would drop the element on the next edit.
*/
const HIGHLIGHT_BACKGROUND = "hsl(60, 75%, 60%)";
/**
- * Creates an extension for Obsidian-style highlights: `==text==` → a background-coloured
- * `<span>`.
+ * Creates an extension for highlights: `==text==` → a background-coloured `<span>`.
+ *
+ * Not part of CommonMark or GFM, but the de-facto standard everywhere else (Obsidian,
+ * Typora, Bear, python-markdown), so it is enabled for all Markdown rendering rather than
+ * only for the Obsidian importer.
*
* Inner markdown is parsed so `==**bold**==` highlights bold text. A non-space is required
* just inside each `==` (like emphasis), so `a == b` and `====` stay literal.
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.ts b/packages/trilium-core/src/services/import/obsidian/importer.ts
index 6e147808c9..68d1f241a2 100644
--- a/packages/trilium-core/src/services/import/obsidian/importer.ts
+++ b/packages/trilium-core/src/services/import/obsidian/importer.ts
@@ -20,7 +20,8 @@
* root either way, so the redundant wrapper folder is stripped and the import root is named after the vault.
*
* Obsidian-specific inline syntax is handled during Markdown rendering (gated by the `obsidian` flag):
- * `==highlight==` becomes `<mark>` and `%% comment %%` becomes an HTML comment.
+ * `%% comment %%` becomes an HTML comment, and callouts become admonitions. `==highlight==` needs no
+ * flag — it is supported for all Markdown rendering.
*
* Front matter (parsed by {@link ../frontmatter.js}) becomes labels via {@link mapObsidianFrontmatter}: tags
* become individual labels, aliases become `#alias` labels, cssclasses/publish/permalink are dropped, and