Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(share): show more of a subpage's text in its card, by paragraph
Elian Doran3132d25e6695
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.htmlindex 8c3b655c17..dda6fc6728 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.html@@ -100,8 +100,9 @@</td><td>The links to the note's visible children, each with <code>title</code>, <code>href</code>, <code>isExternal</code>, <code>type</code>, <code>icon</code> (itsicon classes), <code>excerpt</code> (its <code>#shareDescription</code> or- the start of its text, or <code>null</code>) and <code>children</code> (when- it has no excerpt, its first ten visible children, each with <code>title</code>, <code>href</code>, <code>isExternal</code> and <code>icon</code>).+ the start of its text, with a line break between paragraphs, or <code>null</code>)+ and <code>children</code> (when it has no excerpt, its first ten visible+ children, each with <code>title</code>, <code>href</code>, <code>isExternal</code> and <code>icon</code>).Empty for a note that is not a collection, a text note or a code note,and with <code>#hideChildrenOverview</code>.</td></tr>diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md b/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.mdindex 34aae3fbef..97473ecac8 100644--- a/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing/Custom share template.md@@ -34,7 +34,7 @@ Your template is rendered with a context object exposing the note and its render| `logo` | The site logo above the navigation tree and, on a narrow screen, in the header: `href` (`#shareRootLink`, otherwise the site's root page), `image` (the `~shareLogo` image, or `null`), `icon` (the icon classes of the site's root note, shown when there is no `image`), and `width` and `height` for the image. || `navigation` | The navigation tree of the site: its visible pages, each with `title`, `href`, `isExternal`, `type`, `icon`, `isActive` (the page being shown), `isExpanded` (that page or one of its ancestors) and `children`. || `contentClasses` | The classes of the content element: `type-<note type>`, `ck-content` for text and Markdown notes, and `no-content` when the note has no content of its own. |-| `childLinks` | The links to the note's visible children, each with `title`, `href`, `isExternal`, `type`, `icon` (its icon classes), `excerpt` (its `#shareDescription` or the start of its text, or `null`) and `children` (when it has no excerpt, its first ten visible children, each with `title`, `href`, `isExternal` and `icon`). Empty for a note that is not a collection, a text note or a code note, and with `#hideChildrenOverview`. |+| `childLinks` | The links to the note's visible children, each with `title`, `href`, `isExternal`, `type`, `icon` (its icon classes), `excerpt` (its `#shareDescription` or the start of its text, with a line break between paragraphs, or `null`) and `children` (when it has no excerpt, its first ten visible children, each with `title`, `href`, `isExternal` and `icon`). Empty for a note that is not a collection, a text note or a code note, and with `#hideChildrenOverview`. || `childLinksLayout` | How the default template lays `childLinks` out: `list` for a collection whose view type is list, `grid` otherwise. || `prevNext` | The pages before and after the note in the navigation tree, as `previous` and `next`, each with `title` and `href`, or `null`. || `language` | The languages of the page: `page` with the `lang` and `dir` of the application's language, and `content` with those of the note's content language, or `null` when they are the same. |diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.tsindex c292e00ad7..e152705e90 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -500,15 +500,15 @@ describe("getChildLinks", () => {it("describes a child by #shareDescription, else by the start of its text", () => {const parent = fakeNote({ noteId: "parent" });- const sentences = "First sentence of the note, which says what it is about. "- + "A second one goes on about it at length, well past where a summary should stop. "- + "A third one is there only to make the whole too long for a summary.";+ const sentence = "Sentence number one is here. ";const texts: Record<string, string | null> = {described: "Ignored",short: " A short\n note. ",- sentences,- words: "word ".repeat(60),- unbroken: "x".repeat(200),+ paragraphs: "First paragraph.\n\n \n\nSecond\n paragraph.",+ sentences: sentence.repeat(20),+ paragraphEnd: `${"word ".repeat(40)}\n\n${"word ".repeat(150)}`,+ words: "word ".repeat(150),+ unbroken: "x".repeat(600),empty: " ",unreadable: null};@@ -525,15 +525,16 @@ describe("getChildLinks", () => {expect(excerpts).toStrictEqual(["About it","A short note.",- "First sentence of the note, which says what it is about. "- + "A second one goes on about it at length, well past where a summary should stop.",- `${"word ".repeat(32).trim()}…`,- `${"x".repeat(160)}…`,+ "First paragraph.\nSecond paragraph.",+ sentence.repeat(17).trim(),+ "word ".repeat(40).trim(),+ `${"word ".repeat(100).trim()}…`,+ `${"x".repeat(500)}…`,null,null]);expect(getChildLinks(parent, options).map((link) => link.excerpt))- .toStrictEqual([ "About it", null, null, null, null, null, null ]);+ .toStrictEqual([ "About it", null, null, null, null, null, null, null, null ]);const denied = getChildLinks(parent, {...options,@@ -541,8 +542,7 @@ describe("getChildLinks", () => {canAccess: (note) => note.noteId !== "described" && note.noteId !== "short"});expect(denied.map((link) => link.excerpt).slice(0, 3))- .toStrictEqual([ null, null, "First sentence of the note, which says what it is about. "- + "A second one goes on about it at length, well past where a summary should stop." ]);+ .toStrictEqual([ null, null, "First paragraph.\nSecond paragraph." ]);});});diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 552972a87a..b0a5a66261 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -424,23 +424,27 @@ export function getChildLinks(note: ShareNote, options: ChildLinksOptions): Chil});}-/** The most characters of a child's text its excerpt shows. */-const EXCERPT_LENGTH = 160;+/** The most characters of a child's text its excerpt shows, about what fills its card's preview. */+const EXCERPT_LENGTH = 500;/**- * Shortens `text` to an excerpt of at most {@link EXCERPT_LENGTH} characters: whole sentences when- * they fill at least a third of it, else whole words followed by an ellipsis.+ * Shortens `text`, whose paragraphs are separated by blank lines, to an excerpt of at most+ * {@link EXCERPT_LENGTH} characters with a line break between paragraphs: whole sentences or+ * paragraphs when they fill at least a third of it, else whole words followed by an ellipsis.*/function toExcerpt(text: string) {- const plain = text.replace(/\s+/g, " ").trim();+ const plain = text.split(/\n\s*\n/)+ .map((paragraph) => paragraph.replace(/\s+/g, " ").trim())+ .filter(Boolean)+ .join("\n");if (plain.length <= EXCERPT_LENGTH) {return plain || null;}const head = plain.slice(0, EXCERPT_LENGTH);- const sentenceEnd = Math.max(head.lastIndexOf(". "), head.lastIndexOf("! "), head.lastIndexOf("? "));- if (sentenceEnd >= EXCERPT_LENGTH / 3) {- return head.slice(0, sentenceEnd + 1);+ const end = [ ...head.matchAll(/[.!?](?=\s)|[^\n](?=\n)/g) ].at(-1);+ if (end && end.index >= EXCERPT_LENGTH / 3) {+ return head.slice(0, end.index + 1);}const wordEnd = head.lastIndexOf(" ");diff --git a/packages/share-theme/src/page/child_links.css b/packages/share-theme/src/page/child_links.cssindex 69606b169c..3f4f24653a 100644--- a/packages/share-theme/src/page/child_links.css+++ b/packages/share-theme/src/page/child_links.css@@ -65,6 +65,10 @@font-size: 0.8rem;}+.child-link-excerpt {+ white-space: pre-line;+}+.child-link-children {display: flex;flex-direction: column;@@ -105,6 +109,14 @@margin-top: 0.25rem;}+#childLinks.list .child-link-excerpt {+ display: -webkit-box;+ overflow: hidden;+ -webkit-box-orient: vertical;+ -webkit-line-clamp: 2;+ line-clamp: 2;+}+#childLinks.grid > ul {display: flex;flex-wrap: wrap;diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex d64f3382b6..6bdefe891f 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -559,7 +559,7 @@ describe("content_renderer", () => {expect(page.querySelector("#childLinks")?.classList.contains("grid")).toBe(true);expect(items).toStrictEqual([- { icon: true, excerpt: "First & bold. Second.", children: [] },+ { icon: true, excerpt: "First & bold.\nSecond.", children: [] },{icon: false,excerpt: undefined,diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex a11ef869cb..c986146c24 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -408,9 +408,9 @@ function getShareAssetPath() {}/**- * Returns the text of the paragraphs of a text note, which the excerpt of its entry in its parent's- * list of subpages starts from, or `null` for a note of another type or a protected one. Only the- * start of the note is parsed, enough for any excerpt.+ * Returns the text of the paragraphs of a text note, separated by blank lines, which the excerpt+ * of its entry in its parent's list of subpages starts from, or `null` for a note of another type+ * or a protected one. Only the start of the note is parsed, enough for any excerpt.*/function getExcerptSource(note: SNote | BNote) {if (note.type !== "text" || note.isProtected) {@@ -424,7 +424,7 @@ function getExcerptSource(note: SNote | BNote) {return parse(content.slice(0, EXCERPT_SOURCE_LENGTH)).querySelectorAll("p").map((paragraph) => paragraph.text)- .join(" ");+ .join("\n\n");}/** How much of a text note's HTML {@link getExcerptSource} parses. */