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
refactor(share): give the page template its last conditions as values
Elian Dorand1ced1bc53e2
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 869cfdc650..897d15980f 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@@ -85,6 +85,13 @@page being shown), <code>isExpanded</code> (that page or one of its ancestors)and <code>children</code>.</td></tr>+ <tr>+ <td><code>contentClasses</code>+ </td>+ <td>The classes of the content element: <code>type-<note type></code>, <code>ck-content</code> for+ text and Markdown notes, and <code>no-content</code> when the note has no+ content of its own.</td>+ </tr><tr><td><code>childLinks</code></td>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 eb0beb2003..9859a4cca4 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@@ -33,6 +33,7 @@ Your template is rendered with a context object exposing the note and its render| `head` | The values of the page's `<head>`: `title` (the note's title, followed by the site's), `description` (`#shareDescription`), `noIndex` (`#shareDisallowRobotIndexing`) and `openGraph` with `url`, `domain`, `image`, `color` and `card` (`summary_large_image` with an image, `summary` without). A value whose label is not set is `null`. `metaTags` lists the description, OpenGraph and Twitter `<meta>` tags of the values that are set, each with `attribute` (`name` or `property`), `key` and `content`. || `logo` | The site logo in the header: `href` (`#shareRootLink`, otherwise the site's root page), `width` and `height`. || `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` and `type`. || `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 4aeb780af4..6205f8575b 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -1,7 +1,7 @@import { describe, expect, it } from "vitest";import {- getChildLinks, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,+ getChildLinks, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,getPrevNextLinks, getShareLink, getSiteAncestorIds, getSiteLogo, getTableOfContents,type NavigationItem, type PageHeading, type ShareNote} from "./page.js";@@ -349,6 +349,18 @@ describe("getLastUpdated", () => {});});+describe("getContentClasses", () => {+ const classes = (type: string, mime: string, isEmpty = false) =>+ getContentClasses(fakeNote({ noteId: "page", type, mime }), isEmpty);++ it("styles text and Markdown notes as the editor's content, and marks empty content", () => {+ expect(classes("text", "text/html")).toBe("type-text ck-content");+ expect(classes("code", "text/x-markdown")).toBe("type-code ck-content");+ expect(classes("code", "application/javascript")).toBe("type-code");+ expect(classes("book", "", true)).toBe("type-book no-content");+ });+});+describe("getChildLinks", () => {it("links to the visible children of a note, in order", () => {const parent = fakeNote({ noteId: "parent" });@@ -388,6 +400,7 @@ interface FakeNoteOptions {snippets?: (ShareNote | null)[];content?: string;type?: string;+ mime?: string;icon?: string;}@@ -405,6 +418,7 @@ function fakeNote(options: FakeNoteOptions): FakeNote {shareId: `${options.noteId}-alias`,title: options.title ?? options.noteId,type: options.type ?? "text",+ mime: options.mime ?? "text/html",getIcon: (prefixes) => [ options.icon ?? "bx bx-note", ...prefixes ?? [] ].join(" "),getLabelValue: (name) => labels[name] ?? null,hasLabel: (name) => name in labels,diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 76c968729e..3d38774c35 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -23,6 +23,7 @@ export interface ShareNote {getParentNotes(): ShareNote[];getVisibleChildNotes(): ShareNote[];type: string;+ mime: string;/** The icon's CSS classes, among the icon packs whose prefixes are given. */getIcon(iconPackPrefixes?: string[]): string;}@@ -309,6 +310,20 @@ export function getShareLink(note: ShareNote, sanitizeUrl: (url: string) => stri: { href: `./${note.shareId}`, isExternal: false };}+/**+ * Returns the classes of the content element: the note's type, `ck-content` for content the text+ * editor's styles apply to (text notes and Markdown code notes), and `no-content` when empty.+ */+export function getContentClasses(note: ShareNote, isEmpty = false) {+ const isEditorContent = note.type === "text"+ || (note.type === "code" && note.mime === "text/x-markdown");+ return [+ `type-${note.type}`,+ isEditorContent && "ck-content",+ isEmpty && "no-content"+ ].filter(Boolean).join(" ");+}+/** Returns the links to the visible children of `note`, for its list of subpages. */export function getChildLinks(note: ShareNote, sanitizeUrl: (url: string) => string) {return note.getVisibleChildNotes().map((child) => ({diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejsindex 857810c9bd..404e5313fd 100644--- a/packages/share-theme/src/templates/page.ejs+++ b/packages/share-theme/src/templates/page.ejs@@ -1,7 +1,6 @@<!DOCTYPE html><html lang="<%= language.page.lang %>" dir="<%= language.page.dir %>"><head>- <% const hasTree = subRoot.note.hasVisibleChildren(); %><%- snippets["head:start"] %><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">@@ -29,13 +28,13 @@<body data-note-id="<%= note.noteId %>" class="type-<%= note.type %>" data-ancestor-note-id="<%= subRoot.note.noteId %>"><%- snippets["body:start"] %><div id="header">- <button aria-label="<%= t("share_theme.toggle-navigation") %>" id="left-pane-toggle-button" class="header-button"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M4 6h16v2H4zm0 5h16v2H4zm0 5h16v2H4z"></path></svg></button>+ <button aria-label="<%= t("share_theme.toggle-navigation") %>" id="left-pane-toggle-button" class="header-button"><span class="bx bx-menu" aria-hidden="true"></span></button><a href="<%= logo.href %>" id="header-logo"><img src="<%= logoUrl %>" width="<%= logo.width %>" height="<%= logo.height %>" alt="<%= t("share_theme.logo-alt") %>" /><%= subRoot.note.title %></a><% if (headings.length > 1) { %>- <button aria-label="<%= t("share_theme.toggle-toc") %>" id="toc-pane-toggle-button" class="header-button"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9h14V7H3v2zm0 4h14v-2H3v2zm0 4h14v-2H3v2zm16 0h2v-2h-2v2zm0-10V7h2v2h-2zm0 6h2v-2h-2v2z"></path></svg></button>+ <button aria-label="<%= t("share_theme.toggle-toc") %>" id="toc-pane-toggle-button" class="header-button"><span class="bx bx-list-ul" aria-hidden="true"></span></button><% } else { %><div class="header-button-placeholder"></div><% } %>@@ -49,18 +48,18 @@<label class="switch"><input type="checkbox" aria-labelledby="sitetheme"><span class="slider"></span>- <svg class="dark-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M20.742 13.045a8.088 8.088 0 0 1-2.077.271c-2.135 0-4.14-.83-5.646-2.336a8.025 8.025 0 0 1-2.064-7.723A1 1 0 0 0 9.73 2.034a10.014 10.014 0 0 0-4.489 2.582c-3.898 3.898-3.898 10.243 0 14.143a9.937 9.937 0 0 0 7.072 2.93 9.93 9.93 0 0 0 7.07-2.929 10.007 10.007 0 0 0 2.583-4.491 1.001 1.001 0 0 0-1.224-1.224zm-2.772 4.301a7.947 7.947 0 0 1-5.656 2.343 7.953 7.953 0 0 1-5.658-2.344c-3.118-3.119-3.118-8.195 0-11.314a7.923 7.923 0 0 1 2.06-1.483 10.027 10.027 0 0 0 2.89 7.848 9.972 9.972 0 0 0 7.848 2.891 8.036 8.036 0 0 1-1.484 2.059z"></path></svg>- <svg class="light-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M6.993 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007S14.761 6.993 12 6.993 6.993 9.239 6.993 12zM12 8.993c1.658 0 3.007 1.349 3.007 3.007S13.658 15.007 12 15.007 8.993 13.658 8.993 12 10.342 8.993 12 8.993zM10.998 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2h-3zm17 0h3v2h-3zM4.219 18.363l2.12-2.122 1.415 1.414-2.12 2.122zM16.24 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.342 7.759 4.22 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path></svg>+ <span class="dark-icon bx bx-moon" aria-hidden="true"></span>+ <span class="light-icon bx bx-sun" aria-hidden="true"></span></label></div>- <% if (hasTree) { %>+ <% if (navigation.length) { %><div class="search-item">- <svg class="search-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M10 18a7.952 7.952 0 0 0 4.897-1.688l4.396 4.396 1.414-1.414-4.396-4.396A7.952 7.952 0 0 0 18 10c0-4.411-3.589-8-8-8s-8 3.589-8 8 3.589 8 8 8zm0-14c3.309 0 6 2.691 6 6s-2.691 6-6 6-6-2.691-6-6 2.691-6 6-6z"></path></svg>+ <span class="search-icon bx bx-search" aria-hidden="true"></span><input type="text" class="search-input" placeholder="<%= t("share_theme.search_placeholder") %>"></div><% } %></div>- <% if (hasTree) { %>+ <% if (navigation.length) { %><nav id="menu"><%- include("tree_item", { items: navigation }) %></nav>@@ -69,10 +68,10 @@</div><div id="right-pane"><div id="main">- <div id="content" class="type-<%= note.type %><% if (note.type === "text" || (note.type === "code" && note.mime === "text/x-markdown")) { %> ck-content<% } %><% if (isEmpty) { %> no-content<% } %>"<% if (language.content) { %> lang="<%= language.content.lang %>" dir="<%= language.content.dir %>"<% } %> data-tab-title-placeholder="<%= t("share_theme.tab-title") %>">+ <div id="content" class="<%= contentClasses %>"<% if (language.content) { %> lang="<%= language.content.lang %>" dir="<%= language.content.dir %>"<% } %> data-tab-title-placeholder="<%= t("share_theme.tab-title") %>"><%- snippets["content:start"] %><h1 id="title"><%= note.title %></h1>- <% if (isEmpty && (!note.hasVisibleChildren() && note.type !== "book")) { %>+ <% if (isEmpty && !childLinks.length && note.type !== "book") { %><p><%= t("share_page.no-content") %></p><% } else { %><%- content %>@@ -80,7 +79,7 @@<%- snippets["content:end"] %></div>- <% if (note.hasVisibleChildren() || note.type === "book") { %>+ <% if (childLinks.length) { %><nav id="childLinks" class="<% if (isEmpty) { %>grid<% } else { %>list<% } %>"><% if (!isEmpty) { %><span><%= t("share_theme.subpages") %></span>@@ -103,7 +102,7 @@</div><% } %>- <% if (hasTree) { %>+ <% if (navigation.length) { %><%- include("prev_next") %><% } %>diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 8439f292d7..f88447ece2 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -1,6 +1,6 @@import { trimIndentation } from "@triliumnext/commons";import {- getChildLinks, getHtmlSnippets, getPageHead, getPrevNextLinks, getSiteLogo+ getChildLinks, getContentClasses, getHtmlSnippets, getPageHead, getPrevNextLinks, getSiteLogo} from "@triliumnext/share-theme/model/page";import ejs from "ejs";import { parse } from "node-html-parser";@@ -420,6 +420,23 @@ describe("content_renderer", () => {.format(new Date(time?.getAttribute("datetime") ?? "")));});+ it("shows a subpage list only when there are subpages", () => {+ const shareRootNote = buildShareNote({+ id: shareRoot.SHARE_ROOT_NOTE_ID,+ children: [+ { id: "emptyBook", type: "book", content: "" },+ { id: "fullBook", type: "book", content: "", children: [ { id: "bookChild", title: "Child" } ] }+ ]+ });+ const [ emptyBook, fullBook ] = shareRootNote.getChildNotes()+ .map((note) => parse(String(renderNoteContent(note))));++ expect(emptyBook.querySelector("#childLinks") === null).toBe(true);+ expect(emptyBook.querySelector("#content")?.classList.contains("no-content")).toBe(true);+ expect(fullBook.querySelectorAll("#childLinks a").map((link) => link.text))+ .toStrictEqual([ "Child" ]);+ });+it("keeps the alt text of an image on the page", () => {const shareRootNote = buildShareNote({id: shareRoot.SHARE_ROOT_NOTE_ID,@@ -1267,6 +1284,7 @@ describe("content_renderer", () => {prevNext: getPrevNextLinks(note, note),navigation: [],childLinks: getChildLinks(note, sanitize.sanitizeUrl),+ contentClasses: getContentClasses(note, isEmpty),language: { page: { lang: "en", dir: "ltr" }, content: null },lastUpdated: null,headings: [],diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex 78b6cb51e8..f2b227b3b6 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -8,7 +8,7 @@ import { renderToHtml as renderMarkdownToHtml } from "@triliumnext/commons/src/limport { renderSpreadsheetToHtml } from "@triliumnext/commons/src/lib/spreadsheet/render_to_html.js";import { getLanguage, highlight, highlightAuto, syncMimeTypes } from "@triliumnext/highlightjs";import {- getChildLinks, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,+ getChildLinks, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,getPrevNextLinks, getShareLink, getSiteAncestorIds, getSiteLogo, getTableOfContents,type PageHeading} from "@triliumnext/share-theme/model/page";@@ -231,6 +231,7 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)iconPackPrefixes: renderArgs.iconPackSupportedPrefixes}),childLinks: getChildLinks(note, sanitize.sanitizeUrl),+ contentClasses: getContentClasses(note, isEmpty),language: getPageLanguages(note, {displayLanguage,defaultContentLanguage: options.getOptionOrNull("defaultContentLanguage")