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
client/content manager: add support for extra content types
Adorian Doranc9ba5a011db4
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex 3e50071b1e..0804f5ec34 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -70,13 +70,16 @@ describe("CONTENT_CATEGORIES", () => {expect(CONTENT_CATEGORIES.map((category) => category.id)).toEqual(["backendScripts","frontendScripts",+ "eventHandlers",+ "endpoints","widgets","renderNotes","themes","customCss","iconPacks","templates",- "snippets"+ "snippets",+ "sharing"]);});});@@ -210,6 +213,46 @@ describe("resolveProperties", () => {expect(resolveProperties(plain, categoryById("templates"))).toEqual([]);});+ it("matches relations, listing every event a note handles", () => {+ const note = buildNote({+ title: "Handler",+ "~runOnNoteCreation": "script1",+ "~disabled:runOnBranchDeletion": "script2"+ });++ expect(resolveProperties(note, categoryById("eventHandlers"))).toEqual([ {+ titleKey: "content_manager.property_event",+ values: [+ { titleKey: "content_manager.event_note_creation" },+ { titleKey: "content_manager.event_branch_deletion" }+ ]+ } ]);+ });++ it("does not confuse a label with a relation of the same name", () => {+ // `widget` exists as both; a relation condition must not be satisfied by the label.+ const category: ContentCategory = {+ id: "test",+ titleKey: "unused",+ filter: "~widget",+ properties: [ { titleKey: "prop.k", values: [ { titleKey: "v.rel", condition: { relation: "widget" } } ] } ]+ };++ expect(resolveProperties(buildNote({ title: "L", "#widget": "" }), category)).toEqual([]);+ expect(resolveProperties(buildNote({ title: "R", "~widget": "abc" }), category)).toEqual([+ { titleKey: "prop.k", values: [ { titleKey: "v.rel" } ] }+ ]);+ });++ it("shows an endpoint's kind and its path pattern", () => {+ const note = buildNote({ title: "API", "#customRequestHandler": "api/my-handler/([a-z]+)" });++ expect(resolveProperties(note, categoryById("endpoints"))).toEqual([+ { titleKey: "content_manager.property_kind", values: [ { titleKey: "content_manager.endpoint_request_handler" } ] },+ { titleKey: "content_manager.property_path", values: [ { text: "api/my-handler/([a-z]+)" } ] }+ ]);+ });+it("returns nothing for a category that declares no properties", () => {expect(resolveProperties(buildNote({ title: "X", "#appCss": "" }), categoryById("customCss"))).toEqual([]);});diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex c8d1cab51e..cae5c2d507 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -11,12 +11,15 @@ export interface ContentTrigger {/** How the notes within each category are ordered. */export type ContentSortOrder = "title" | "dateCreated";-/** Matched against the note's own labels, reading through the `disabled:` prefix. */+/** Matched against the note's own attributes, reading through the `disabled:` prefix. */export interface ContentPropertyCondition {- label: string;- /** Matches when the label carries any of these values. */+ /** Label to match. Set either this or {@link relation}, not both. */+ label?: string;+ /** Relation to match, e.g. an event handler. */+ relation?: string;+ /** Matches when the attribute carries any of these values. */is?: string | string[];- /** Matches when the label exists and carries none of these values. */+ /** Matches when the attribute exists and carries none of these values. */isNot?: string | string[];}@@ -51,6 +54,25 @@ export interface ContentCategory {filter: string;}+/**+ * The event relations that actually fire, paired with how they read in the UI.+ *+ * `runOnNoteDeletion` is deliberately absent: it is declared in `BUILTIN_ATTRIBUTES` but nothing in+ * `handlers.ts` subscribes to it, so listing it would advertise a trigger that never runs.+ */+const EVENT_HANDLERS = [+ { relation: "runOnNoteCreation", titleKey: "content_manager.event_note_creation" },+ { relation: "runOnNoteTitleChange", titleKey: "content_manager.event_note_title_change" },+ { relation: "runOnNoteChange", titleKey: "content_manager.event_note_change" },+ { relation: "runOnNoteContentChange", titleKey: "content_manager.event_note_content_change" },+ { relation: "runOnChildNoteCreation", titleKey: "content_manager.event_child_note_creation" },+ { relation: "runOnAttributeCreation", titleKey: "content_manager.event_attribute_creation" },+ { relation: "runOnAttributeChange", titleKey: "content_manager.event_attribute_change" },+ { relation: "runOnBranchCreation", titleKey: "content_manager.event_branch_creation" },+ { relation: "runOnBranchChange", titleKey: "content_manager.event_branch_change" },+ { relation: "runOnBranchDeletion", titleKey: "content_manager.event_branch_deletion" }+];+export const CONTENT_CATEGORIES: ContentCategory[] = [{id: "backendScripts",@@ -87,6 +109,40 @@ export const CONTENT_CATEGORIES: ContentCategory[] = []} ]},+ {+ id: "eventHandlers",+ titleKey: "content_manager.category_event_handlers",+ // The relation lives on the note the handler is attached to, not on the script it runs, so+ // these rows are the notes whose changes trigger something.+ filter: EVENT_HANDLERS.flatMap(({ relation }) => [ `~${relation}`, `~disabled:${relation}` ]).join(" OR "),+ properties: [ {+ titleKey: "content_manager.property_event",+ values: EVENT_HANDLERS.map(({ relation, titleKey }) => ({ titleKey, condition: { relation } }))+ } ]+ },+ {+ id: "endpoints",+ titleKey: "content_manager.category_endpoints",+ filter: "#customRequestHandler OR #customResourceProvider"+ + " OR #disabled:customRequestHandler OR #disabled:customResourceProvider",+ properties: [+ {+ titleKey: "content_manager.property_kind",+ values: [+ { titleKey: "content_manager.endpoint_request_handler", condition: { label: "customRequestHandler" } },+ { titleKey: "content_manager.endpoint_resource_provider", condition: { label: "customResourceProvider" } }+ ]+ },+ {+ // The label's value is the regular expression matched against the request path.+ titleKey: "content_manager.property_path",+ values: [+ { valueOfLabel: "customRequestHandler" },+ { valueOfLabel: "customResourceProvider" }+ ]+ }+ ]+ },{id: "widgets",titleKey: "content_manager.category_widgets",@@ -134,6 +190,21 @@ export const CONTENT_CATEGORIES: ContentCategory[] = [id: "snippets",titleKey: "content_manager.category_snippets",filter: "#snippet OR #textSnippet OR #disabled:snippet OR #disabled:textSnippet"+ },+ {+ id: "sharing",+ titleKey: "content_manager.category_sharing",+ filter: "#shareRaw OR ~shareJs OR ~shareHtml OR ~shareTemplate"+ + " OR #disabled:shareRaw OR ~disabled:shareJs OR ~disabled:shareHtml OR ~disabled:shareTemplate",+ properties: [ {+ titleKey: "content_manager.property_kind",+ values: [+ { titleKey: "content_manager.share_raw", condition: { label: "shareRaw" } },+ { titleKey: "content_manager.share_js", condition: { relation: "shareJs" } },+ { titleKey: "content_manager.share_html", condition: { relation: "shareHtml" } },+ { titleKey: "content_manager.share_template", condition: { relation: "shareTemplate" } }+ ]+ } ]}];@@ -202,7 +273,7 @@ export function resolveProperties(note: FNote, category: ContentCategory): Resolvalues.push({ titleKey: value.titleKey });} else if (value.valueOfLabel) {// Several labels of the same name are possible, e.g. two `#run` triggers.- values.push(...getOwnedLabelValues(note, value.valueOfLabel)+ values.push(...getOwnedAttributeValues(note, value.valueOfLabel, "label").filter((text) => text.trim()).map((text) => ({ text })));}@@ -216,8 +287,10 @@ export function resolveProperties(note: FNote, category: ContentCategory): Resolreturn resolved;}-function matchesCondition(note: FNote, { label, is, isNot }: ContentPropertyCondition) {- const values = getOwnedLabelValues(note, label);+function matchesCondition(note: FNote, { label, relation, is, isNot }: ContentPropertyCondition) {+ const values = relation+ ? getOwnedAttributeValues(note, relation, "relation")+ : label ? getOwnedAttributeValues(note, label, "label") : [];if (!values.length) {return false;@@ -231,12 +304,12 @@ function matchesCondition(note: FNote, { label, is, isNot }: ContentPropertyCond}/**- * All values the note holds for a label, matched through the `disabled:` prefix so that switching an- * item off doesn't blank out the detail explaining what it does.+ * All values the note holds for an attribute, matched through the `disabled:` prefix so that+ * switching an item off doesn't blank out the detail explaining what it does.*/-function getOwnedLabelValues(note: FNote, name: string) {+function getOwnedAttributeValues(note: FNote, name: string, type: "label" | "relation") {return note.getOwnedAttributes()- .filter((attribute) => attribute.type === "label"+ .filter((attribute) => attribute.type === type&& attributes.getNameWithoutDangerousPrefix(attribute.name).toLowerCase() === name.toLowerCase()).map((attribute) => attribute.value);}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d636580cac..05d36e6a5a 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2609,6 +2609,9 @@"category_icon_packs": "Icon Packs","category_templates": "Templates","category_snippets": "Snippets",+ "category_event_handlers": "Event Handlers",+ "category_endpoints": "Custom Endpoints",+ "category_sharing": "Sharing","loading": "Retrieving content...","toggle_enable": "Enable","toggle_disable": "Disable",@@ -2623,6 +2626,25 @@"trigger_mobile_startup": "Mobile startup","trigger_hourly": "Hourly","trigger_daily": "Daily",+ "property_event": "Event",+ "property_kind": "Kind",+ "property_path": "Path",+ "event_note_creation": "Note created",+ "event_note_title_change": "Title changed",+ "event_note_change": "Note changed",+ "event_note_content_change": "Content changed",+ "event_child_note_creation": "Child note created",+ "event_attribute_creation": "Attribute created",+ "event_attribute_change": "Attribute changed",+ "event_branch_creation": "Branch created",+ "event_branch_change": "Branch changed",+ "event_branch_deletion": "Branch deleted",+ "endpoint_request_handler": "Request handler",+ "endpoint_resource_provider": "Resource provider",+ "share_raw": "Raw",+ "share_js": "JavaScript",+ "share_html": "HTML",+ "share_template": "Template","sort_order": "Sort by:","sort_by_title": "Title","sort_by_date_created": "Creation date",