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
fix(attributes): ignore definitions with an empty name
Elian Doran538050a2aef9
diff --git a/apps/client/src/entities/fattribute.spec.ts b/apps/client/src/entities/fattribute.spec.tsindex cc94f331b0..bcf556dfd1 100644--- a/apps/client/src/entities/fattribute.spec.ts+++ b/apps/client/src/entities/fattribute.spec.ts@@ -69,6 +69,9 @@ describe("FAttribute", () => {expect(makeAttribute(froca, { type: "label", name: "relation:foo", value: "" }).isDefinition()).toBe(true);expect(makeAttribute(froca, { type: "label", name: "foo", value: "" }).isDefinition()).toBe(false);expect(makeAttribute(froca, { type: "relation", name: "label:foo", value: "x" }).isDefinition()).toBe(false);+ // a prefix without a name defines nothing (#label:=promoted,single,text)+ expect(makeAttribute(froca, { type: "label", name: "label:", value: "promoted,single,text" }).isDefinition()).toBe(false);+ expect(makeAttribute(froca, { type: "label", name: "relation:", value: "promoted,single" }).isDefinition()).toBe(false);});it("getDefinition() parses the value via the promoted attribute definition parser", () => {diff --git a/apps/client/src/entities/fattribute.ts b/apps/client/src/entities/fattribute.tsindex bdccead31d..2d5ddbf264 100644--- a/apps/client/src/entities/fattribute.ts+++ b/apps/client/src/entities/fattribute.ts@@ -82,7 +82,7 @@ class FAttribute {}isDefinition() {- return this.type === "label" && (this.name.startsWith("label:") || this.name.startsWith("relation:"));+ return this.type === "label" && isDefinitionName(this.name);}getDefinition() {@@ -101,4 +101,14 @@ class FAttribute {}}+const DEFINITION_PREFIXES = [ "label:", "relation:" ];++/**+ * A definition needs an attribute name after its prefix: `#label:` on its own defines nothing, so it+ * is treated as an ordinary label rather than as a definition for a nameless attribute.+ */+export function isDefinitionName(name: string) {+ return DEFINITION_PREFIXES.some((prefix) => name.startsWith(prefix) && name.length > prefix.length);+}+export default FAttribute;diff --git a/apps/client/src/widgets/PromotedAttributes.tsx b/apps/client/src/widgets/PromotedAttributes.tsxindex 248e546881..ebf9f6eeb4 100644--- a/apps/client/src/widgets/PromotedAttributes.tsx+++ b/apps/client/src/widgets/PromotedAttributes.tsx@@ -421,7 +421,9 @@ function useTextLabelAutocomplete(inputId: string, valueAttr: Attribute, definit// Obtain data.useEffect(() => {- if (definition.labelType !== "text") {+ // A nameless attribute has no values to suggest, and would request `attribute-values/`, which+ // matches no route.+ if (definition.labelType !== "text" || !valueAttr.name) {return;}diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex 261f7615b2..744a2f530f 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -4,6 +4,7 @@ import type { DefinitionObject, LabelType, Multiplicity } from "@triliumnext/comimport { useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context.js";+import { isDefinitionName } from "../../entities/fattribute.js";import type { Attribute } from "../../services/attribute_parser.js";import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";@@ -792,10 +793,10 @@ type AttrType = "label" | "label-definition" | "relation" | "relation-definitionfunction getAttrType(attribute: Attribute): AttrType {if (attribute.type === "label") {- if (attribute.name.startsWith("label:")) {- return "label-definition";- } else if (attribute.name.startsWith("relation:")) {- return "relation-definition";+ // A bare prefix (`#label:`) defines nothing, so it stays an ordinary label — editing it as a+ // definition would only offer a name-less one back.+ if (isDefinitionName(attribute.name)) {+ return attribute.name.startsWith("label:") ? "label-definition" : "relation-definition";}return "label";} else if (attribute.type === "relation") {diff --git a/packages/trilium-core/src/becca/entities/battribute.spec.ts b/packages/trilium-core/src/becca/entities/battribute.spec.tsindex eb9c4dd395..34b4804ff1 100644--- a/packages/trilium-core/src/becca/entities/battribute.spec.ts+++ b/packages/trilium-core/src/becca/entities/battribute.spec.ts@@ -407,5 +407,21 @@ describe("BAttribute", () => {expect(attr.isDefinition()).toBe(false);expect(attr.getDefinedName()).toBe("plain");});++ it("treats a definition prefix without a name as not a definition", () => {+ const owner = createNote();+ const makeAttr = (name: string) => new BAttribute({+ attributeId: `attr-${randomString(8)}`,+ noteId: owner.noteId,+ type: "label",+ name,+ value: "promoted,single,text",+ position: 0,+ isInheritable: false+ });++ expect(makeAttr("label:").isDefinition()).toBe(false);+ expect(makeAttr("relation:").isDefinition()).toBe(false);+ });});});diff --git a/packages/trilium-core/src/becca/entities/battribute.ts b/packages/trilium-core/src/becca/entities/battribute.tsindex ee90b0e48a..48c5283360 100644--- a/packages/trilium-core/src/becca/entities/battribute.ts+++ b/packages/trilium-core/src/becca/entities/battribute.ts@@ -162,7 +162,7 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {}isDefinition() {- return this.type === "label" && (this.name.startsWith("label:") || this.name.startsWith("relation:"));+ return this.type === "label" && isDefinitionName(this.name);}getDefinition() {@@ -246,4 +246,14 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {}}+const DEFINITION_PREFIXES = [ "label:", "relation:" ];++/**+ * A definition needs an attribute name after its prefix: `#label:` on its own defines nothing, so it+ * is treated as an ordinary label rather than as a definition for a nameless attribute.+ */+export function isDefinitionName(name: string) {+ return DEFINITION_PREFIXES.some((prefix) => name.startsWith(prefix) && name.length > prefix.length);+}+export default BAttribute;