Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): convert Notion inline equations to math-tex

Elian Doran58903fdd64f2
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.ts
index 966031d21b..455fa09107 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -139,3 +139,14 @@ describe("convertNotionHtml — callouts", () => {
);
});
});
+
+describe("convertNotionHtml — math", () => {
+ it("converts an inline Notion equation to a Trilium math-tex span and drops the katex style import", () => {
+ const input = `<p><style>@import url('https://cdn.jsdelivr.net/npm/[email protected]/dist/katex-swap.min.css')</style><span data-token-index="0" contenteditable="false" class="notion-text-equation-token" style="user-select:all"><span></span><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">e=mc^2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true">e=mc2</span></span></span></span></p>`;
+ expect(convertNotionHtml(input)).toBe(`<p><span class="math-tex">\\(e=mc^2\\)</span></p>`);
+ });
+
+ it("removes a stray katex style block on its own", () => {
+ expect(convertNotionHtml(`<p>Text<style>@import url('x')</style></p>`)).toBe(`<p>Text</p>`);
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index 6484b294cb..7abba0e612 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -12,6 +12,7 @@ import { HTMLElement, parse } from "node-html-parser";
export function convertNotionHtml(html: string): string {
const root = parse(html);
+ convertMath(root);
convertTodoLists(root);
convertToggles(root);
unwrapDisplayContents(root);
@@ -19,6 +20,29 @@ export function convertNotionHtml(html: string): string {
return root.toString();
}
+/**
+ * Notion renders each equation with KaTeX, preceded by a `<style>@import …katex…</style>` whose text
+ * would otherwise survive sanitization as visible junk. Drop those style blocks and replace each inline
+ * equation token (`span.notion-text-equation-token`) with Trilium's inline math span, recovering the
+ * original LaTeX from the KaTeX `<annotation encoding="application/x-tex">`. (Block/display equations are
+ * not in scope yet.)
+ */
+function convertMath(root: HTMLElement) {
+ for (const style of root.querySelectorAll("style")) {
+ style.remove();
+ }
+
+ for (const token of root.querySelectorAll("span.notion-text-equation-token")) {
+ const annotations = token.querySelectorAll("annotation");
+ const latex = (annotations.find((a) => a.getAttribute("encoding") === "application/x-tex") ?? annotations[0])?.textContent?.trim();
+ if (!latex) {
+ continue;
+ }
+ token.insertAdjacentHTML("beforebegin", `<span class="math-tex">\\(${latex}\\)</span>`);
+ token.remove();
+ }
+}
+
/** Notion's default callout icon; it maps 1:1 to a "tip" admonition, so the emoji itself is redundant. */
const DEFAULT_CALLOUT_EMOJI = "💡";