Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): restrict note reference triggers to text boundaries

tianma-if1e6a355bb52a
diff --git a/apps/web/src/components/editor/NoteLinkSuggestion.test.mjs b/apps/web/src/components/editor/NoteLinkSuggestion.test.mjs
index 85090ff9..2a69b5e4 100644
--- a/apps/web/src/components/editor/NoteLinkSuggestion.test.mjs
+++ b/apps/web/src/components/editor/NoteLinkSuggestion.test.mjs
@@ -1,10 +1,12 @@
import { describe, expect, test } from "bun:test";
import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
+import { TextSelection } from "@tiptap/pm/state";
import { ensureTestWindowDom } from "../../lib/restore-test-global.mjs";
import {
filterNoteLinkSuggestions,
insertSuggestedNoteLink,
+ createNoteLinkSuggestionExtension,
} from "./NoteLinkSuggestion.tsx";
const labels = {
@@ -23,6 +25,63 @@ const memo = (overrides) => ({
});
describe("note link suggestion", () => {
+ const suggestionFor = (content) => {
+ ensureTestWindowDom();
+ const editor = new Editor({
+ extensions: [StarterKit, createNoteLinkSuggestionExtension({
+ getCurrentMemoId: () => null,
+ getLabels: () => labels,
+ searchMemos: async () => [],
+ })],
+ content: { type: "doc", content: [{ type: "paragraph", content }] },
+ });
+ try {
+ const initialState = editor.state.reconfigure({ plugins: editor.extensionManager.plugins });
+ const plugin = initialState.plugins.find((item) => item.key.startsWith("edgeever-note-link-suggestion$"));
+ const state = initialState.apply(initialState.tr.setSelection(TextSelection.atEnd(initialState.doc)));
+ return plugin.getState(state);
+ } finally {
+ editor.destroy();
+ }
+ };
+
+ test("does not open for @ embedded in passwords, emails, or ordinary text", () => {
+ for (const text of ["secret+@F(", "[email protected]", "文字@笔记", "hello@"]) {
+ expect(suggestionFor([{ type: "text", text }]).active).toBe(false);
+ }
+ });
+
+ test("opens at paragraph starts and after whitespace, including multiword queries", () => {
+ for (const [text, query] of [["@", ""], ["@项目", "项目"], ["参考 @项目 笔记", "项目 笔记"], ["参考\t@项目", "项目"], ["参考\u3000@项目", "项目"]]) {
+ const suggestion = suggestionFor([{ type: "text", text }]);
+ expect(suggestion.active).toBe(true);
+ expect(suggestion.query).toBe(query);
+ }
+ });
+
+ test("formatting does not turn an embedded @ into a paragraph-start trigger", () => {
+ expect(suggestionFor([
+ { type: "text", text: "secret+" },
+ { type: "text", text: "@F(", marks: [{ type: "bold" }] },
+ ]).active).toBe(false);
+ expect(suggestionFor([
+ { type: "text", text: "参考 " },
+ { type: "text", text: "@项目", marks: [{ type: "bold" }] },
+ ]).active).toBe(true);
+ });
+
+ test("opens after a hard break but not immediately after inline code", () => {
+ expect(suggestionFor([
+ { type: "text", text: "参考" },
+ { type: "hardBreak" },
+ { type: "text", text: "@项目" },
+ ]).active).toBe(true);
+ expect(suggestionFor([
+ { type: "text", text: "user", marks: [{ type: "code" }] },
+ { type: "text", text: "@example.com" },
+ ]).active).toBe(false);
+ });
+
test("excludes the current and deleted notes", () => {
expect(filterNoteLinkSuggestions([
memo({ id: "current" }),
diff --git a/apps/web/src/components/editor/NoteLinkSuggestion.tsx b/apps/web/src/components/editor/NoteLinkSuggestion.tsx
index a4597f66..7a16b746 100644
--- a/apps/web/src/components/editor/NoteLinkSuggestion.tsx
+++ b/apps/web/src/components/editor/NoteLinkSuggestion.tsx
@@ -147,7 +147,13 @@ export const createNoteLinkSuggestionExtension = ({
decorationClass: "edgeever-note-link-query",
allow: ({ state, range }) => {
const position = state.doc.resolve(range.from);
- return this.editor.isEditable && position.parent.type.name === "paragraph";
+ // Check the paragraph boundary, since formatting can split a word into text nodes.
+ const prefix = position.parent.textBetween(
+ Math.max(0, position.parentOffset - 1), position.parentOffset, "\n",
+ (node) => node.type.name === "hardBreak" ? "\n" : "\ufffc",
+ );
+ return this.editor.isEditable && position.parent.type.name === "paragraph"
+ && (position.parentOffset === 0 || /\s/u.test(prefix));
},
items: async ({ query }) => {
const currentMemoId = getCurrentMemoId();