Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): keep colons in promoted attribute names

hippi345737ffe6661ca
diff --git a/apps/client/src/widgets/attribute_widgets/UserAttributesList.spec.tsx b/apps/client/src/widgets/attribute_widgets/UserAttributesList.spec.tsx
index b5b9795960..1eddd55e89 100644
--- a/apps/client/src/widgets/attribute_widgets/UserAttributesList.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/UserAttributesList.spec.tsx
@@ -72,6 +72,14 @@ describe("UserAttributesDisplay", () => {
expect(values()).toEqual([ "Ada", "red", "green" ]);
});
+ it("draws an attribute whose name contains a colon under the full colon-carrying name", () => {
+ const note = noteWith({ "sometool:parameter": [ "value1" ] });
+
+ draw({ note });
+
+ expect(values()).toEqual([ "value1" ]);
+ });
+
it("leaves out what it is told to ignore, whichever way it is ordered", () => {
draw({ ignoredAttributes: [ "stage" ], shownAttributes: [ "stage", "owner" ] });
diff --git a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx
index 52f630948c..df0e284c39 100644
--- a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx
+++ b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx
@@ -1,5 +1,6 @@
import "./UserAttributesList.css";
+import { extractAttributeDefinitionTypeAndName } from "@triliumnext/commons";
import type { DefinitionObject } from "@triliumnext/commons";
import { ComponentChildren, CSSProperties } from "preact";
import { useEffect, useState } from "preact/hooks";
@@ -142,7 +143,7 @@ function getAttributesWithDefinitions(
const result: AttributeWithDefinitions[] = [];
for (const attr of attributeDefintions) {
const def = attr.getDefinition();
- const [ type, name ] = attr.name.split(":", 2);
+ const [ type, name ] = extractAttributeDefinitionTypeAndName(attr.name);
const friendlyName = def?.promotedAlias || name;
const props: Omit<AttributeWithDefinitions, "value"> = { def, name, type, friendlyName };
diff --git a/apps/client/src/widgets/collections/board/group_by.tsx b/apps/client/src/widgets/collections/board/group_by.tsx
index 076a28c7dc..a3f1be7f45 100644
--- a/apps/client/src/widgets/collections/board/group_by.tsx
+++ b/apps/client/src/widgets/collections/board/group_by.tsx
@@ -1,4 +1,4 @@
-import { DEFAULT_BOARD_GROUP_BY, normalizeBoardGroupBy } from "@triliumnext/commons";
+import { DEFAULT_BOARD_GROUP_BY, extractAttributeDefinitionTypeAndName, normalizeBoardGroupBy } from "@triliumnext/commons";
import { createPortal } from "preact";
import { useCallback, useRef, useState } from "preact/hooks";
@@ -75,7 +75,7 @@ export default function BoardGroupBy({ note, options, current, onSelect }: {
/** Writes the definition the editor was left holding, and groups by it. */
const save = useCallback(async () => {
const definition = edited.current;
- const [ , name ] = definition?.name.split(":", 2) ?? [];
+ const [ , name ] = definition ? extractAttributeDefinitionTypeAndName(definition.name) : [];
if (!definition || !name) {
setDetail(null);
return;
diff --git a/apps/client/src/widgets/collections/promoted_attributes.spec.ts b/apps/client/src/widgets/collections/promoted_attributes.spec.ts
index a199a5d5d1..c2e95d090f 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.spec.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.spec.ts
@@ -144,6 +144,15 @@ describe("resolvePromotedAttributes", () => {
expect(resolvePromotedAttributes(undefined, undefined)).toEqual([]);
});
+ it("preserves colons in the attribute name when the definition name contains multiple colons", () => {
+ const resolved = resolvePromotedAttributes(
+ collection([ definition("label:sometool:parameter") ]), undefined);
+
+ expect(resolved).toHaveLength(1);
+ expect(resolved[0].name).toBe("sometool:parameter");
+ expect(resolved[0].type).toBe("label");
+ });
+
/** A definition names its kind before the colon; anything else is not one to draw. */
it("passes over a definition of a kind it cannot draw", () => {
const resolved = resolvePromotedAttributes(
diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.ts
index 5cd300384c..b9f411286b 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.ts
@@ -89,7 +89,9 @@ export function resolvePromotedAttributes(
const defined = new Map<string, PromotedAttribute>();
const add = (definition: FAttribute, isDefinedByItems: boolean) => {
- const [ type, name ] = definition.name.split(":", 2);
+ const firstColon = definition.name.indexOf(":");
+ const type = firstColon >= 0 ? definition.name.substring(0, firstColon) : definition.name;
+ const name = firstColon >= 0 ? definition.name.substring(firstColon + 1) : "";
if ((type !== "label" && type !== "relation") || !name
|| (!isDefinedByItems && !definition.isInheritable)) {
return;
diff --git a/apps/client/src/widgets/collections/table/col_editing.ts b/apps/client/src/widgets/collections/table/col_editing.ts
index dbbd106894..bb16d99099 100644
--- a/apps/client/src/widgets/collections/table/col_editing.ts
+++ b/apps/client/src/widgets/collections/table/col_editing.ts
@@ -1,5 +1,6 @@
import { useLegacyImperativeHandlers } from "../../react/hooks";
import { Attribute } from "../../../services/attribute_parser";
+import { extractAttributeDefinitionTypeAndName } from "@triliumnext/commons";
import { RefObject } from "preact";
import { Tabulator } from "tabulator-tables";
import { useRef } from "preact/hooks";
@@ -73,9 +74,9 @@ export default function useColTableEditing(api: RefObject<Tabulator | null>, att
const isRename = (existingAttributeToEdit.current && existingAttributeToEdit.current.name !== name);
try {
if (isRename) {
- const oldName = existingAttributeToEdit.current!.name.split(":")[1];
- const [ type, newName ] = name.split(":");
- await renameColumn(parentNote.noteId, type as "label" | "relation", oldName, newName);
+ const [ , oldName ] = extractAttributeDefinitionTypeAndName(existingAttributeToEdit.current!.name);
+ const [ type, newName ] = extractAttributeDefinitionTypeAndName(name);
+ await renameColumn(parentNote.noteId, type, oldName, newName);
}
if (existingAttributeToEdit.current && (isRename || existingAttributeToEdit.current.isInheritable !== isInheritable)) {
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
index 531e33460e..6fc23f45f9 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
@@ -3,6 +3,7 @@ import "./PromotedAttributesCard.css";
import { createPortal } from "preact";
import { useCallback, useMemo, useRef, useState } from "preact/hooks";
+import { extractAttributeDefinitionTypeAndName } from "@triliumnext/commons";
import type FNote from "../../entities/fnote";
import type { Attribute } from "../../services/attribute_parser";
import attributes, { removeOwnedAttributesByNameOrType } from "../../services/attributes";
@@ -177,10 +178,10 @@ export default function PromotedAttributesCard({
return;
}
- const [ type, name ] = definition.name.split(":", 2) as [ "label" | "relation", string ];
+ const [ type, name ] = extractAttributeDefinitionTypeAndName(definition.name);
const isRenamed = !!was && was.name !== definition.name;
if (isRenamed && was) {
- const [ , previous ] = was.name.split(":", 2);
+ const [ , previous ] = extractAttributeDefinitionTypeAndName(was.name);
await renameAttributeInSubtree(note.noteId, type, previous, name);
}