Skip to content

Build your personal knowledge base with Trilium Notes

feat(onenote): render floating images (and their captions) as figures

Elian Doran0b9ab2804570
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index bc696dfe58..7f728f080f 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -214,6 +214,26 @@ const RESOURCE_SAMPLE = `<html lang="en-US">
</body>
</html>`;
+// Real OneNote source (debug-captured): a "screen clipping" page — two standalone images, each
+// floating directly in the outline <div> and separated from its <cite> caption by OneNote's <br>
+// spacing. Left verbatim the caption renders as loose body text next to the image.
+const SCREEN_CLIPPING_SAMPLE = `<html lang="ro-RO">
+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
+ <div style="position:absolute;left:48px;top:115px;width:720px">
+ <img width="577.5" height="277.5" src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-aaa!1-BBB!sccc/$value" data-src-type="image/png" data-fullres-src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-aaa!1-BBB!sccc/$value" data-fullres-src-type="image/png" />
+ <br />
+ <cite lang="en-US" style="font-size:9pt;color:#595959;margin-top:0pt;margin-bottom:0pt">Screen clipping taken: 23.07.2026 21:40</cite>
+ <br />
+ <br />
+ <img width="385" height="509" src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-ddd!1-BBB!sccc/$value" data-src-type="image/png" data-fullres-src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-ddd!1-BBB!sccc/$value" data-fullres-src-type="image/png" />
+ <br />
+ <cite lang="en-US" style="font-size:9pt;color:#595959;margin-top:0pt;margin-bottom:0pt">Screen clipping taken: 23.07.2026 21:40</cite>
+ <br />
+ <br />
+ </div>
+ </body>
+</html>`;
+
// Real OneNote source (debug-captured): OneNote puts the list marker type on each <li> and wraps
// item text in a margin-0 <p>. The marker type belongs on the <ul>/<ol> (CKEditor's representation),
// and OneNote's lower-alpha/upper-alpha map to CKEditor's lower-latin/upper-latin.
@@ -437,6 +457,52 @@ describe("convertPageHtml", () => {
expect(out).not.toContain("0-eee"); // the full-resolution resource id is gone
});
+ it("wraps a floating image and its trailing cite caption into a figure with a figcaption", () => {
+ const out = converter.convertPageHtml(SCREEN_CLIPPING_SAMPLE);
+ const root = parse(out);
+
+ // Each standalone image becomes a CKEditor block image (<figure class="image">).
+ const figures = root.querySelectorAll("figure.image");
+ expect(figures).toHaveLength(2);
+
+ const firstImg = figures[0].querySelector("img");
+ // The display dimensions are kept and carried into an aspect-ratio (CKEditor's stored form).
+ expect(firstImg?.getAttribute("width")).toBe("577.5");
+ expect(firstImg?.getAttribute("height")).toBe("277.5");
+ expect(firstImg?.getAttribute("style")).toContain("aspect-ratio:577.5/277.5");
+ // The Graph URL is left intact for the importer to swap for a local attachment reference.
+ expect(firstImg?.getAttribute("src")).toContain("/resources/0-aaa!1-BBB!sccc/$value");
+
+ // The caption is pulled in as the figure's <figcaption>, small and wrapped in a bare <cite>.
+ const figcaption = figures[0].querySelector("figcaption");
+ expect(figcaption?.querySelector("span.text-small cite")?.textContent).toContain("Screen clipping taken: 23.07.2026 21:40");
+ // OneNote's grey caption colour is dropped so the caption inherits the theme foreground.
+ expect(figcaption?.querySelector("cite")?.getAttribute("style") ?? "").not.toContain("color");
+
+ // Both captions are absorbed into a figcaption — none left floating as body text — and the
+ // <br> spacing between image and caption is gone.
+ expect(root.querySelectorAll("cite")).toHaveLength(2);
+ expect(root.querySelectorAll("figcaption cite")).toHaveLength(2);
+ expect(root.querySelectorAll("br")).toHaveLength(0);
+ });
+
+ it("wraps a caption-less floating image in a figure carrying its aspect ratio", () => {
+ const out = converter.convertPageHtml(`<body><div><img width="480" height="441" src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-x!1-y!sz/$value" /><br /></div></body>`);
+ const figure = parse(out).querySelector("figure.image");
+
+ expect(figure).toBeTruthy();
+ expect(figure?.querySelector("figcaption")).toBeFalsy();
+ expect(figure?.querySelector("img")?.getAttribute("style")).toContain("aspect-ratio:480/441");
+ });
+
+ it("leaves an inline image (inside a paragraph) unwrapped", () => {
+ const out = converter.convertPageHtml(`<body><div><p>before <img width="16" height="16" src="https://graph.microsoft.com/v1.0/users('me')/onenote/resources/0-x!1-y!sz/$value" /> after</p></div></body>`);
+ const root = parse(out);
+
+ expect(root.querySelectorAll("figure")).toHaveLength(0);
+ expect(root.querySelector("p img")).toBeTruthy();
+ });
+
it("converts inline-style formatting (bold/italic/underline) to semantic tags", () => {
const out = converter.convertPageHtml(FORMATTING_SAMPLE);
expect(out).toContain("<strong>Bold text.</strong>");
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index b31ad3e572..8dd9047cb6 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -107,6 +107,7 @@ export function convertPageHtml(rawHtml: string): string {
sortPositionedOutlines(scope);
convertResourceReferences(scope);
+ wrapFloatingImages(scope);
convertTags(scope);
convertInlineFormatting(scope);
normalizeNamedColors(scope);
@@ -176,6 +177,70 @@ function convertResourceReferences(scope: HTMLElement) {
}
}
+/** Block contexts whose direct <img> children are standalone (block) images, not inline runs. */
+const FLOATING_IMAGE_PARENTS = new Set(["body", "div", "section", "article"]);
+
+/**
+ * OneNote lays a standalone image out as a bare <img> floating in the outline <div>, frequently
+ * trailed — across OneNote's block-level <br> spacing — by a <cite> caption (e.g. a screen clipping's
+ * "Screen clipping taken: …"). Left verbatim the caption renders as loose body text beside the image.
+ *
+ * CKEditor represents a block image as `<figure class="image">`, with any caption as its
+ * `<figcaption>`, so wrap each floating image in a figure, carry its width/height into an
+ * `aspect-ratio` (the form CKEditor stores so the image reserves space and resizes proportionally),
+ * and pull a trailing <cite> in as the caption. Inline images (inside a <p>/<a>/<span>) are left alone.
+ */
+function wrapFloatingImages(scope: HTMLElement) {
+ for (const img of scope.querySelectorAll("img")) {
+ const parent = img.parentNode;
+ if (!(parent instanceof HTMLElement) || !FLOATING_IMAGE_PARENTS.has(parent.tagName?.toLowerCase() ?? "")) {
+ continue;
+ }
+
+ const width = img.getAttribute("width");
+ const height = img.getAttribute("height");
+ const style = parseStyle(img.getAttribute("style") ?? "");
+ if (width && height && !style.has("aspect-ratio") && /^\d+(\.\d+)?$/.test(width) && /^\d+(\.\d+)?$/.test(height)) {
+ style.set("aspect-ratio", `${width}/${height}`);
+ img.setAttribute("style", serializeStyle(style));
+ }
+
+ const caption = takeTrailingCaption(img);
+ const figcaption = caption ? `<figcaption>${caption}</figcaption>` : "";
+ img.insertAdjacentHTML("beforebegin", `<figure class="image">${img.toString()}${figcaption}</figure>`);
+ img.remove();
+ }
+}
+
+/**
+ * Consumes the <cite> caption that trails a floating image (OneNote separates the two with block-level
+ * <br>s), returning the markup for the image's <figcaption> and removing the cite and the intervening
+ * breaks from the document. The caption's text is placed in a fresh <cite>, wrapped in its font-size
+ * class (e.g. text-small) — OneNote's inline caption styling (its grey chrome colour, point size and
+ * zero margins) is dropped so the caption inherits the theme foreground. Returns null when the image
+ * has no trailing cite. Runs before convertInlineFormatting so the fresh cite's content is still
+ * formatted (a caption's own bold/italic runs survive).
+ */
+function takeTrailingCaption(img: HTMLElement): string | null {
+ const breaks: HTMLElement[] = [];
+ let sibling = img.nextElementSibling;
+ while (sibling && sibling.tagName?.toLowerCase() === "br") {
+ breaks.push(sibling);
+ sibling = sibling.nextElementSibling;
+ }
+ if (!sibling || sibling.tagName?.toLowerCase() !== "cite") {
+ return null;
+ }
+
+ const sizeClass = fontSizeClass(parseStyle(sibling.getAttribute("style") ?? "").get("font-size"), sibling);
+ const cite = `<cite>${sibling.innerHTML}</cite>`;
+ const caption = sizeClass ? `<span class="${sizeClass}">${cite}</span>` : cite;
+
+ breaks.forEach((br) => br.remove());
+ sibling.remove();
+ return caption;
+}
+
/**
* OneNote represents note tags as a `data-tag` attribute on a <p> — a comma-separated list of
* `shape[:status]` values (e.g. `to-do:completed`, `important`, `movie-to-see,book-to-read`).
diff --git a/packages/trilium-core/src/services/sanitizer.spec.ts b/packages/trilium-core/src/services/sanitizer.spec.ts
index 319e0cd0aa..9b01e9f476 100644
--- a/packages/trilium-core/src/services/sanitizer.spec.ts
+++ b/packages/trilium-core/src/services/sanitizer.spec.ts
@@ -68,6 +68,15 @@ describe("sanitize", () => {
expect(sanitizeHtml(dirty)).toBe(dirty);
});
+ it("keeps a fractional image aspect-ratio (OneNote reports fractional pixel dimensions)", () => {
+ // CKEditor's usual integer ratio still passes...
+ expect(sanitizeHtml(`<img style="aspect-ratio:991/403" src="x.png" />`)).toContain("aspect-ratio:991/403");
+ // ...and a fractional ratio (e.g. a 577.5×277.5 OneNote screen clipping) is no longer stripped.
+ expect(sanitizeHtml(`<img style="aspect-ratio:577.5/277.5" src="x.png" />`)).toContain("aspect-ratio:577.5/277.5");
+ // A non-ratio value is still rejected.
+ expect(sanitizeHtml(`<img style="aspect-ratio:auto" src="x.png" />`)).not.toContain("aspect-ratio");
+ });
+
describe("bookmark anchors", () => {
it("preserves id attribute on empty <a> tags (CKEditor bookmarks)", () => {
const dirty = `<a id="my-bookmark"></a>`;
diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.ts
index 53ed10951f..5ba4adc0ab 100644
--- a/packages/trilium-core/src/services/sanitizer.ts
+++ b/packages/trilium-core/src/services/sanitizer.ts
@@ -70,7 +70,8 @@ export function sanitizeHtml(dirtyHtml: string) {
height: sizeRegex
},
img: {
- "aspect-ratio": [ /^\d+\/\d+$/ ],
+ // Allow fractional ratios too (e.g. OneNote reports 577.5×277.5 screen clippings).
+ "aspect-ratio": [ /^\d+(\.\d+)?\/\d+(\.\d+)?$/ ],
width: sizeRegex,
height: sizeRegex
},