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
feat(clipper): save the page's author with a page clip
Elian Dorand9b36cbac8b5
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.htmlindex f605878b84..7ff90f8bbc 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html@@ -39,11 +39,15 @@<li><em>Selection</em>, when text is selected on the page: <em>Save selection</em> savesit into the note of that page's clippings, as the context menu does.</li><li><em>Page</em>, when the page has an article: <em>Save page to Trilium</em> saves- the article's text and images. When the page states when it was published,- that date shows under the description, marked by the <span class="tn-icon bx bx-calendar"></span> icon,- and is saved in the <code>#publishedDate</code> label of the note. When it- states when it was last modified, that date shows below, marked by the <span- class="tn-icon bx bx-history"></span> icon, and is saved in the <code>#modifiedDate</code> label.</li>+ the article's text and images. The author the page names shows under the+ description, marked by the <span class="tn-icon bx bx-user"></span> icon.+ It can be edited or cleared before saving, or written in when the page+ names none, and is saved in the <code>#pageAuthor</code> label of the note.+ When the page states when it was published, that date shows under the author,+ marked by the <span class="tn-icon bx bx-calendar"></span> icon, and is saved+ in the <code>#publishedDate</code> label of the note. When it states when+ it was last modified, that date shows below, marked by the <span class="tn-icon bx bx-history"></span> icon,+ and is saved in the <code>#modifiedDate</code> label.</li><li><em>Bookmark</em>, always: a text box for a note about the page replacesthe preview, and <em>Save bookmark</em> (or <kbd>Ctrl</kbd>+<kbd>Enter</kbd>)saves a link to the page with that note. Left empty, the title stays thediff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsxindex 823731baa5..ebdbb6a4b3 100644--- a/apps/web-clipper/entrypoints/popup/main.tsx+++ b/apps/web-clipper/entrypoints/popup/main.tsx@@ -17,6 +17,7 @@ import RefreshCw from "@boxicons/js/icons/RefreshCw";import Screenshot from "@boxicons/js/icons/Screenshot";import Tabs from "@boxicons/js/icons/Tabs";import Unlink from "@boxicons/js/icons/Unlink";+import User from "@boxicons/js/icons/User";import Youtube from "@boxicons/js/icons/Youtube";import monospaceFontUrl from "@triliumnext/client/src/fonts/JetBrainsMono-Light.woff2?url";import type { ComponentChildren } from "preact";@@ -79,6 +80,8 @@ interface Draft {title?: string;/** `undefined` until the user edits the page's description. */description?: string;+ /** `undefined` until the user edits the author of the page. */+ author?: string;text: string;}@@ -336,7 +339,7 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {draft: Draft;onDraftChange: (draft: Draft) => void;}) {- const { mode: chosenMode, title, description, text } = draft;+ const { mode: chosenMode, title, description, author, text } = draft;const textAreaRef = useRef<HTMLTextAreaElement>(null);const article = typeof tab?.page === "object" ? tab.page : undefined;@@ -347,6 +350,8 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {const shownTitle = title ?? (article ?? selection)?.title ?? tab?.title ?? "";const shownDescription = description ?? tab?.description ?? "";const savedDescription = shownDescription.replace(/\s+/g, " ").trim();+ const shownAuthor = author ?? article?.labels.pageAuthor ?? "";+ const savedAuthor = shownAuthor.replace(/\s+/g, " ").trim();useEffect(() => {if (mode === "note") textAreaRef.current?.focus();@@ -365,7 +370,10 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {}function saveClip(content: SelectedContent) {- const labels = withDescription(content.labels, savedDescription);+ let labels = withLabel(content.labels, "pageDescription", savedDescription);+ if (mode === "page") {+ labels = withLabel(labels, "pageAuthor", savedAuthor);+ }const titled = { ...content, title: shownTitle.trim() || content.title, ...(labels && { labels }) };void sendMessage(mode === "selection"? { name: "save-selection", selection: titled }@@ -447,6 +455,24 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {onKeyDown={saveNoteOnCtrlEnter}/></div>+ {mode === "page" && (+ <div className="page-field">+ <span+ className="page-field-icon"+ title="The page's author. It is saved in the #pageAuthor label of the note."+ >+ <Icon icon={User} />+ </span>+ <input+ type="text"+ className="page-author"+ aria-label="Author"+ placeholder="Add an author"+ value={shownAuthor}+ onInput={(e) => onDraftChange({ ...draft, author: e.currentTarget.value })}+ />+ </div>+ )}{published && (<div className="page-field"><span@@ -507,10 +533,10 @@ function PagePreview({ tab, disabled, shortcuts, draft, onDraftChange }: {);}-/** A clip's labels with `#pageDescription` as the user left it, or without one when it is empty. */-function withDescription(labels: Record<string, string> | undefined, description: string) {- const { pageDescription: _, ...others } = labels ?? {};- return description ? { ...others, pageDescription: description } : labels && others;+/** A clip's labels with the label `name` as the user left it, or without it when `value` is empty. */+function withLabel(labels: Record<string, string> | undefined, name: string, value: string) {+ const { [name]: _, ...others } = labels ?? {};+ return value ? { ...others, [name]: value } : labels && others;}/**diff --git a/apps/web-clipper/entrypoints/popup/popup.css b/apps/web-clipper/entrypoints/popup/popup.cssindex d8a381969f..107c8dfc47 100644--- a/apps/web-clipper/entrypoints/popup/popup.css+++ b/apps/web-clipper/entrypoints/popup/popup.css@@ -174,7 +174,8 @@ body {}}- .page-description {+ .page-description,+ .page-author {--input-background-color: transparent;flex-grow: 1;diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsxindex 8295a17d05..d8d08ad611 100644--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx@@ -25,7 +25,7 @@ const PAGE = {images: [ { imageId: "i1", src: "https://example.com/a.png" } ],pageUrl: "https://example.com/post?id=1",clipType: "page",- labels: { publishedDate: "2024-05-01", modifiedDate: "2024-06-12" }+ labels: { pageAuthor: "Jane Doe", publishedDate: "2024-05-01", modifiedDate: "2024-06-12" }};/** What `readable-page.js` returns in the tab, as `browser.scripting.executeScript()` answers. */const executeScript = vi.fn(async (): Promise<unknown> => [ { result: PAGE } ]);@@ -217,12 +217,12 @@ describe("popup", () => {expect(heading?.querySelector<HTMLInputElement>(".page-title")?.value).toBe("An article");expect(heading?.querySelector(".page-meta")?.textContent).toBe("example.com");expect(publishedDate()).toBe("Published 2024-05-01");- const dateIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(2) > .page-field-icon");+ const dateIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(3) > .page-field-icon");expect(dateIcon?.querySelector("svg.icon path")).not.toBeNull();expect(dateIcon?.title).toBe("The date the page was published. It is saved in the #publishedDate label of the note.");expect(modifiedDate()).toBe("Updated 2024-06-12");- const modifiedIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(3) > .page-field-icon");+ const modifiedIcon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(4) > .page-field-icon");expect(modifiedIcon?.querySelector("svg.icon path")).not.toBeNull();expect(modifiedIcon?.title).toBe("The date the page was last modified. It is saved in the #modifiedDate label of the note.");@@ -249,7 +249,7 @@ describe("popup", () => {executeScript.mockResolvedValueOnce([ { result: { ...PAGE, labels: {} } } ]);await rerender();expect(container.querySelector(".page-meta")?.textContent).toBe("example.com");- expect(container.querySelectorAll(".page-fields > .page-field")).toHaveLength(1);+ expect(container.querySelectorAll(".page-fields > .page-field")).toHaveLength(2);expect(publishedDate()).toBeUndefined();expect(modifiedDate()).toBeUndefined();});@@ -464,6 +464,46 @@ describe("popup", () => {expect(container.querySelector(".page-body .note-text")).not.toBeNull();});+ it("offers the page's author in Page mode, and saves it as edited", async () => {+ const author = () => container.querySelector<HTMLInputElement>(".page-fields .page-author");+ expect(author()?.value).toBe("Jane Doe");+ expect(author()?.getAttribute("aria-label")).toBe("Author");+ expect(author()?.getAttribute("placeholder")).toBe("Add an author");+ const icon = container.querySelector<HTMLElement>(".page-fields > .page-field:nth-child(2) > .page-field-icon");+ expect(icon?.querySelector("svg.icon path")).not.toBeNull();+ expect(icon?.title).toBe("The page's author. It is saved in the #pageAuthor label of the note.");++ await type(".page-author", " John Smith ");+ await click("Save page to Trilium");+ expect(sendMessage).toHaveBeenLastCalledWith({+ name: "save-whole-page",+ page: { ...PAGE, labels: { ...PAGE.labels, pageAuthor: "John Smith" } }+ });++ await type(".page-author", " ");+ expect(author()).not.toBeNull();+ await click("Save page to Trilium");+ const { pageAuthor: _, ...withoutAuthor } = PAGE.labels;+ expect(sendMessage).toHaveBeenLastCalledWith({ name: "save-whole-page", page: { ...PAGE, labels: withoutAuthor } });++ selection = SELECTION;+ await rerender();+ expect(author()).toBeNull();+ await click("Bookmark");+ expect(author()).toBeNull();++ selection = undefined;+ executeScript.mockResolvedValueOnce([ { result: { ...PAGE, labels: {} } } ]);+ await rerender();+ expect(author()?.value).toBe("");+ await type(".page-author", "Written by hand");+ await click("Save page to Trilium");+ expect(sendMessage).toHaveBeenLastCalledWith({+ name: "save-whole-page",+ page: { ...PAGE, labels: { pageAuthor: "Written by hand" } }+ });+ });+it("never lets a preview run scripts, since it shares the extension's origin", async () => {selection = SELECTION;await rerender();diff --git a/apps/web-clipper/entrypoints/readable-page/index.spec.ts b/apps/web-clipper/entrypoints/readable-page/index.spec.tsindex 0ac6a31718..3e93508a3f 100644--- a/apps/web-clipper/entrypoints/readable-page/index.spec.ts+++ b/apps/web-clipper/entrypoints/readable-page/index.spec.ts@@ -22,6 +22,7 @@ describe("readable page", () => {<meta property="article:published_time" content="2026-01-02T10:00:00Z"><meta property="article:modified_time" content="2026-02-03T10:00:00Z"><meta property="og:description" content="About the article">+ <meta name="author" content=" Jane Doe "><script>window.tracked = true;</script>`;document.body.innerHTML = `<nav><a href="/">Home</a> <a href="/archive">Archive</a></nav>@@ -44,6 +45,7 @@ describe("readable page", () => {clipType: "page",labels: {pageDescription: "About the article",+ pageAuthor: "Jane Doe",publishedDate: "2026-01-02",modifiedDate: "2026-02-03"},diff --git a/apps/web-clipper/entrypoints/readable-page/index.ts b/apps/web-clipper/entrypoints/readable-page/index.tsindex 7083781e10..81b5529ed2 100644--- a/apps/web-clipper/entrypoints/readable-page/index.ts+++ b/apps/web-clipper/entrypoints/readable-page/index.ts@@ -23,6 +23,10 @@ function readPage(): ReadablePage | undefined {const images = getImages(body);const labels = descriptionLabels();+ const author = article.author.replace(/\s+/g, " ").trim();+ if (author) {+ labels['pageAuthor'] = author;+ }const publishedDate = isoDate(metaContent("article:published_time") || article.published);if (publishedDate) {labels['publishedDate'] = publishedDate;diff --git a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.mdindex 2221e95ff5..4529613219 100644--- a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md+++ b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md@@ -32,7 +32,7 @@ Clicking the extension's button in the browser toolbar opens its popup:* The popup shows what will be saved from the current page: the note's title, which can be edited before saving, the website, and below them a preview, which starts with the page's description. A switch above the preview chooses what to save, among what the page offers:* _Selection_, when text is selected on the page: _Save selection_ saves it into the note of that page's clippings, as the context menu does.- * _Page_, when the page has an article: _Save page to Trilium_ saves the article's text and images. When the page states when it was published, that date shows under the description, marked by the <span class="tn-icon bx bx-calendar"></span> icon, and is saved in the `#publishedDate` label of the note. When it states when it was last modified, that date shows below, marked by the <span class="tn-icon bx bx-history"></span> icon, and is saved in the `#modifiedDate` label.+ * _Page_, when the page has an article: _Save page to Trilium_ saves the article's text and images. The author the page names shows under the description, marked by the <span class="tn-icon bx bx-user"></span> icon. It can be edited or cleared before saving, or written in when the page names none, and is saved in the `#pageAuthor` label of the note. When the page states when it was published, that date shows under the author, marked by the <span class="tn-icon bx bx-calendar"></span> icon, and is saved in the `#publishedDate` label of the note. When it states when it was last modified, that date shows below, marked by the <span class="tn-icon bx bx-history"></span> icon, and is saved in the `#modifiedDate` label.* _Bookmark_, always: a text box for a note about the page replaces the preview, and _Save bookmark_ (or <kbd>Ctrl</kbd>+<kbd>Enter</kbd>) saves a link to the page with that note. Left empty, the title stays the page's title.The description, marked by the <span class="tn-icon bx bx-align-left"></span> icon, comes from the page's `description` meta tag, or the one it gives social sites. It can be edited or cleared before saving, or written in when the page has none, and is saved in the `#pageDescription` label of the note. Clippings saved with the keyboard shortcuts or the context menu, screenshots and images included, get the description as the page gives it. Each clipping that brings a description updates the label.