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): print the page's meta tags from a list
Elian Doran84ff82daee68
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 5d923c8145..cae8d362f2 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@@ -68,7 +68,9 @@<td>The values of the page's <code><head></code>: <code>title</code> (thenote's title, followed by the site's), <code>description</code> (<code>#shareDescription</code>), <code>noIndex</code> (<code>#shareDisallowRobotIndexing</code>)and <code>openGraph</code> with <code>url</code>, <code>domain</code>, <code>image</code>, <code>color</code> and <code>card</code> (<code>summary_large_image</code> with- an image, <code>summary</code> without). A value whose label is not set is <code>null</code>.</td>+ an image, <code>summary</code> without). A value whose label is not set is <code>null</code>. <code>metaTags</code> lists+ the description, OpenGraph and Twitter <code><meta></code> tags of+ the values that are set, each with <code>attribute</code> (<code>name</code> or <code>property</code>), <code>key</code> and <code>content</code>.</td></tr><tr><td><code>logo</code>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 b86031df26..a38c44a106 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@@ -30,7 +30,7 @@ Your template is rendered with a context object exposing the note and its render| `content` | The note's already-rendered HTML content, as a string. || `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`. |+| `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`. || `childLinks` | The links to the note's visible children, each with `title`, `href`, `isExternal` and `type`. |diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.tsindex 77dce7815f..289f68ab48 100644--- a/packages/share-theme/src/model/page.spec.ts+++ b/packages/share-theme/src/model/page.spec.ts@@ -23,7 +23,7 @@ describe("getPageHead", () => {labels: { shareDescription: "About the page", shareDisallowRobotIndexing: "" }});- expect(getPageHead(page, site)).toStrictEqual({+ expect(getPageHead(page, site)).toMatchObject({title: "Page - My site",description: "About the page",noIndex: true,@@ -45,7 +45,7 @@ describe("getPageHead", () => {relations: { shareOpenGraphImage: "imageNote" }});- expect(getPageHead(site, site)).toStrictEqual({+ expect(getPageHead(site, site)).toMatchObject({title: "My site",description: null,noIndex: false,@@ -93,6 +93,44 @@ describe("getPageHead", () => {openGraph: { url: null, domain: null, image: null, color: null, card: "summary" }});});++ it("lists the meta tags that have a value, ready to print", () => {+ const site = fakeNote({+ noteId: "site",+ title: "Site",+ labels: {+ shareDescription: "About",+ shareOpenGraphURL: "https://example.com/share/",+ shareOpenGraphDomain: "example.com",+ shareOpenGraphImage: "cover.png",+ shareOpenGraphColor: "#fff"+ }+ });+ const tags = (note: ShareNote) => getPageHead(note, note).metaTags+ .map(({ attribute, key, content }) => `${attribute}:${key}=${content}`);++ expect(tags(site)).toStrictEqual([+ "name:description=About",+ "property:og:type=website",+ "property:og:title=Site",+ "property:og:description=About",+ "property:og:url=https://example.com/share/",+ "property:og:image=https://example.com/share/cover.png",+ "name:twitter:card=summary_large_image",+ "name:twitter:title=Site",+ "name:twitter:description=About",+ "property:twitter:domain=example.com",+ "property:twitter:url=https://example.com/share/",+ "name:twitter:image=https://example.com/share/cover.png",+ "name:theme-color=#fff"+ ]);+ expect(tags(fakeNote({ noteId: "bare", title: "Bare" }))).toStrictEqual([+ "property:og:type=website",+ "property:og:title=Bare",+ "name:twitter:card=summary",+ "name:twitter:title=Bare"+ ]);+ });});describe("getHtmlSnippets", () => {diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.tsindex 02ef38b6c1..5aea21a5b6 100644--- a/packages/share-theme/src/model/page.ts+++ b/packages/share-theme/src/model/page.ts@@ -63,6 +63,15 @@ export interface PageHead {/** The kind of Twitter card: a large picture when there is an image. */card: "summary_large_image" | "summary";};+ /** The description, OpenGraph and Twitter `<meta>` tags of the values that are set. */+ metaTags: MetaTag[];+}++/** A `<meta>` tag, such as `<meta property="og:title" content="…">`. */+export interface MetaTag {+ attribute: "name" | "property";+ key: string;+ content: string;}/** The site logo in the page header. */@@ -107,20 +116,44 @@ export function getPageHead(note: ShareNote, siteRoot: ShareNote): PageHead {? `api/images/${siteRoot.getRelationValue("shareOpenGraphImage")}/image.png`: readLabel(siteRoot, "shareOpenGraphImage");+ const description = readLabel(note, "shareDescription");+ const openGraph: PageHead["openGraph"] = {+ url,+ domain: readLabel(siteRoot, "shareOpenGraphDomain"),+ image: image && url ? toAbsoluteUrl(image, url) : image,+ color: readLabel(siteRoot, "shareOpenGraphColor"),+ card: image ? "summary_large_image" : "summary"+ };+return {title,- description: readLabel(note, "shareDescription"),+ description,noIndex: note.hasLabel("shareDisallowRobotIndexing"),- openGraph: {- url,- domain: readLabel(siteRoot, "shareOpenGraphDomain"),- image: image && url ? toAbsoluteUrl(image, url) : image,- color: readLabel(siteRoot, "shareOpenGraphColor"),- card: image ? "summary_large_image" : "summary"- }+ openGraph,+ metaTags: getMetaTags(title, description, openGraph)};}+function getMetaTags(title: string, description: string | null, openGraph: PageHead["openGraph"]) {+ const tags: [ MetaTag["attribute"], string, string | null ][] = [+ [ "name", "description", description ],+ [ "property", "og:type", "website" ],+ [ "property", "og:title", title ],+ [ "property", "og:description", description ],+ [ "property", "og:url", openGraph.url ],+ [ "property", "og:image", openGraph.image ],+ [ "name", "twitter:card", openGraph.card ],+ [ "name", "twitter:title", title ],+ [ "name", "twitter:description", description ],+ [ "property", "twitter:domain", openGraph.domain ],+ [ "property", "twitter:url", openGraph.url ],+ [ "name", "twitter:image", openGraph.image ],+ [ "name", "theme-color", openGraph.color ]+ ];+ return tags.flatMap(([ attribute, key, content ]): MetaTag[] =>+ (content ? [ { attribute, key, content } ] : []));+}+/** Returns the trimmed value of the label, or `null` when it is missing or blank. */function readLabel(note: ShareNote, name: string) {return note.getLabelValue(name)?.trim() || null;diff --git a/packages/share-theme/src/templates/page.ejs b/packages/share-theme/src/templates/page.ejsindex 82ef27856f..6e01a1d1f1 100644--- a/packages/share-theme/src/templates/page.ejs+++ b/packages/share-theme/src/templates/page.ejs@@ -18,37 +18,8 @@<% } %><title><%= head.title %></title><%- include("boot_script") %>- <% const { description, openGraph } = head; %>- <% if (description) { %>- <meta name="description" content="<%= description %>">- <% } %>- <meta property="og:type" content="website">- <meta property="og:title" content="<%= head.title %>">- <% if (description) { %>- <meta property="og:description" content="<%= description %>">- <% } %>- <% if (openGraph.url) { %>- <meta property="og:url" content="<%= openGraph.url %>">- <% } %>- <% if (openGraph.image) { %>- <meta property="og:image" content="<%= openGraph.image %>">- <% } %>- <meta name="twitter:card" content="<%= openGraph.card %>">- <meta name="twitter:title" content="<%= head.title %>">- <% if (description) { %>- <meta name="twitter:description" content="<%= description %>">- <% } %>- <% if (openGraph.domain) { %>- <meta property="twitter:domain" content="<%= openGraph.domain %>">- <% } %>- <% if (openGraph.url) { %>- <meta property="twitter:url" content="<%= openGraph.url %>">- <% } %>- <% if (openGraph.image) { %>- <meta name="twitter:image" content="<%= openGraph.image %>">- <% } %>- <% if (openGraph.color) { %>- <meta name="theme-color" content="<%= openGraph.color %>">+ <% for (const meta of head.metaTags) { %>+ <meta <%= meta.attribute %>="<%= meta.key %>" content="<%= meta.content %>"><% } %><style id="trilium-icon-packs"><%- utils.escapeInlineStylesheet(iconPackCss) %>