Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): keep the admonition icon when its block is active

Elian Doran55444c6505c8
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.css b/apps/client/src/widgets/type_widgets/markdown/Markdown.css
index eec767e4b6..de5aa72eb7 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.css
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.css
@@ -13,27 +13,17 @@
overflow: auto;
padding: 0.5em 1em;
user-select: text;
- }
-
- /* The marker is drawn as an overlay so it doesn't disturb each block's own box. */
- .markdown-preview [data-source-line] {
- position: relative;
- }
- .markdown-preview [data-source-line].markdown-preview-active::before {
- content: "";
- position: absolute;
- inset-block: 0;
- left: -0.5em;
- width: 2px;
- background: var(--main-text-color);
+ /* The active-block marker is the preview's own background rather than a pseudo-element
+ * on the block: `.admonition` already draws its icon in `::before`, and a block that
+ * sets `overflow: hidden` clips anything drawn in its gutter. `local` attachment
+ * scrolls it with the content; `useSyncedHighlight` writes the custom properties. */
+ background-image: linear-gradient(var(--main-text-color), var(--main-text-color));
+ background-attachment: local;
+ background-repeat: no-repeat;
+ background-position: 0.5em var(--markdown-preview-marker-top, 0);
+ background-size: 2px var(--markdown-preview-marker-height, 0);
}
-
- /* Collapsibles clip their overflow, so the marker has to sit inside the block. */
- .markdown-preview details.trilium-collapsible.markdown-preview-active::before {
- left: 0;
- }
-
}
/* Markdown preview content blocks in split-view */
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 fd7ad93370..ffe42e8658 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
@@ -4,7 +4,7 @@
// sanitization-dependent specs under jsdom, which matches real-browser behavior.
import { describe, expect, it } from "vitest";
-import { renderWithSourceLines } from "./Markdown.js";
+import { findActiveBlock, renderWithSourceLines } from "./Markdown.js";
describe("renderWithSourceLines", () => {
function extractLines(src: string): number[] {
@@ -300,3 +300,30 @@ describe("renderWithSourceLines", () => {
.toBe(`<p>a <span style="color:#ff0000;"><strong>red</strong></span> b</p>`);
});
});
+
+describe("findActiveBlock", () => {
+ /** The preview's blocks, tagged with the source lines they start at. */
+ function blocks(...sourceLines: number[]) {
+ return sourceLines.map((line) => {
+ const el = document.createElement("p");
+ el.dataset.sourceLine = String(line);
+ return el;
+ });
+ }
+
+ it("picks the block the cursor sits in, and the last one past the end", () => {
+ const [ first, second, third ] = blocks(1, 5, 9);
+ const all = [ first, second, third ];
+
+ expect(findActiveBlock(all, 1)).toBe(first);
+ expect(findActiveBlock(all, 4)).toBe(first);
+ expect(findActiveBlock(all, 5)).toBe(second);
+ expect(findActiveBlock(all, 9)).toBe(third);
+ expect(findActiveBlock(all, 100)).toBe(third);
+ });
+
+ it("matches nothing above the first block or in an empty preview", () => {
+ expect(findActiveBlock(blocks(3), 1)).toBeNull();
+ expect(findActiveBlock([], 1)).toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
index 0cc8c6f03c..95dc3625ee 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
@@ -270,41 +270,54 @@ function useSyncedScrolling(view: VanillaCodeMirror | null, preview: HTMLDivElem
}
/**
- * Highlights the preview block that corresponds to the editor's active line,
- * matching the built-in `cm-activeLine` behavior. Re-runs when the rendered
- * HTML changes so newly inserted blocks pick up the current cursor position.
+ * Marks the preview block that corresponds to the editor's active line, matching the built-in
+ * `cm-activeLine` behavior. Re-runs when the rendered HTML changes so newly inserted blocks pick
+ * up the current cursor position.
+ *
+ * The marker is painted as the preview's background (see `Markdown.css`), so its geometry has to
+ * be measured here. Every block is observed for resize, since a block that grows after render —
+ * a mermaid diagram, an image, an included note — shifts everything below it.
*/
function useSyncedHighlight(view: VanillaCodeMirror | null, preview: HTMLDivElement | null, html: string) {
useEffect(() => {
if (!view || !preview) return;
- let current: HTMLElement | null = null;
-
function update() {
if (!view || !preview) return;
const activeLine = view.state.doc.lineAt(view.state.selection.main.head).number;
-
const blocks = preview.querySelectorAll<HTMLElement>("[data-source-line]");
- let match: HTMLElement | null = null;
- for (const el of blocks) {
- if (parseInt(el.dataset.sourceLine!, 10) <= activeLine) match = el;
- else break;
- }
+ const match = findActiveBlock(blocks, activeLine);
- if (match === current) return;
- current?.classList.remove("markdown-preview-active");
- match?.classList.add("markdown-preview-active");
- current = match;
+ // `offsetTop` and the marker's containing block are both the preview's padding box.
+ preview.style.setProperty("--markdown-preview-marker-top", `${match?.offsetTop ?? 0}px`);
+ preview.style.setProperty("--markdown-preview-marker-height", `${match?.offsetHeight ?? 0}px`);
}
update();
+ const observer = new ResizeObserver(update);
+ observer.observe(preview);
+ for (const block of preview.children) observer.observe(block);
+
const unsubscribe = view.addUpdateListener((v) => {
if (v.selectionSet || v.docChanged) update();
});
- return unsubscribe;
+ return () => {
+ observer.disconnect();
+ unsubscribe();
+ };
}, [ view, preview, html ]);
}
+/** The last block that starts at or before `activeLine`, i.e. the one the cursor sits in. */
+export function findActiveBlock(blocks: ArrayLike<HTMLElement>, activeLine: number) {
+ let match: HTMLElement | null = null;
+ for (const block of Array.from(blocks)) {
+ if (Number(block.dataset.sourceLine) <= activeLine) match = block;
+ else break;
+ }
+ return match;
+}
+
//#region Text commands
/**
* Handles text-detail commands for the Markdown editor: