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
fix(share): lay out a list collection's subpages as a list
Elian Doran1ddb2117ae1f
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.tsindex 9c5e23c2c5..21444e3989 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -600,16 +600,16 @@ describe("getChildLinks", () => {});describe("getChildLinksLayout", () => {- const layout = (type: string, labels: Record<string, string> = {}) =>- getChildLinksLayout(fakeNote({ noteId: "page", type, labels }));+ const layout = (type: string, viewType: string | null = null) =>+ getChildLinksLayout(fakeNote({ noteId: "page", type }), viewType);it("shows a grid, unless a collection asks for a list", () => {expect(layout("book")).toBe("grid");- expect(layout("book", { viewType: "grid" })).toBe("grid");- expect(layout("book", { viewType: "list" })).toBe("list");- expect(layout("book", { viewType: "calendar" })).toBe("grid");+ expect(layout("book", "grid")).toBe("grid");+ expect(layout("book", "list")).toBe("list");+ expect(layout("book", "calendar")).toBe("grid");expect(layout("text")).toBe("grid");- expect(layout("text", { viewType: "list" })).toBe("grid");+ expect(layout("text", "list")).toBe("grid");});});diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 0198574c42..36c0674142 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -462,11 +462,11 @@ function toExcerpt(text: string) {}/**- * Returns how the list of subpages is laid out: a grid, unless the note is a collection whose- * `#viewType` is `list`.+ * Returns how the list of subpages is laid out: a grid, unless the note is a collection whose view+ * type is `list`. The caller resolves the view type, which can come from a template outside the share.*/-export function getChildLinksLayout(note: ShareNote): "grid" | "list" {- return note.type === "book" && note.getLabelValue("viewType") === "list" ? "list" : "grid";+export function getChildLinksLayout(note: ShareNote, viewType: string | null): "grid" | "list" {+ return note.type === "book" && viewType === "list" ? "list" : "grid";}/** What {@link getNavigationTree} needs besides the notes. */diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 341e145763..371846bb8f 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -1508,7 +1508,7 @@ describe("content_renderer", () => {prevNext: getPrevNextLinks(note, note),navigation: [],childLinks: getChildLinks(note, { sanitizeUrl: sanitize.sanitizeUrl }),- childLinksLayout: getChildLinksLayout(note),+ childLinksLayout: getChildLinksLayout(note, note.getLabelValue("viewType")),contentClasses: getContentClasses(note, isEmpty),language: { page: { lang: "en", dir: "ltr" }, content: null },lastUpdated: null,@@ -1744,6 +1744,16 @@ describe("content_renderer pages", () => {expect(textPage.querySelector("#content > h1#title") === null).toBe(false);});+ it("lists the subpages of a collection whose template makes it a list", () => {+ buildNote({ id: "_template_testList", title: "List template", "#viewType": "list" });+ const list = buildSitePage({+ id: "templateList", type: "book", content: "", "~template": "_template_testList",+ children: [ { id: "listItem", title: "Item", content: "<p>Item</p>" } ]+ });+ expect(parse(String(renderNoteContent(list))).querySelector("#childLinks")?.classList.value)+ .toStrictEqual([ "list" ]);+ });+it("counts a board's cards in the tree in place of listing them, its template hiding them", () => {buildNote({id: "_template_testBoard", title: "Board template", "#subtreeHidden": "", "#iconClass": "bx bx-columns"diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex dcb686d1b2..c11202099c 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -291,7 +291,7 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)prevNext: isFullHeight ? { previous: null, next: null } : getPrevNextLinks(note, siteRoot, isSubtreeHidden),navigation,childLinks,- childLinksLayout: getChildLinksLayout(note),+ childLinksLayout: getChildLinksLayout(note, getViewType(note)),contentClasses: getContentClasses(note, isEmpty, { isAppView, isFullHeight }),language: getPageLanguages(note, {displayLanguage,@@ -901,9 +901,13 @@ function renderMermaid(result: Result, note: SNote | BNote) {/*** The view type of a collection: its own `#viewType`, else the one of a built-in template such as- * `_template_calendar`, which lives in the hidden subtree and so is never in shaca.+ * `_template_calendar`, which lives in the hidden subtree and so is never in shaca. A becca note,+ * as the static export renders, reads its templates itself.*/-function getViewType(note: SNote) {+function getViewType(note: SNote | BNote) {+ if (note instanceof BNote) {+ return note.getLabelValue("viewType");+ }return note.getLabelValue("viewType") || note.getBuiltInTemplateLabelValue("viewType");}