Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): show a collection with its template's icon

Elian Doran51bfa43e49d2
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index cc61c7f8c8..341e145763 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1716,7 +1716,11 @@ describe("content_renderer pages", () => {
expect(relationMap.isAppView).toBe(true);
expect(parse(String(relationMap.content))
.querySelector(".share-note-view[data-note-id=relationMapNote]")?.innerHTML).toBe("");
- expect(render({ type: "render", content: "" }).content)
+ const renderNote = render({ id: "renderNote", type: "render", content: "" });
+ expect(renderNote.isAppView).toBe(true);
+ expect(parse(String(renderNote.content))
+ .querySelector(".share-note-view[data-note-id=renderNote]")?.innerHTML).toBe("");
+ expect(render({ type: "launcher", content: "" }).content)
.toBe(`<p>${t("content_renderer.note-cannot-be-displayed")}</p>`);
});
@@ -1741,7 +1745,9 @@ describe("content_renderer pages", () => {
});
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": "" });
+ buildNote({
+ id: "_template_testBoard", title: "Board template", "#subtreeHidden": "", "#iconClass": "bx bx-columns"
+ });
const board = buildSitePage({
id: "hidingBoard",
title: "Board",
@@ -1758,19 +1764,22 @@ describe("content_renderer pages", () => {
expect(entry?.querySelector(".tree-item-hidden-count")?.getAttribute("title"))
.toBe("2 subpages that are hidden from the tree");
expect(entry?.querySelector("li") === null).toBe(true);
+ expect(entry?.querySelector(".tree-item-row a > .tn-icon")?.classList.contains("bx-columns")).toBe(true);
const card = parse(String(renderNoteContent(shaca.getNote("boardCard1"))));
expect(card.querySelectorAll("#menu li[data-note-id=hidingBoard] li").map((item) =>
item.getAttribute("data-note-id"))).toEqual([ "boardCard1" ]);
const shown = buildSitePage({
- id: "showingBoard", content: "<p>Board</p>", "#subtreeHidden": "false",
+ id: "showingBoard", content: "<p>Board</p>", "#subtreeHidden": "false", "#iconClass": "bx bx-rocket",
"~template": "_template_testBoard",
children: [ { id: "shownCard", title: "Card", content: "<p>Card</p>" } ]
});
const shownPage = parse(String(renderNoteContent(shown)));
expect(shownPage.querySelector("#menu .tree-item-hidden-count") === null).toBe(true);
expect(shownPage.querySelector("#menu li[data-note-id=shownCard]") === null).toBe(false);
+ expect(shownPage.querySelector("#menu li[data-note-id=showingBoard] > .tree-item-row a > .tn-icon")
+ ?.classList.contains("bx-rocket")).toBe(true);
});
it("leaves content that is not text as it is, and empty text as empty", () => {
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index c91e2f7e46..aa7e843af5 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -904,20 +904,7 @@ function renderMermaid(result: Result, note: SNote | BNote) {
* `_template_calendar`, which lives in the hidden subtree and so is never in shaca.
*/
function getViewType(note: SNote) {
- const viewType = note.getLabelValue("viewType");
- if (viewType) {
- return viewType;
- }
-
- for (const relation of note.getOwnedRelations("template")) {
- if (relation.value.startsWith("_template_")) {
- const templateViewType = becca.getNote(relation.value)?.getLabelValue("viewType");
- if (templateViewType) {
- return templateViewType;
- }
- }
- }
- return null;
+ return note.getLabelValue("viewType") || note.getBuiltInTemplateLabelValue("viewType");
}
/**
@@ -930,8 +917,8 @@ function isSubtreeHidden(note: ShareNote) {
return shared.isLabelTruthy("subtreeHidden");
}
- return shared.getOwnedRelations("template").some((relation) => relation.value.startsWith("_template_")
- && !!becca.getNote(relation.value)?.isLabelTruthy("subtreeHidden"));
+ const value = shared.getBuiltInTemplateLabelValue("subtreeHidden");
+ return value !== null && value !== "false";
}
/**
diff --git a/packages/trilium-core/src/share/shaca/entities/snote.ts b/packages/trilium-core/src/share/shaca/entities/snote.ts
index 36ece31afd..b3dba859ac 100644
--- a/packages/trilium-core/src/share/shaca/entities/snote.ts
+++ b/packages/trilium-core/src/share/shaca/entities/snote.ts
@@ -2,6 +2,8 @@ import { BlobRow, getNoteIcon, NoteType } from "@triliumnext/commons";
import { decodeUtf8 } from "../../../services/utils/binary.js";
import escape from "escape-html";
+import becca from "../../../becca/becca.js";
+
import { isStringNote } from "../../../services/utils/index.js";
import sql from "../../sql.js";
import AbstractShacaEntity from "./abstract_shaca_entity.js";
@@ -515,18 +517,17 @@ class SNote extends AbstractShacaEntity {
}
getIcon(filterByPrefix: string[] = []) {
- const iconClassLabels = this.getLabels("iconClass").filter(label => {
- if (filterByPrefix.length === 0) {
- return true;
- }
- return filterByPrefix.some(prefix => label.value.startsWith(prefix));
- });
+ const isAvailable = (iconClass: string) =>
+ filterByPrefix.length === 0 || filterByPrefix.some((prefix) => iconClass.startsWith(prefix));
+ const templateIconClass = this.getBuiltInTemplateLabelValue("iconClass");
+ const iconClass = this.getLabels("iconClass").map((label) => label.value).find(isAvailable)
+ ?? (templateIconClass && isAvailable(templateIconClass) ? templateIconClass : undefined);
const icon = getNoteIcon({
noteId: this.noteId,
type: this.type,
mime: this.mime,
workspaceIconClass: undefined,
- iconClass: iconClassLabels.length > 0 ? iconClassLabels[0].value : undefined,
+ iconClass,
isFolder: this.isFolder.bind(this),
getLabelValue: this.getLabelValue.bind(this)
});
@@ -537,6 +538,23 @@ class SNote extends AbstractShacaEntity {
isFolder() {
return this.getChildBranches().length > 0;
}
+
+ /**
+ * Returns the value of the label on the first built-in template (`_template_*`) the note names
+ * with `~template` that has one, or `null`. Shaca holds only shared notes, so the label the app
+ * reads from such a template, such as a collection's icon, is read from becca.
+ */
+ getBuiltInTemplateLabelValue(name: string): string | null {
+ for (const relation of this.getOwnedRelations("template")) {
+ const value = relation.value.startsWith("_template_")
+ ? becca.getNote(relation.value)?.getLabelValue(name)
+ : null;
+ if (value !== null && value !== undefined) {
+ return value;
+ }
+ }
+ return null;
+ }
}
export default SNote;