Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): show the site's note icon as the logo by default

Elian Doran75003ccfc35f
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
index 58630219ae..bf2e361874 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html
@@ -454,7 +454,8 @@ class="admonition note">
<td><code>~shareLogo</code>
</td>
<td>Relation set to an image to use as logo. The image must be part of the
- share tree (it can be hidden if needed).</td>
+ share tree (it can be hidden if needed). Without it, the header shows the
+ icon of the shared root note.</td>
</tr>
<tr>
<td><code>#shareLogoWidth</code>
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.html
index 897d15980f..ad85ec5143 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
@@ -76,7 +76,10 @@
<td><code>logo</code>
</td>
<td>The site logo in the header: <code>href</code> (<code>#shareRootLink</code>,
- otherwise the site's root page), <code>width</code> and <code>height</code>.</td>
+ otherwise the site's root page), <code>image</code> (the <code>~shareLogo</code> image,
+ or <code>null</code>), <code>icon</code> (the icon classes of the site's root
+ note, shown when there is no <code>image</code>), and <code>width</code> and <code>height</code> for
+ the image.</td>
</tr>
<tr>
<td><code>navigation</code>
@@ -138,7 +141,8 @@
<tr>
<td><code>faviconUrl</code> / <code>logoUrl</code>
</td>
- <td>Resolved favicon and logo URLs.</td>
+ <td>Resolved favicon and logo URLs. Without <code>~shareLogo</code>, <code>logoUrl</code> is
+ the Trilium logo.</td>
</tr>
<tr>
<td><code>isStatic</code>
diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
index 714efcc651..8c2739a5fe 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md
@@ -297,7 +297,7 @@ It's possible to adjust the logo which is displayed on the top-left of the left
| Attribute | Description |
| --- | --- |
-| `~shareLogo` | Relation set to an image to use as logo. The image must be part of the share tree (it can be hidden if needed). |
+| `~shareLogo` | Relation set to an image to use as logo. The image must be part of the share tree (it can be hidden if needed). Without it, the header shows the icon of the shared root note. |
| `#shareLogoWidth` | The width of the logo, as a number without unit. With `#shareLogoHeight`, it sets the proportions of the logo, which is shown 32 pixels wide. Default is `53`. |
| `#shareLogoHeight` | The height of the logo, as a number without unit. With `#shareLogoWidth`, it sets the proportions of the logo. Default is `40`. |
| `#shareRootLink` | URL to navigate to when the logo is pressed. |
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.md
index 9859a4cca4..25bfdd4fa6 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
@@ -31,7 +31,7 @@ Your template is rendered with a context object exposing the note and its render
| `header` | Extra HTML to place in the document head for this note (used by some note types). |
| `isEmpty` | `true` when the note has no content of its own. |
| `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`. |
+| `logo` | The site logo 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` and `type`. |
@@ -41,7 +41,7 @@ Your template is rendered with a context object exposing the note and its render
| `snippets` | The HTML of the `~shareHtml` snippets for each location, such as `snippets["head:end"]`. Every location is present, empty when no snippet goes there. |
| `subRoot` | The root of the shared subtree as `{ note, branch }` — handy for a site-wide title or logo. |
| `cssToLoad` / `jsToLoad` | Arrays of stylesheet / script URLs the default theme would inject (includes anything added via `~shareCss` / `~shareJs`). |
-| `faviconUrl` / `logoUrl` | Resolved favicon and logo URLs. |
+| `faviconUrl` / `logoUrl` | Resolved favicon and logo URLs. Without `~shareLogo`, `logoUrl` is the Trilium logo. |
| `isStatic` | `true` during a static HTML export, `false` for a live server render. |
| `t` | The `i18next` translation function. |
| `utils` | Helper utilities such as `slugify()` and `stripTags()`. |
diff --git a/packages/share-theme/src/base.css b/packages/share-theme/src/base.css
index 7ebd9c26b4..bdf791d933 100644
--- a/packages/share-theme/src/base.css
+++ b/packages/share-theme/src/base.css
@@ -62,6 +62,11 @@ a:hover {
text-decoration: underline;
}
+/* An inline-block glyph does not take the underline of its link. */
+.tn-icon::before {
+ display: inline-block;
+}
+
pre, code, kbd, samp {
font-family: JetBrainsLight, ui-monospace, monospace;
}
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.ts
index 6205f8575b..36c2f56ebb 100644
--- a/packages/share-theme/src/model/page.spec.ts
+++ b/packages/share-theme/src/model/page.spec.ts
@@ -164,23 +164,27 @@ describe("getHtmlSnippets", () => {
describe("getSiteLogo", () => {
const sanitizeUrl = (url: string) => (url.startsWith("javascript:") ? "about:blank" : url);
+ const logo = (labels: Record<string, string>, image: string | null = null) =>
+ getSiteLogo(fakeNote({ noteId: "site", labels, icon: "bx bx-book" }), {
+ sanitizeUrl, image, iconPackPrefixes: [ "custom" ]
+ });
- it("draws the logo 32 pixels wide, in the proportions of its labels", () => {
- const logo = (labels: Record<string, string>) =>
- getSiteLogo(fakeNote({ noteId: "site", labels }), sanitizeUrl);
+ it("shows the site root's note icon without a logo image", () => {
+ expect(logo({})).toStrictEqual({
+ href: "./site-alias", image: null, icon: "bx bx-book custom", width: 32, height: 24
+ });
+ });
- expect(logo({})).toStrictEqual({ href: "./site-alias", width: 32, height: 24 });
+ it("draws a logo image 32 pixels wide, in the proportions of its labels", () => {
+ expect(logo({}, "api/images/logo/image.png").image).toBe("api/images/logo/image.png");
expect(logo({ shareLogoWidth: "100", shareLogoHeight: "50" }).height).toBe(16);
expect(logo({ shareLogoWidth: "auto", shareLogoHeight: "" }).height).toBe(24);
expect(logo({ shareLogoWidth: "0", shareLogoHeight: "-5" }).height).toBe(24);
});
it("links to #shareRootLink, made safe", () => {
- const logo = (shareRootLink: string) => getSiteLogo(
- fakeNote({ noteId: "site", labels: { shareRootLink } }), sanitizeUrl);
-
- expect(logo("https://example.com").href).toBe("https://example.com");
- expect(logo("javascript:alert(1)").href).toBe("about:blank");
+ expect(logo({ shareRootLink: "https://example.com" }).href).toBe("https://example.com");
+ expect(logo({ shareRootLink: "javascript:alert(1)" }).href).toBe("about:blank");
});
});
diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.ts
index 3d38774c35..a78186f49b 100644
--- a/packages/share-theme/src/model/page.ts
+++ b/packages/share-theme/src/model/page.ts
@@ -85,10 +85,24 @@ export interface MetaTag {
export interface SiteLogo {
/** `#shareRootLink` when set, otherwise the site's root page. */
href: string;
+ /** The `~shareLogo` image, or `null` to show {@link SiteLogo.icon} instead. */
+ image: string | null;
+ /** The icon classes of the site root's note icon. */
+ icon: string;
+ /** The size to draw {@link SiteLogo.image} at. */
width: number;
height: number;
}
+/** What {@link getSiteLogo} needs besides the site root. */
+export interface SiteLogoOptions {
+ /** Makes `#shareRootLink` safe to use as a link. */
+ sanitizeUrl: (url: string) => string;
+ /** The URL of the `~shareLogo` image, if any. */
+ image: string | null;
+ iconPackPrefixes?: string[];
+}
+
/** A heading of the page's content, as core's `preparePageContent()` finds it. */
export interface PageHeading {
/** 1 for `<h1>`, up to 6 for `<h6>`. */
@@ -257,17 +271,19 @@ export function getHtmlSnippets(note: ShareNote): Record<HtmlSnippetLocation, st
const LOGO_WIDTH = 32;
/**
- * Returns the site logo of the site starting at `siteRoot`. `#shareLogoWidth` and
- * `#shareLogoHeight` give the logo's proportions; a label that is not a positive number falls back
- * to the default logo's. `sanitizeUrl` makes `#shareRootLink` safe to use as a link.
+ * Returns the site logo of the site starting at `siteRoot`: the `~shareLogo` image when there is
+ * one, otherwise the site root's note icon. `#shareLogoWidth` and `#shareLogoHeight` give the
+ * image's proportions; a label that is not a positive number falls back to 53 by 40.
*/
-export function getSiteLogo(siteRoot: ShareNote, sanitizeUrl: (url: string) => string): SiteLogo {
+export function getSiteLogo(siteRoot: ShareNote, options: SiteLogoOptions): SiteLogo {
const width = readPositiveNumber(siteRoot.getLabelValue("shareLogoWidth")) ?? 53;
const height = readPositiveNumber(siteRoot.getLabelValue("shareLogoHeight")) ?? 40;
const rootLink = siteRoot.getLabelValue("shareRootLink");
return {
- href: rootLink ? sanitizeUrl(rootLink) : `./${siteRoot.shareId}`,
+ href: rootLink ? options.sanitizeUrl(rootLink) : `./${siteRoot.shareId}`,
+ image: options.image,
+ icon: siteRoot.getIcon(options.iconPackPrefixes),
width: LOGO_WIDTH,
height: Math.round(LOGO_WIDTH * height / width)
};
diff --git a/packages/share-theme/src/page/header.css b/packages/share-theme/src/page/header.css
index 62c1d93e39..b9f39d2f73 100644
--- a/packages/share-theme/src/page/header.css
+++ b/packages/share-theme/src/page/header.css
@@ -18,6 +18,10 @@
max-width: 32px;
}
+#header-logo .tn-icon {
+ font-size: 24px;
+}
+
.header-button {
color: var(--text-menu);
background: transparent;
diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejs
index b93642d6a8..9ab7618128 100644
--- a/packages/share-theme/src/templates/page.ejs
+++ b/packages/share-theme/src/templates/page.ejs
@@ -30,7 +30,11 @@
<div id="header">
<button aria-label="<%= t("share_theme.toggle-navigation") %>" id="left-pane-toggle-button" class="header-button"><span class="tn-icon 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") %>" />
+ <% if (logo.image) { %>
+ <img src="<%= logo.image %>" width="<%= logo.width %>" height="<%= logo.height %>" alt="<%= t("share_theme.logo-alt") %>" />
+ <% } else { %>
+ <span class="tn-icon <%= logo.icon %>" aria-hidden="true"></span>
+ <% } %>
<%= subRoot.note.title %>
</a>
<% if (headings.length > 1) { %>
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index f88447ece2..76d207e113 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1138,6 +1138,28 @@ describe("content_renderer", () => {
}
});
+ describe("Site logo", () => {
+ it("shows the site root's note icon, or the ~shareLogo image when there is one", () => {
+ const shareRootNote = buildShareNote({
+ id: shareRoot.SHARE_ROOT_NOTE_ID,
+ children: [
+ { "id": "iconSite", "title": "Icon", "content": "<p>a</p>", "#iconClass": "bx bx-book" },
+ { "id": "imageSite", "title": "Image", "content": "<p>b</p>", "~shareLogo": "iconSite" }
+ ]
+ });
+ const [ iconSite, imageSite ] = shareRootNote.getChildNotes();
+ const logoOf = (note: SNote) => parse(String(renderNoteContent(note))).querySelector("#header-logo");
+
+ const iconLogo = logoOf(iconSite);
+ expect(iconLogo?.querySelector("img") === null).toBe(true);
+ expect(iconLogo?.querySelector(".tn-icon")?.classList.contains("bx-book")).toBe(true);
+
+ const imageLogo = logoOf(imageSite);
+ expect(imageLogo?.querySelector(".tn-icon") === null).toBe(true);
+ expect(imageLogo?.querySelector("img")?.getAttribute("src")).toBe("api/images/iconSite/image.png");
+ });
+ });
+
describe("Navigation tree", () => {
it("links each page of the site, expanding the way to the page shown", () => {
const shareRootNote = buildShareNote({
@@ -1280,7 +1302,7 @@ describe("content_renderer", () => {
iconPackSupportedPrefixes: [],
head: getPageHead(note, note),
snippets: getHtmlSnippets(note),
- logo: getSiteLogo(note, sanitize.sanitizeUrl),
+ logo: getSiteLogo(note, { sanitizeUrl: sanitize.sanitizeUrl, image: null }),
prevNext: getPrevNextLinks(note, note),
navigation: [],
childLinks: getChildLinks(note, sanitize.sanitizeUrl),
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index f2b227b3b6..e72724a198 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -151,7 +151,8 @@ export function renderNoteContent(note: SNote, canAccessEmbed?: CanAccessEmbed)
}
const customLogoId = note.getRelation("shareLogo")?.value;
- const logoUrl = customLogoId ? `api/images/${customLogoId}/image.png` : `../${assetUrlFragment}/images/icon-color.svg`;
+ const logoImageUrl = customLogoId ? `api/images/${customLogoId}/image.png` : null;
+ const logoUrl = logoImageUrl ?? `../${assetUrlFragment}/images/icon-color.svg`;
const iconPacks = iconPackService.getIconPacks().filter(p => p.builtin || !!shaca.notes[p.manifestNoteId]);
return renderNoteContentInternal(note, {
@@ -160,6 +161,7 @@ export function renderNoteContent(note: SNote, canAccessEmbed?: CanAccessEmbed)
cssToLoad,
jsToLoad,
logoUrl,
+ logoImageUrl,
ancestors,
isStatic: false,
canAccessEmbed,
@@ -184,6 +186,8 @@ interface RenderArgs {
cssToLoad: string[];
jsToLoad: string[];
logoUrl: string;
+ /** The `~shareLogo` image; without it, the default template shows the site's note icon. */
+ logoImageUrl?: string | null;
ancestors: string[];
isStatic: boolean;
canAccessEmbed?: CanAccessEmbed;
@@ -224,7 +228,11 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)
sanitizeUrl: sanitize.sanitizeUrl,
head: getPageHead(note, siteRoot),
snippets: getHtmlSnippets(note),
- logo: getSiteLogo(siteRoot, sanitize.sanitizeUrl),
+ logo: getSiteLogo(siteRoot, {
+ sanitizeUrl: sanitize.sanitizeUrl,
+ image: renderArgs.logoImageUrl ?? null,
+ iconPackPrefixes: renderArgs.iconPackSupportedPrefixes
+ }),
prevNext: getPrevNextLinks(note, siteRoot),
navigation: getNavigationTree(siteRoot, note, renderArgs.ancestors, {
sanitizeUrl: sanitize.sanitizeUrl,