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): show a clipping's description, author and dates
Elian Doran344b52152dda
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex fa8edf8683..50cff7db7c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -736,6 +736,10 @@"language": "The language this note's content is written in, which drives spell checking and text direction.","original_file_name": "The name the file had when it was imported, kept so that it can be exported under it again.","page_url": "The address a clipped page came from, shown beside the note title.",+ "page_description": "The description of a clipped page, shown in the details of the note's \"Web clip\" badge.",+ "page_author": "The author of a clipped page, shown in the details of the note's \"Web clip\" badge.",+ "published_date": "The date a clipped page was published, shown in the details of the note's \"Web clip\" badge.",+ "modified_date": "The date a clipped page was last updated, shown in the details of the note's \"Web clip\" badge.","command": "Which built-in command a launcher runs when it is pressed.","display_mode": "Which pane a split editor shows: <code>source</code>, <code>preview</code>, or <code>split</code> for both. Overrides what <code>#readOnly</code> would otherwise choose.","search_string": "The query a saved search runs.",@@ -3816,6 +3820,10 @@"shared_unshare": "Remove share","clipped_note": "Web clip","clipped_note_description": "This note was originally taken from {{url}}.\n\nClick to navigate to the source webpage.",+ "clipped_note_details": "This note was taken from a web page. Click to see the page's description, author and dates.",+ "clipped_note_author": "Author",+ "clipped_note_published": "Published",+ "clipped_note_modified": "Updated","doc_url": "Online docs","doc_url_description": "Click to view this page on the online documentation website.","execute_script": "Run script",diff --git a/apps/client/src/widgets/attribute_widgets/attr_help.ts b/apps/client/src/widgets/attribute_widgets/attr_help.tsindex 84dd8f7438..3e2bc1df81 100644--- a/apps/client/src/widgets/attribute_widgets/attr_help.ts+++ b/apps/client/src/widgets/attribute_widgets/attr_help.ts@@ -123,6 +123,10 @@ export const ATTR_HELP: AttrHelpMap = {language: t("attribute_detail.language"),originalFileName: t("attribute_detail.original_file_name"),pageUrl: t("attribute_detail.page_url"),+ pageDescription: t("attribute_detail.page_description"),+ pageAuthor: t("attribute_detail.page_author"),+ publishedDate: t("attribute_detail.published_date"),+ modifiedDate: t("attribute_detail.modified_date"),command: t("attribute_detail.command"),displayMode: t("attribute_detail.display_mode"),searchString: t("attribute_detail.search_string"),diff --git a/apps/client/src/widgets/layout/NoteBadges.css b/apps/client/src/widgets/layout/NoteBadges.cssindex efd8c3e207..c485fefd64 100644--- a/apps/client/src/widgets/layout/NoteBadges.css+++ b/apps/client/src/widgets/layout/NoteBadges.css@@ -68,3 +68,45 @@}}++.dropdown-menu.clipped-note-details-panel {+ width: max-content;+ max-width: min(420px, calc(100vw - 20px));+ padding: 1em;++ .clipped-note-source {+ display: flex;+ align-items: center;+ gap: 0.5em;+ overflow-wrap: anywhere;+ }++ .clipped-note-description {+ margin: 0.75em 0 0;+ user-select: text;+ }++ .clipped-note-fields {+ display: grid;+ grid-template-columns: max-content 1fr;+ gap: 0.2em 2em;+ margin: 0.75em 0 0;++ > div {+ display: contents;+ }++ dt {+ display: flex;+ align-items: center;+ gap: 0.4em;+ font-weight: normal;+ opacity: 0.5;+ }++ dd {+ margin: 0;+ user-select: text;+ }+ }+}diff --git a/apps/client/src/widgets/layout/NoteBadges.spec.tsx b/apps/client/src/widgets/layout/NoteBadges.spec.tsxindex 6915661a76..1f6ab8dc28 100644--- a/apps/client/src/widgets/layout/NoteBadges.spec.tsx+++ b/apps/client/src/widgets/layout/NoteBadges.spec.tsx@@ -1,3 +1,4 @@+import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";import FNote from "../../entities/fnote";@@ -22,7 +23,7 @@ vi.mock("../../services/i18n", () => ({import { buildNote } from "../../test/easy-froca";import { renderInto } from "../../test/render";-import { ExecuteBadge, getShareScope, OfficePreviewBadge } from "./NoteBadges";+import { ClippedNoteBadge, ExecuteBadge, getShareScope, OfficePreviewBadge } from "./NoteBadges";const DOCX_MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";@@ -70,6 +71,70 @@ describe("ExecuteBadge", () => {}});+describe("ClippedNoteBadge", () => {+ const PAGE_URL = "https://www.example.com/articles/1";++ it("links straight to the page when the clipping carries nothing more about it", () => {+ const badge = renderBadge({ "#pageUrl": PAGE_URL });++ expect(badge.querySelector(".clipped-note-badge a")?.getAttribute("href")).toBe(PAGE_URL);+ expect(badge.querySelector(".dropdown-clipped-note-badge")).toBeNull();+ });++ it("opens the details of the page it was clipped from, leaving out what the page did not give", async () => {+ const details = await openDetails({+ "#pageUrl": PAGE_URL,+ "#pageDescription": "First line\nSecond line",+ "#pageAuthor": "Ada Lovelace",+ "#publishedDate": "2024-05-01"+ });++ const source = details.querySelector<HTMLAnchorElement>(".clipped-note-source a");+ expect(source?.getAttribute("href")).toBe(PAGE_URL);+ expect(source?.target).toBe("_blank");+ expect(source?.textContent).toBe("www.example.com");+ expect(details.querySelector(".clipped-note-description")?.textContent).toBe("First line\nSecond line");+ expect(fields(details)).toEqual([+ [ "breadcrumb_badges.clipped_note_author", "Ada Lovelace" ],+ [ "breadcrumb_badges.clipped_note_published", "2024-05-01" ]+ ]);+ });++ it("lists the dates of a page that has no description", async () => {+ const details = await openDetails({ "#pageUrl": PAGE_URL, "#modifiedDate": "2024-06-12" });++ expect(details.querySelector(".clipped-note-description")).toBeNull();+ expect(fields(details)).toEqual([ [ "breadcrumb_badges.clipped_note_modified", "2024-06-12" ] ]);+ });++ function renderBadge(labels: Record<`#${string}`, string>) {+ shownNote.current = buildNote({ title: "Clipping", ...labels });+ shownNote.viewScope = undefined;+ return renderInto(<ClippedNoteBadge />);+ }++ async function openDetails(labels: Record<`#${string}`, string>) {+ const toggle = renderBadge(labels).querySelector<HTMLButtonElement>(".dropdown-clipped-note-badge button");+ expect(toggle).not.toBeNull();+ act(() => {+ toggle?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));+ toggle?.click();+ });+ return await vi.waitFor(() => {+ const details = document.querySelector<HTMLElement>(".tn-popup .clipped-note-details");+ if (!details) throw new Error("expected the details panel to open");+ return details;+ });+ }++ function fields(details: HTMLElement) {+ return [ ...details.querySelectorAll(".clipped-note-fields > div") ].map((field) => [+ field.querySelector("dt")?.textContent,+ field.querySelector("dd")?.textContent+ ]);+ }+});+describe("getShareScope", () => {it("tells a public share from a local one, a standalone preview and one only an export publishes", () => {const syncServerHost = "https://sync.example.com";diff --git a/apps/client/src/widgets/layout/NoteBadges.tsx b/apps/client/src/widgets/layout/NoteBadges.tsxindex 0c188f8eef..605d6e7411 100644--- a/apps/client/src/widgets/layout/NoteBadges.tsx+++ b/apps/client/src/widgets/layout/NoteBadges.tsx@@ -1,6 +1,6 @@import "./NoteBadges.css";-import { isOfficeMimeType } from "@triliumnext/commons";+import { isOfficeMimeType, safeHostname } from "@triliumnext/commons";import { clsx } from "clsx";import { useCallback, useEffect, useState } from "preact/hooks";@@ -22,6 +22,7 @@ import {useGetContextDataFrom, useIsNoteReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteLabelByName, useNoteProperty,useTriliumEvent, useTriliumOption} from "../react/hooks";+import Icon from "../react/Icon";import { ActiveContentBadges } from "./ActiveContentBadges";import { SnippetBadge } from "./SnippetBadge";@@ -136,10 +137,14 @@ const SHARE_BADGES: Record<ShareScope, { icon: string; text: string; tooltip?: s}};-function ClippedNoteBadge() {+export function ClippedNoteBadge() {const { note } = useNoteContext();const isHelpNote = !!note?.noteId.startsWith("_help");const [ url ] = useNoteLabel(note, isHelpNote ? "docUrl" : "pageUrl");+ const [ description ] = useNoteLabel(note, "pageDescription");+ const [ author ] = useNoteLabel(note, "pageAuthor");+ const [ published ] = useNoteLabel(note, "publishedDate");+ const [ modified ] = useNoteLabel(note, "modifiedDate");if (!url) return;@@ -155,14 +160,53 @@ function ClippedNoteBadge() {);}+ const fields = [+ { icon: "bx bx-user", label: t("breadcrumb_badges.clipped_note_author"), value: author },+ { icon: "bx bx-calendar", label: t("breadcrumb_badges.clipped_note_published"), value: published },+ { icon: "bx bx-history", label: t("breadcrumb_badges.clipped_note_modified"), value: modified }+ ].filter((field) => field.value);++ if (!description && !fields.length) {+ return (+ <Badge+ className="clipped-note-badge"+ icon="bx bx-globe"+ text={t("breadcrumb_badges.clipped_note")}+ tooltip={t("breadcrumb_badges.clipped_note_description", { url })}+ href={url}+ />+ );+ }+return (- <Badge+ <BadgeWithDropdown+ panelclassName="clipped-note-badge"icon="bx bx-globe"text={t("breadcrumb_badges.clipped_note")}- tooltip={t("breadcrumb_badges.clipped_note_description", { url })}- href={url}- />+ tooltip={t("breadcrumb_badges.clipped_note_details")}+ dropdownProps={{ dropdownContainerClassName: "clipped-note-details-panel" }}+ >+ <div className="clipped-note-details">+ <div className="clipped-note-source">+ <Icon icon="bx bx-globe" />+ <a className="tn-link external" href={url} target="_blank" rel="noopener noreferrer">+ {safeHostname(url)}+ </a>+ </div>+ {description && <p className="clipped-note-description pre-wrap-text">{description}</p>}+ {fields.length > 0 && (+ <dl className="clipped-note-fields">+ {fields.map((field) => (+ <div key={field.icon}>+ <dt><Icon icon={field.icon} />{field.label}</dt>+ <dd>{field.value}</dd>+ </div>+ ))}+ </dl>+ )}+ </div>+ </BadgeWithDropdown>);}diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsxindex 6b68b100d2..f6bda49437 100644--- a/apps/client/src/widgets/react/Badge.tsx+++ b/apps/client/src/widgets/react/Badge.tsx@@ -7,7 +7,7 @@ import { useRef } from "preact/hooks";import { isDefinitionName } from "../../entities/fattribute";import { escapeHtml } from "../../services/utils";import { attributeKindIcon } from "../attribute_widgets/attribute_types";-import Dropdown, { DropdownProps } from "./Dropdown";+import Dropdown, { DropdownPanel, DropdownPanelProps } from "./Dropdown";import { useStaticTooltip } from "./hooks";import Icon from "./Icon";import RawHtml from "./RawHtml";@@ -144,12 +144,15 @@ function htmlToText(html: string) {return template.content.textContent ?? "";}-export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, ...props }: BadgeProps & {+export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, panel, ...props }: BadgeProps & {children?: ComponentChildren,- dropdownProps?: Partial<DropdownProps>+ dropdownProps?: Partial<DropdownPanelProps>,+ /** Opens a {@link DropdownPanel} instead of a menu, for content other than menu rows. */+ panel?: boolean}) {+ const Toggle = panel ? DropdownPanel : Dropdown;return (- <Dropdown+ <ToggleclassName={`dropdown-badge dropdown-${className}`}text={<Badgetext={<>@@ -165,6 +168,6 @@ export function BadgeWithDropdown({ text, children, tooltip, className, dropdowntitlePosition="bottom"{...dropdownProps}placement="bottom"- >{children}</Dropdown>+ >{children}</Toggle>);}diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.htmlindex 1afee4ca60..1ed01ade94 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.html@@ -20,7 +20,9 @@which will indicate if the note was clipped using the <a class="reference-link"href="#root/_help_MtPxeAWVAzMg">Web Clipper</a>. The badge acts as a link,so it can be clicked on to navigate to the page or right clicked for more- options.</li>+ options. When the clipper also saved the page's description, its author,+ or the dates it was published and last updated, clicking the badge shows+ these details instead, with a link to the page at the top.</li><li><span class="tn-icon bx bx-play"></span> <strong>Execute badge</strong>,for <a href="#root/_help_CdNpE2pqjmI6">scripts</a> or <a href="#root/_help_YKWqdJhzi2VY">saved SQL queries</a> whichhave an execute button or a description.</li>diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.mdindex 4771f41192..fc7fb41db6 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Note badges.md@@ -7,6 +7,6 @@ The following badges are available:* <span class="tn-icon bx bx-lock-alt"></span> **Read-only badge**, which will be shown if the note is not editable due to either automatic read-only or manual read-only. Clicking on the badge will temporarily edit the note.* <span class="tn-icon bx bx-share-alt"></span> **Share badge**, which will indicate that the current note is shared. The badge will also indicate if the share is on the local network (for the desktop application without <a class="reference-link" href="../../Installation%20%26%20Setup/Synchronization.md">Synchronization</a> set up) or publicly accessible (for the server).-* <span class="tn-icon bx bx-globe"></span> **Web clip badge**, which will indicate if the note was clipped using the <a class="reference-link" href="../../Installation%20%26%20Setup/Web%20Clipper.md">Web Clipper</a>. The badge acts as a link, so it can be clicked on to navigate to the page or right clicked for more options.+* <span class="tn-icon bx bx-globe"></span> **Web clip badge**, which will indicate if the note was clipped using the <a class="reference-link" href="../../Installation%20%26%20Setup/Web%20Clipper.md">Web Clipper</a>. The badge acts as a link, so it can be clicked on to navigate to the page or right clicked for more options. When the clipper also saved the page's description, its author, or the dates it was published and last updated, clicking the badge shows these details instead, with a link to the page at the top.* <span class="tn-icon bx bx-play"></span> **Execute badge**, for [scripts](../../Scripting.md) or [saved SQL queries](../../Advanced%20Usage/Database/Manually%20altering%20the%20database/SQL%20Console.md) which have an execute button or a description.* For <a class="reference-link" href="../Active%20content.md">Active content</a> (e.g. scripts, styles), a dedicated badge will be shown indicating the type of note and with an option to easily enable or disable it.\ No newline at end of filediff --git a/packages/commons/src/lib/builtin_attributes.ts b/packages/commons/src/lib/builtin_attributes.tsindex dcabb85d5a..7256e7a28d 100644--- a/packages/commons/src/lib/builtin_attributes.ts+++ b/packages/commons/src/lib/builtin_attributes.ts@@ -210,6 +210,11 @@ const BUILTIN_ATTRIBUTES = [{ type: "label", name: "language", valueType: "text", hasUserValue: true },{ type: "label", name: "originalFileName", valueType: "text", hasUserValue: true },{ type: "label", name: "pageUrl", valueType: "url", hasUserValue: true },+ // Metadata the web clipper saves with a page; shown in the "Web clip" badge's details.+ { type: "label", name: "pageDescription", valueType: "text", hasUserValue: true },+ { type: "label", name: "pageAuthor", valueType: "text", hasUserValue: true },+ { type: "label", name: "publishedDate", valueType: "date", hasUserValue: true },+ { type: "label", name: "modifiedDate", valueType: "date", hasUserValue: true },{ type: "label", name: "command", valueType: "text", hasUserValue: true },// Which half of the split editor a code-like note opens on; without it the read-only state decides.{ type: "label", name: "displayMode", valueType: "select", hasUserValue: true, selectOptions: [