Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): convert Notion block equations to display math

Elian Doran74ef2b7cdf62
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 9b4cf92ebf..e0350b8041 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -154,6 +154,11 @@ describe("convertNotionHtml — math", () => {
expect(convertNotionHtml(input)).toBe(`<p><span class="math-tex">\\(e=mc^2\\)</span></p>`);
});
+ it("converts a block Notion equation to a Trilium display math-tex figure and drops the katex style import", () => {
+ const input = `<figure id="386c5eca-1b8b-8032-9922-d271e443683f" class="equation"><style>@import url('https://cdn.jsdelivr.net/npm/[email protected]/dist/katex-swap.min.css')</style><div class="equation-container"><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><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></div></figure>`;
+ expect(convertNotionHtml(input)).toBe(`<figure><span class="math-tex">\\[e=mc^2\\]</span></figure>`);
+ });
+
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 83764bd39c..076d2c0914 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -31,11 +31,12 @@ export function convertNotionHtml(html: string): string {
// #region Math
/**
- * 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.)
+ * Notion renders each equation with KaTeX, preceded (or, for block equations, wrapped) by a
+ * `<style>@import …katex…</style>` whose text would otherwise survive sanitization as visible junk. Drop
+ * those style blocks, then recover the original LaTeX from each equation's KaTeX
+ * `<annotation encoding="application/x-tex">` and emit Trilium's math markup:
+ * - inline equations (`span.notion-text-equation-token`) → inline math span, `\(…\)`;
+ * - block equations (`figure.equation`) → a `<figure>` wrapping a display math span, `\[…\]`.
*/
function convertMath(root: HTMLElement) {
for (const style of root.querySelectorAll("style")) {
@@ -43,14 +44,28 @@ function convertMath(root: HTMLElement) {
}
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();
+ const latex = extractLatex(token);
if (!latex) {
continue;
}
token.insertAdjacentHTML("beforebegin", `<span class="math-tex">\\(${latex}\\)</span>`);
token.remove();
}
+
+ for (const figure of root.querySelectorAll("figure.equation")) {
+ const latex = extractLatex(figure);
+ if (!latex) {
+ continue;
+ }
+ figure.insertAdjacentHTML("beforebegin", `<figure><span class="math-tex">\\[${latex}\\]</span></figure>`);
+ figure.remove();
+ }
+}
+
+/** Recovers the original LaTeX from a KaTeX subtree via its `<annotation encoding="application/x-tex">`. */
+function extractLatex(el: HTMLElement): string | undefined {
+ const annotations = el.querySelectorAll("annotation");
+ return (annotations.find((a) => a.getAttribute("encoding") === "application/x-tex") ?? annotations[0])?.textContent?.trim();
}
// #endregion