Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/collapsible block: enable consecutive blocks to merge as a single continuous block

Adorian Dorane56bf6b98185
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.css b/apps/client/src/widgets/type_widgets/markdown/Markdown.css
index a4a683e485..eec767e4b6 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.css
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.css
@@ -15,20 +15,29 @@
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] {
- border-left: 2px solid transparent;
- padding-left: 0.5em;
- margin-left: -0.5em;
+ position: relative;
}
- [data-source-line].markdown-preview-active {
- border-left-color: var(--main-text-color);
+ .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);
+ }
+
+ /* 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 */
-.note-detail-split.split-horizontal .note-detail-split-preview > .note-detail-readonly-text-content > div {
+.note-detail-split.split-horizontal .note-detail-split-preview > .note-detail-readonly-text-content > * {
max-width: var(--preferred-max-content-width);
body.prefers-centered-content & {
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 9117239020..abe6c8d6a2 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts
@@ -12,8 +12,9 @@ describe("renderWithSourceLines", () => {
return [ ...html.matchAll(/data-source-line="(\d+)"/g) ].map((m) => parseInt(m[1], 10));
}
+ /** Markup with the source-line attributes stripped, so markup assertions stay readable. */
function html(src: string): string {
- return renderWithSourceLines(src).html;
+ return renderWithSourceLines(src).html.replace(/ data-source-line="\d+"/g, "");
}
it("returns empty html for empty input", () => {
@@ -51,13 +52,34 @@ describe("renderWithSourceLines", () => {
expect(extractLines(src)).toEqual([ 1, 6 ]);
});
- it("renders standard markdown constructs inside the wrappers", () => {
+ it("renders standard markdown constructs", () => {
const result = html("## Heading\n\n- item\n");
expect(result).toContain("<h2>Heading</h2>");
expect(result).toContain("<ul>");
expect(result).toContain("<li>item</li>");
});
+ it("tags blocks in place so they stay direct siblings", () => {
+ // The .ck-content styles use sibling combinators (e.g. the stacked look of consecutive
+ // collapsibles), which only match if blocks are not each wrapped in a container.
+ const src = [
+ '<details class="trilium-collapsible">',
+ "<summary>One</summary>",
+ "</details>",
+ "",
+ '<details class="trilium-collapsible">',
+ "<summary>Two</summary>",
+ "</details>"
+ ].join("\n");
+
+ const container = document.createElement("div");
+ container.innerHTML = renderWithSourceLines(src).html;
+
+ const blocks = Array.from(container.children);
+ expect(blocks.map((block) => block.tagName)).toEqual([ "DETAILS", "DETAILS" ]);
+ expect(blocks.every((block) => block.hasAttribute("data-source-line"))).toBe(true);
+ });
+
it("keeps H1 as H1 in the preview (no title-row context to avoid)", () => {
expect(html("# Top level")).toContain("<h1>Top level</h1>");
});
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
index bcaf584293..2f37e647dd 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
@@ -562,12 +562,13 @@ export function renderWithSourceLines(src: string): { html: string; headings: Ma
const container = document.createElement("div");
container.innerHTML = html;
- const parts: string[] = [];
+ // The line goes on the block itself rather than on a wrapper, so blocks stay direct
+ // siblings and the .ck-content sibling rules (e.g. collapsible runs) keep matching.
const children = Array.from(container.children);
for (let i = 0; i < children.length; i++) {
const sourceLine = lines[i] ?? lines[lines.length - 1] ?? 1;
- parts.push(`<div data-source-line="${sourceLine}">${children[i].outerHTML}</div>`);
+ children[i].setAttribute("data-source-line", String(sourceLine));
}
- return { html: parts.join(""), headings };
+ return { html: container.innerHTML, headings };
}
//#endregion