Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix(editor): expand tag autocomplete beyond heading markers (#6327)
ABird2148bf7a21c1
diff --git a/web/src/components/MemoEditor/Editor/markdownTagRanges.ts b/web/src/components/MemoEditor/Editor/markdownTagRanges.tsindex c0f924a00..f747278fe 100644--- a/web/src/components/MemoEditor/Editor/markdownTagRanges.ts+++ b/web/src/components/MemoEditor/Editor/markdownTagRanges.ts@@ -335,16 +335,3 @@ export function findMarkdownMentionMatches(state: EditorState, from: number, to:}return matches;}--/** Return the active tag whose recognized source span ends at the cursor. */-export function tagMatchBefore(state: EditorState, position: number): TagMatch | undefined {- const line = state.doc.lineAt(position);- const matches = findMarkdownTagMatches(state, line.from, position);- return matches.findLast((match) => match.to === position);-}--/** Whether an offset is in a literal-source run (used for explicit `#` completion). */-export function isLiteralTagPosition(state: EditorState, position: number): boolean {- const line = state.doc.lineAt(position);- return literalMarkdownSourceRanges(state, line.from, position).some((range) => range.to === position);-}diff --git a/web/src/components/MemoEditor/Editor/tagAutocomplete.ts b/web/src/components/MemoEditor/Editor/tagAutocomplete.tsindex 73c0d36af..e5cafd90b 100644--- a/web/src/components/MemoEditor/Editor/tagAutocomplete.ts+++ b/web/src/components/MemoEditor/Editor/tagAutocomplete.ts@@ -1,7 +1,7 @@import { autocompletion, type CompletionContext, type CompletionResult } from "@codemirror/autocomplete";+import { ensureSyntaxTree, syntaxTree } from "@codemirror/language";import type { Extension } from "@codemirror/state";-import { isTagIntroducerAt } from "@/utils/tag-grammar";-import { isLiteralTagPosition, tagMatchBefore } from "./markdownTagRanges";+import { findTagMatches, isTagIntroducerAt } from "@/utils/tag-grammar";/*** Ranks a candidate tag against the typed text (both already lower-cased).@@ -22,13 +22,27 @@ const matchRank = (tag: string, typed: string): number | undefined => {export function makeTagCompletionSource(getTags: () => string[]) {return (ctx: CompletionContext): CompletionResult | null => {- const match = tagMatchBefore(ctx.state, ctx.pos);- const source = ctx.state.doc.toString();- const explicitBareIntroducer =- ctx.explicit && ctx.pos > 0 && isTagIntroducerAt(source, ctx.pos - 1) && isLiteralTagPosition(ctx.state, ctx.pos);- if (!match && !explicitBareIntroducer) return null;+ // Completion is an input aid, including inside code, links, and escapes.+ // Keep tag spelling rules, but don't restrict candidates to rendered tags.+ const line = ctx.state.doc.lineAt(ctx.pos);+ const position = ctx.pos - line.from;+ const match = findTagMatches(line.text, 0, position).findLast(+ (candidate) => candidate.to === position || (candidate.to === position - 1 && line.text[position - 1] === "/"),+ );+ const bareIntroducer = isTagIntroducerAt(line.text, position - 1);+ if (!match && !bareIntroducer) return null;- const typed = (match?.value ?? "").toLowerCase();+ const from = match ? line.from + match.from + 1 : ctx.pos;+ if (!ctx.explicit) {+ const tree = ensureSyntaxTree(ctx.state, ctx.pos) ?? syntaxTree(ctx.state);+ const node = tree.resolveInner(from - 1, 1);+ // Only the opening heading marker is reserved. A later # in heading+ // text still offers tags, including a potential closing heading marker.+ if (node.name === "HeaderMark" && node.parent?.firstChild?.from === node.from) return null;+ }++ // A trailing slash is an unfinished child segment, not the end of input.+ const typed = (match ? match.value + (match.to < position ? "/" : "") : "").toLowerCase();const options = getTags().map((tag) => ({ tag, rank: matchRank(tag.toLowerCase(), typed) })).filter((candidate): candidate is { tag: string; rank: number } => candidate.rank !== undefined)@@ -38,7 +52,7 @@ export function makeTagCompletionSource(getTags: () => string[]) {if (options.length === 0) return null;// `filter: false` keeps this ranking: CodeMirror would otherwise re-filter// and re-score the options with its own fuzzy matcher.- return { from: match ? match.from + 1 : ctx.pos, options, filter: false };+ return { from, options, filter: false };};}diff --git a/web/src/components/MemoEditor/README.md b/web/src/components/MemoEditor/README.mdindex eec615fe4..b4a1f35b8 100644--- a/web/src/components/MemoEditor/README.md+++ b/web/src/components/MemoEditor/README.md@@ -98,7 +98,7 @@ The app formatting keymap sits ahead of CodeMirror's generic `defaultKeymap`: `M- `Editor/markdownTagRanges.ts` adapts the CodeMirror syntax tree into literal-source ranges, excluding links, code, math, raw HTML syntax, escapes, and entities before calling the shared scanner.- `Editor/tagMentionDecorations.ts` decorates the tag matches returned by that adapter; mention recognition remains separate.-- `Editor/tagAutocomplete.ts` reuses the same adapter for the tag ending at the cursor and offers known tags from `useTagCounts`.+- `Editor/tagAutocomplete.ts` scans the raw input at the cursor and offers known tags from `useTagCounts`, including inside code, links, and escapes. Bare `#` offers all tags except at an opening heading marker (manual completion still works there). Nested paths match by prefix, path segment, or substring, including an unfinished child after `/`.- `@/utils/remark-plugins/remark-tag` is the read-only renderer's Markdown AST adapter to the same scanner.### Servicesdiff --git a/web/tests/editor-tag-autocomplete.test.ts b/web/tests/editor-tag-autocomplete.test.tsindex a801c5465..6162a926b 100644--- a/web/tests/editor-tag-autocomplete.test.ts+++ b/web/tests/editor-tag-autocomplete.test.ts@@ -1,7 +1,10 @@-import { CompletionContext } from "@codemirror/autocomplete";+import { acceptCompletion, CompletionContext, completionStatus, currentCompletions } from "@codemirror/autocomplete";import { markdown } from "@codemirror/lang-markdown";import { EditorState } from "@codemirror/state";+import { EditorView } from "@codemirror/view";+import { waitFor } from "@testing-library/react";import { describe, expect, it } from "vitest";+import { buildEditorExtensions } from "@/components/MemoEditor/Editor/extensions";import { makeTagCompletionSource } from "@/components/MemoEditor/Editor/tagAutocomplete";import { memoMarkdownExtensions } from "@/utils/memo-markdown-extension";@@ -12,236 +15,167 @@ function complete(doc: string, pos: number, tags: string[], explicit = false) {}describe("tag autocomplete", () => {- it("offers known tags after #", () => {- const result = complete("hello #to", 9, ["todo", "today", "work"]);- expect(result?.options.map((o) => o.label)).toEqual(["todo", "today"]);+ it("offers known tags after one character", () => {+ expect(complete("#t", 2, ["thoughts", "today", "work"])?.options.map((o) => o.label)).toEqual(["thoughts", "today"]);});- it("matches a segment of a nested tag path", () => {- expect(complete("hello #Mem", 10, ["software/hosted/Memos"])?.options.map((o) => o.label)).toEqual(["software/hosted/Memos"]);- });-- it("ranks full-path prefixes above segment starts above loose substrings", () => {- const result = complete("#work", 5, ["home/paperwork", "team/work-log", "work/project"]);- expect(result?.options.map((o) => o.label)).toEqual(["work/project", "team/work-log", "home/paperwork"]);+ it.each(["hello #", "hello#", "## heading #", "> text #", "- [ ] #", "#######"])("offers all tags after a bare hash: %s", (doc) => {+ const result = complete(doc, doc.length, ["todo", "work"]);+ expect(result?.from).toBe(doc.length);+ expect(result?.options.map((o) => o.label)).toEqual(["todo", "work"]);});- it("keeps its own ranking instead of CodeMirror's fuzzy filter", () => {- expect(complete("#work", 5, ["work"])?.filter).toBe(false);- });+ it.each([+ "#",+ "##",+ "######",+ " #",+ "> #",+ "> > ###",+ "- #",+ "1. ##",+ "- item\n #",+ "> - ##",+ ])("does not automatically complete an opening heading marker: %s", (doc) => expect(complete(doc, doc.length, ["todo"])).toBeNull());- it("returns null on a bare # with nothing typed", () => {- expect(complete("hello #", 7, ["todo"])).toBeNull();+ it("does not complete an opening heading marker before existing heading text", () => {+ for (const pos of [1, 2, 3]) expect(complete("## heading", pos, ["todo"])).toBeNull();});- it("returns null when not in a tag", () => {- expect(complete("hello world", 11, ["todo"])).toBeNull();+ it.each(["#", "> ##", "- #"])("allows explicit completion at an opening heading marker: %s", (doc) => {+ expect(complete(doc, doc.length, ["todo"], true)?.options.map((o) => o.label)).toEqual(["todo"]);});- it("supports tags without a left boundary and hierarchical values", () => {- expect(complete("hello#work/pr", 13, ["work", "work/private", "work/project"])?.options.map((option) => option.label)).toEqual([- "work/private",- "work/project",- ]);+ it.each(["#t", "##t", "> #t", "- #t", "1. #t"])("offers tags once the input is not a heading marker: %s", (doc) => {+ expect(complete(doc, doc.length, ["thoughts"])?.options.map((o) => o.label)).toEqual(["thoughts"]);});- it("filters by the emitted value and replaces the complete source spelling", () => {- const result = complete("#AB", 4, ["AB", "acorn"]);- expect(result?.from).toBe(1);- expect(result?.options.map((option) => option.label)).toEqual(["AB"]);+ it.each([+ "`#│`",+ "```\n#│\n```",+ " #│",+ "[#│](/path)",+ "",+ "[label](https://example.com/#│)",+ "https://example.com/#│",+ "www.點看.com/#│",+ "\\#│",+ "$#│$",+ "$$\n#│\n$$",+ '<span title="#│">',+ "<!-- #│ -->",+ "[#│][known]\n\n[known]: /path",+ "#│[email protected]",+ ])("completes bare and partial tags regardless of surrounding Markdown: %s", (source) => {+ for (const typed of ["", "t"]) {+ const pos = source.indexOf("│");+ const result = complete(source.replace("│", typed), pos + typed.length, ["thoughts"]);+ expect(result?.from).toBe(pos);+ expect(result?.options.map((o) => o.label)).toEqual(["thoughts"]);+ }});- it("completes tags containing word-internal apostrophes", () => {- const ascii = complete("#O'Br", 5, ["O'Brien", "O’Connor"]);- expect(ascii?.from).toBe(1);- expect(ascii?.options.map((option) => option.label)).toEqual(["O'Brien"]);-- const curly = complete("#O’Co", 5, ["O'Brien", "O’Connor"]);- expect(curly?.from).toBe(1);- expect(curly?.options.map((option) => option.label)).toEqual(["O’Connor"]);+ it("does not depend on parsing a distant reference definition", () => {+ const doc = `[#ta][known]\n\n${"x".repeat(120_000)}\n\n[known]: /path`;+ expect(complete(doc, doc.indexOf("]"), ["tag"])?.options.map((o) => o.label)).toEqual(["tag"]);});- it("keeps unknown character-reference shapes in literal tag source", () => {- const source = "#R&bogus;D";- const position = source.indexOf(";");- expect(complete(source, position, ["R&bogus", "R&D"])?.options.map((option) => option.label)).toEqual(["R&bogus"]);+ it.each(["", "hello world", "# ", "#tag ", "#work//", "#to"])("does not offer tags outside an active tag input: %s", (doc) => {+ expect(complete(doc, doc.length, ["todo", "work/project"])).toBeNull();});- it("completes tags in GFM task items", () => {- for (const source of ["- [ ] #ta", "- [x] #ta", "- [ ] text\n #ta"]) {- expect(complete(source, source.length, ["tag"])?.options.map((option) => option.label)).toEqual(["tag"]);- }+ it("returns null when no known tags match", () => {+ expect(complete("hello #", 7, [])).toBeNull();+ expect(complete("#missing", 8, ["todo"])).toBeNull();});- it("uses decoded boundaries hidden by a rejected URL node", () => {- const source = "+http://x.m/#u##va";- expect(complete(source, source.length, ["value"])?.options.map((option) => option.label)).toEqual(["value"]);+ it("matches a segment of a nested tag path without its parents", () => {+ expect(complete("hello #Mem", 10, ["software/hosted/Memos"])?.options.map((o) => o.label)).toEqual(["software/hosted/Memos"]);});- it("does not complete in opaque syntax swallowed by a rejected URL node", () => {- for (const source of [".https://a.b/`#code` #tail", ".https://a.b/[#link](/x) #tail", ".https://a.b/$#math$ #tail"]) {- expect(complete(source, source.indexOf("#") + 4, ["code", "link", "math"])).toBeNull();- expect(complete(source, source.length, ["tail"])?.options.map((option) => option.label)).toEqual(["tail"]);- }+ it("ranks full-path prefixes above segment starts above loose substrings", () => {+ const result = complete("#work", 5, ["home/paperwork", "team/work-log", "work/project"]);+ expect(result?.options.map((o) => o.label)).toEqual(["work/project", "team/work-log", "home/paperwork"]);+ expect(result?.filter).toBe(false);});- it("completes tags inside unresolved link-like source", () => {- for (const source of ["[#ta][missing]", "[#ta](unterminated"]) {- const position = source.indexOf("]");- expect(complete(source, position, ["tag"])?.options.map((option) => option.label)).toEqual(["tag"]);- }+ it.each(["#work/", "#work/pr", "hello#work/", "first line\n#work/"])("completes child tags after a path separator: %s", (doc) => {+ const result = complete(doc, doc.length, ["work", "work/private", "work/project", "home"]);+ expect(result?.from).toBe(doc.lastIndexOf("#") + 1);+ expect(result?.options.map((o) => o.label)).toEqual(["work/private", "work/project"]);});- it("completes tags inside unresolved second labels", () => {- for (const source of ["[x][#tw]", "[#one][#tw]", "[x][**#tw**]"]) {- const position = source.indexOf("tw") + 2;- expect(complete(source, position, ["two"])?.options.map((option) => option.label)).toEqual(["two"]);- }- const emphasisBoundary = "[#x][#_foo_]";- expect(complete(emphasisBoundary, emphasisBoundary.indexOf("foo") + 3, ["_foo_"])).toBeNull();+ it.each(["#hosted/", "#software/hosted/"])("matches nested paths at any depth: %s", (doc) => {+ const result = complete(doc, doc.length, ["software/hosted/Memos"]);+ expect(result?.options.map((o) => o.label)).toEqual(["software/hosted/Memos"]);+ expect(result?.from).toBe(1);});- it.each([- "[#[email protected]][missing]",- "[x][#[email protected]]",- "[#[email protected]](unterminated",- "![#[email protected]][missing]",- "[x][_#[email protected]_]",- "![x][_#[email protected]_]",- ])("does not complete inside a GFM email in unresolved link-like source: %s", (source) =>- expect(complete(source, source.indexOf("foo") + 3, ["foo"])).toBeNull());-- it("does not complete tags inside resolved references", () => {- const source = "[#ta][known]\n\n[known]: /path";- expect(complete(source, source.indexOf("]"), ["tag"])).toBeNull();+ it("filters by the emitted Unicode value and replaces the source spelling", () => {+ const result = complete("#AB", 4, ["AB", "acorn"]);+ expect(result?.from).toBe(1);+ expect(result?.options.map((o) => o.label)).toEqual(["AB"]);+ expect(complete("#工作/", 4, ["工作/项目"])?.options.map((o) => o.label)).toEqual(["工作/项目"]);});- it.each([- "[a [#link](x) b][missing]",- "[a  b][missing]",- ])("does not complete inside a resolved child of an unresolved outer link: %s", (source) =>- expect(complete(source, source.indexOf("#") + 4, ["link", "image"])).toBeNull());-- it("fails closed while a distant reference definition is not parsed yet", () => {- const source = `[#ta][known]\n\n${"x".repeat(120_000)}\n\n[known]: /path`;- expect(complete(source, source.indexOf("]"), ["tag"])).toBeNull();+ it("completes tags containing word-internal apostrophes", () => {+ expect(complete("#O'Br", 5, ["O'Brien", "O’Connor"])?.options.map((o) => o.label)).toEqual(["O'Brien"]);+ expect(complete("#O’Co", 5, ["O'Brien", "O’Connor"])?.options.map((o) => o.label)).toEqual(["O’Connor"]);});- it("follows keycap candidate enumeration", () => {+ it("distinguishes keycap emoji from a tag introducer", () => {expect(complete("#️⃣", 3, ["#️⃣"])).toBeNull();const result = complete("##️⃣", 4, ["#️⃣"]);expect(result?.from).toBe(1);- expect(result?.options.map((option) => option.label)).toEqual(["#️⃣"]);- });-- it.each([- ["`#to`", 4],- ["[#to](https://example.com)", 4],- ["https://example.com/#to", 23],- ["\\#to", 4],- ["#to", 7],- ["$#to$", 4],- ["$#to\\$", 4],- ["$$meta\n#to", 10],- ])("returns null in opaque Markdown source: %s", (doc, position) => expect(complete(doc, position, ["todo"])).toBeNull());-- it("matches escaped-dollar and mismatched-run math boundaries", () => {- expect(complete("$\\$#ta$", 6, ["tag"])?.options.map((option) => option.label)).toEqual(["tag"]);- expect(complete("$$#ta$", 5, ["tag"])?.options.map((option) => option.label)).toEqual(["tag"]);- });-- it("keeps recognized URL suffixes opaque without hiding invalid-domain tags", () => {- const url = "http://example.com#z";- const bomURL = "\uFEFFhttp://example.com#z";- const doubleBOMURL = "\uFEFF\uFEFFhttp://example.com#z";- const midDocumentBOMURL = "before\n\n\uFEFFhttp://example.com#z";- const invalidDomain = "https://foo.example_/#vi";-- expect(complete(url, url.length, ["zebra"])).toBeNull();- expect(complete(bomURL, bomURL.length, ["zebra"])).toBeNull();- expect(complete(doubleBOMURL, doubleBOMURL.length, ["zebra"])?.options.map((option) => option.label)).toEqual(["zebra"]);- expect(complete(midDocumentBOMURL, midDocumentBOMURL.length, ["zebra"])?.options.map((option) => option.label)).toEqual(["zebra"]);- expect(complete(invalidDomain, invalidDomain.length, ["visible"])?.options.map((option) => option.label)).toEqual(["visible"]);- });-- it("keeps recognized Unicode-domain URL suffixes opaque", () => {- const protocolURL = "https://點看.com/#work";- const wwwURL = "www.點看.com/#work";- const validPrefixedSegment = "https://a_b.點看.com/#work";- const invalidPenultimateSegment = "https://foo_bar.點看/#work";- const missingBoundary = "hellohttps://點看.com/#work";-- expect(complete(protocolURL, protocolURL.length, ["work"])).toBeNull();- expect(complete(wwwURL, wwwURL.length, ["work"])).toBeNull();- expect(complete(validPrefixedSegment, validPrefixedSegment.length, ["work"])).toBeNull();- expect(complete(invalidPenultimateSegment, invalidPenultimateSegment.length, ["work"])?.options.map((option) => option.label)).toEqual([- "work",- ]);- expect(complete(missingBoundary, missingBoundary.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);- });-- it("uses GFM protocol and www boundaries for Unicode-domain URLs", () => {- for (const url of ["1https://點看.com/#work", ".https://點看.com/#work", "]www.點看.com/#work", "中https://點看.com/#work"]) {- expect(complete(url, url.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);- }-- const joined = "hellohttps://點看.com/#work";- expect(complete(joined, joined.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);- });-- it("keeps URLs blocked by unbalanced brackets tag-eligible", () => {- for (const url of ["[https://點看.com/#work", "[www.點看.com/#work"]) {- expect(complete(url, url.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);- }- for (const url of ["\\[https://點看.com/#work", "\\[www.點看.com/#work"]) {- expect(complete(url, url.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);+ expect(result?.options.map((o) => o.label)).toEqual(["#️⃣"]);+ for (const [doc, pos] of [+ ["#️⃣", 1],+ ["##️⃣", 2],+ ["#first#️⃣", 7],+ ] as const) {+ expect(complete(doc, pos, ["todo"], true)).toBeNull();}});+});+// Exercise automatic activation and insertion through the real editor extensions.+describe("tag completion popup", () => {it.each([- "[ https://example.com/#hidden]",- "[x https://example.com/#hidden]",- "![ https://example.com/#hidden]",- ])("does not complete in a GFM URL inside unresolved bracket text: %s", (source) =>- expect(complete(source, source.indexOf("hidden") + "hidden".length, ["hidden"])).toBeNull());-- it("uses one explicit separator class for written URLs", () => {- for (const source of [- "https://點看.com/\u00a0#work",- "https://點看.com/\u2003#work",- "https://點看.com/\u000b#work",- "https://點看.com/\u2028#work",- "https://點看.com/\u2029#work",- ]) {- expect(complete(source, source.length, ["work"])?.options.map((option) => option.label)).toEqual(["work"]);+ ["hello ", "#", "hello #software/hosted/Memos"],+ ["", "#Mem", "#software/hosted/Memos"],+ ["", "#software/hosted/", "#software/hosted/Memos"],+ ])("opens and inserts a full nested tag after typing %s%s", async (initial, input, expected) => {+ const view = new EditorView({+ state: EditorState.create({+ doc: initial,+ selection: { anchor: initial.length },+ extensions: buildEditorExtensions({+ placeholder: "",+ onChange: () => {},+ onFiles: () => {},+ onUpdate: () => {},+ onSubmit: () => {},+ getTags: () => ["software/hosted/Memos"],+ }),+ }),+ parent: document.body,+ });+ try {+ view.focus();+ for (const character of input) {+ view.dispatch({+ changes: { from: view.state.selection.main.head, insert: character },+ selection: { anchor: view.state.selection.main.head + character.length },+ userEvent: "input.type",+ });+ }+ await waitFor(() => expect(completionStatus(view.state)).toBe("active"));+ expect(currentCompletions(view.state).map((o) => o.label)).toEqual(["software/hosted/Memos"]);+ // CodeMirror briefly ignores acceptance just after the popup opens.+ await waitFor(() => expect(acceptCompletion(view)).toBe(true));+ expect(view.state.doc.toString()).toBe(expected);+ } finally {+ view.destroy();}- const nelURL = "https://點看.com/\u0085#work";- expect(complete(nelURL, nelURL.length, ["work"])).toBeNull();- });-- it("uses reconciled GFM email ranges for completion", () => {- const escapedPlus = "#foo\\[email protected]";- const escapedUnderscore = "#next\\[email protected]";- const invalidSuffix = "#foo/[email protected]_";-- expect(complete(escapedPlus, 4, ["foo"])).toBeNull();- expect(complete(escapedUnderscore, 5, ["next"])).toBeNull();- expect(complete(invalidSuffix, 8, ["foo/bar"])?.options.map((option) => option.label)).toEqual(["foo/bar"]);- });-- it.each([- "#foo.[email protected]",- "#foo.[email protected]",- "#foo@example.com",- ])("does not complete inside a GFM email joined by a known entity: %s", (source) => expect(complete(source, 4, ["foo"])).toBeNull());-- it("offers all tags on explicitly completed bare introducer", () => {- expect(complete("hello #", 7, ["todo", "work"], true)?.options.map((option) => option.label)).toEqual(["todo", "work"]);- });-- it("does not treat a keycap emoji hash as an explicit bare introducer", () => {- expect(complete("#️⃣", 1, ["todo"], true)).toBeNull();- expect(complete("##️⃣", 2, ["todo"], true)).toBeNull();- expect(complete("#first#️⃣", 7, ["todo"], true)).toBeNull();});});