Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): title reference links in the chat, attribute and memo editors

Elian Doran11d0f05af669
diff --git a/apps/client/src/widgets/attribute_widgets/AttributeEditor.spec.tsx b/apps/client/src/widgets/attribute_widgets/AttributeEditor.spec.tsx
index 1f3fa9491f..6f816a00b1 100644
--- a/apps/client/src/widgets/attribute_widgets/AttributeEditor.spec.tsx
+++ b/apps/client/src/widgets/attribute_widgets/AttributeEditor.spec.tsx
@@ -1,7 +1,8 @@
import type { ModelPosition } from "@triliumnext/ckeditor5";
import { describe, expect, it } from "vitest";
-import { getClickIndex } from "./AttributeEditor";
+import link from "../../services/link";
+import { buildAttributeEditorConfig, getClickIndex } from "./AttributeEditor";
describe("getClickIndex", () => {
it("measures every sibling before the pressed one as the text it stands for", () => {
@@ -24,3 +25,13 @@ describe("getClickIndex", () => {
expect(getClickIndex({ offset: 7, textNode: null } as unknown as ModelPosition)).toBe(7);
});
});
+
+describe("buildAttributeEditorConfig", () => {
+ it("titles the notes picked as relation targets", () => {
+ const referenceLink = buildAttributeEditorConfig([]).referenceLink;
+
+ expect(referenceLink?.getTitle).toBe(link.getReferenceLinkTitle);
+ expect(referenceLink?.getTitleSync).toBe(link.getReferenceLinkTitleSync);
+ expect(referenceLink?.renderTitle).toBeTypeOf("function");
+ });
+});
diff --git a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
index d2691b12a3..29c4b89591 100644
--- a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
+++ b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
@@ -1,6 +1,6 @@
import "./AttributeEditor.css";
-import type { AttributeEditor as CKEditorAttributeEditor, ModelElement, ModelNode, ModelPosition, MentionHostedFeed } from "@triliumnext/ckeditor5";
+import type { AttributeEditor as CKEditorAttributeEditor, EditorConfig, ModelElement, ModelNode, ModelPosition, MentionHostedFeed } from "@triliumnext/ckeditor5";
import { AttributeType } from "@triliumnext/commons";
import clsx from "clsx";
import { createPortal, RefObject } from "preact";
@@ -243,14 +243,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
tabIndex={200}
editor={editorClass}
currentValue={currentValue}
- config={{
- toolbar: { items: [] },
- placeholder: t("attribute_editor.placeholder"),
- mention: { feeds: [], hostedFeeds: hostedMentions },
- referenceLink: { renderTitle: renderReferenceTitle },
- licenseKey: "GPL",
- language: "en"
- }}
+ config={buildAttributeEditorConfig(hostedMentions)}
onChange={(currentValue) => {
currentValueRef.current = currentValue ?? "";
@@ -370,6 +363,22 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
);
}
+/** The config of the attribute editor, which completes what is typed from `hostedMentions`. */
+export function buildAttributeEditorConfig(hostedMentions: MentionHostedFeed[]): EditorConfig {
+ return {
+ toolbar: { items: [] },
+ placeholder: t("attribute_editor.placeholder"),
+ mention: { feeds: [], hostedFeeds: hostedMentions },
+ referenceLink: {
+ getTitle: link.getReferenceLinkTitle,
+ getTitleSync: link.getReferenceLinkTitleSync,
+ renderTitle: renderReferenceTitle
+ },
+ licenseKey: "GPL",
+ language: "en"
+ };
+}
+
/** Fills `element` with the title of the note a relation's reference link points to. */
async function renderReferenceTitle(element: HTMLElement, href: string) {
const { noteId } = link.parseNavigationStateFromUrl(href);
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx
index 4e0b07df56..a367d61f8b 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx
@@ -4,8 +4,9 @@ import { describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn() }));
vi.mock("../../../components/app_context.js", () => ({ default: { triggerEvent: mocks.triggerEvent } }));
+import link from "../../../services/link";
import { renderInto } from "../../../test/render";
-import { PendingAttachmentChip } from "./ChatInputBar";
+import { buildChatInputConfig, PendingAttachmentChip } from "./ChatInputBar";
import type { AttachmentBlock } from "./useLlmChat";
const image: AttachmentBlock = { type: "image", attachmentId: "img", mime: "image/png", title: "cat.png", url: "api/attachments/img/image/cat.png" };
@@ -42,3 +43,13 @@ describe("PendingAttachmentChip", () => {
expect(chip?.querySelector<HTMLButtonElement>(".llm-chat-attachment-remove")?.disabled).toBe(true);
});
});
+
+describe("buildChatInputConfig", () => {
+ it("titles the notes picked from the @ list, so the message names them", () => {
+ const referenceLink = buildChatInputConfig([]).referenceLink;
+
+ expect(referenceLink?.getTitle).toBe(link.getReferenceLinkTitle);
+ expect(referenceLink?.getTitleSync).toBe(link.getReferenceLinkTitleSync);
+ expect(referenceLink?.renderTitle).toBeTypeOf("function");
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index 2e2d740423..5cb59e4e2e 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -1,6 +1,6 @@
import "./ChatInputBar.css";
-import type { AttributeEditor as CKEditorAttributeEditor, CKTextEditor, MentionHostedFeed } from "@triliumnext/ckeditor5";
+import type { AttributeEditor as CKEditorAttributeEditor, CKTextEditor, EditorConfig, MentionHostedFeed } from "@triliumnext/ckeditor5";
import type { DISPLAYABLE_LOCALE_IDS, LlmPermissionMode, LlmReasoningEffort } from "@triliumnext/commons";
import { Fragment } from "preact";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
@@ -303,19 +303,7 @@ export default function ChatInputBar({
apiRef={editorApiRef}
className="llm-chat-input"
editor={ckEditor.editor}
- config={{
- // Markdown autoformatting (block quotes, code fences, lists, links) without a toolbar.
- extraPlugins: ckEditor.plugins,
- toolbar: { items: [] },
- placeholder: t("llm_chat.placeholder"),
- mention: { feeds: [], hostedFeeds: hostedMentions },
- referenceLink: {
- renderTitle: (element, href, storedTitle) => {
- void link.loadReferenceLinkTitle($(element), href, undefined, storedTitle);
- }
- },
- licenseKey: "GPL"
- }}
+ config={buildChatInputConfig(ckEditor.plugins)}
// The strings the box shows of its own — the link balloon it raises on Ctrl+K —
// in the language the rest of Trilium is read in; the dictionary is fetched for
// it before the editor is raised.
@@ -565,6 +553,25 @@ export default function ChatInputBar({
);
}
+/** The config of the chat input's editor, which loads `plugins` on top of its own. */
+export function buildChatInputConfig(plugins: EditorConfig["extraPlugins"]): EditorConfig {
+ return {
+ // Markdown autoformatting (block quotes, code fences, lists, links) without a toolbar.
+ extraPlugins: plugins,
+ toolbar: { items: [] },
+ placeholder: t("llm_chat.placeholder"),
+ mention: { feeds: [], hostedFeeds: hostedMentions },
+ referenceLink: {
+ getTitle: link.getReferenceLinkTitle,
+ getTitleSync: link.getReferenceLinkTitleSync,
+ renderTitle: (element, href, storedTitle) => {
+ void link.loadReferenceLinkTitle($(element), href, undefined, storedTitle);
+ }
+ },
+ licenseKey: "GPL"
+ };
+}
+
/** A pending attachment above the input, marked when the selected model can't read it. */
export function PendingAttachmentChip({ att, reason, onRemove, disabled }: {
att: AttachmentBlock;
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
index a3606edd27..e7df310fa6 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
@@ -1,7 +1,8 @@
import type { NodeObj, Topic } from "mind-elixir";
import { describe, expect, it } from "vitest";
-import { getNodeMemo, MEMO_MARKER_ATTRIBUTE, renderMemoMarkers, toMemoHtml } from "./NodeMemo";
+import link from "../../../services/link";
+import { buildMemoEditorConfig, getNodeMemo, MEMO_MARKER_ATTRIBUTE, renderMemoMarkers, toMemoHtml } from "./NodeMemo";
function buildNode(node: Partial<NodeObj> = {}): NodeObj {
return { id: "n1", topic: "Node", ...node };
@@ -105,3 +106,12 @@ describe("renderMemoMarkers", () => {
expect(container.querySelector("me-tpc")?.hasAttribute(MEMO_MARKER_ATTRIBUTE)).toBe(false);
});
});
+
+describe("buildMemoEditorConfig", () => {
+ it("titles the reference links a memo holds", () => {
+ const referenceLink = buildMemoEditorConfig().referenceLink;
+
+ expect(referenceLink?.getTitle).toBe(link.getReferenceLinkTitle);
+ expect(referenceLink?.getTitleSync).toBe(link.getReferenceLinkTitleSync);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
index 859a99d34f..dd7df970aa 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
@@ -6,6 +6,7 @@ import type { NodeObj, Topic } from "mind-elixir";
import { useEffect, useLayoutEffect, useRef } from "preact/hooks";
import { t } from "../../../services/i18n";
+import link from "../../../services/link";
import { escapeHtml } from "../../../services/utils";
import CKEditor, { type CKEditorApi } from "../../react/CKEditor";
import { useTriliumOption } from "../../react/hooks";
@@ -112,11 +113,15 @@ export default function NodeMemo({ selectionKey, memo, readOnly, onCommit }: {
* dictionary has to be fetched, which is done for the field by the wrapper that raises the editor
* (see the `uiLanguage` it is handed).
*/
-function buildMemoEditorConfig(): EditorConfig {
+export function buildMemoEditorConfig(): EditorConfig {
return {
extraPlugins: MEMO_PLUGINS,
toolbar: { items: MEMO_TOOLBAR_ITEMS },
placeholder: t("mind-map.memo-placeholder"),
+ referenceLink: {
+ getTitle: link.getReferenceLinkTitle,
+ getTitleSync: link.getReferenceLinkTitleSync
+ },
licenseKey: "GPL"
};
}