Skip to content

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

feat: support stable note references

tianma-if669a31426735
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 98c06687..69017d57 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -1,5 +1,5 @@
import { useRef, useState, useEffect, useCallback, useMemo, type CSSProperties, type PointerEvent as ReactPointerEvent, type ReactNode } from "react";
-import { useQueryClient, useMutation } from "@tanstack/react-query";
+import { useQuery, useQueryClient, useMutation } from "@tanstack/react-query";
import { NodeViewWrapper, ReactNodeViewRenderer, useEditor, EditorContent, type Editor, type NodeViewProps } from "@tiptap/react";
import { Plugin, PluginKey } from "@tiptap/pm/state";
import { Decoration, DecorationSet } from "@tiptap/pm/view";
@@ -33,6 +33,7 @@ import {
Info,
FileDown,
Printer,
+ Link2,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { GitHubRepositoryLink } from "@/components/GitHubRepositoryLink";
@@ -77,8 +78,11 @@ import {
resolveMemoContentDoc,
type Notebook,
type MemoDetail,
+ type MemoSummary,
type MemoEditSession,
type TiptapDoc,
+ createMemoLinkHref,
+ parseMemoLinkHref,
} from "@edgeever/shared";
import {
DEFAULT_IMAGE_WIDTH_PERCENT,
@@ -518,6 +522,7 @@ type EditorPaneProps = {
searchFocusToken: number;
replaceFocusToken: number;
selectionActionBar?: ReactNode;
+ onOpenMemo?: (memoId: string) => void;
};
type RichEditorPaneProps = EditorPaneProps & {
@@ -1147,6 +1152,7 @@ const RichEditorPane = ({
searchFocusToken,
replaceFocusToken,
selectionActionBar,
+ onOpenMemo,
onRequestMobileNativeEdit,
}: RichEditorPaneProps) => {
const { t, i18n } = useTranslation();
@@ -1171,6 +1177,8 @@ const RichEditorPane = ({
const [noteSearchReplaceOpen, setNoteSearchReplaceOpen] = useState(false);
const [noteSearchReplacement, setNoteSearchReplacement] = useState("");
const [noteSearchIndex, setNoteSearchIndex] = useState(0);
+ const [noteLinkPickerOpen, setNoteLinkPickerOpen] = useState(false);
+ const [noteLinkQuery, setNoteLinkQuery] = useState("");
const [isMobileViewport, setIsMobileViewport] = useState(() =>
typeof window === "undefined" ? false : window.matchMedia(MOBILE_EDITOR_QUERY).matches
);
@@ -1184,6 +1192,11 @@ const RichEditorPane = ({
const [mobileImeDebugActiveElement, setMobileImeDebugActiveElement] = useState(getActiveElementLabel);
const [mobileImeDebugEvents, setMobileImeDebugEvents] = useState<MobileImeDebugEntry[]>([]);
const [wechatCopyState, setWechatCopyState] = useState<"idle" | "copying" | "copied" | "error">("idle");
+ const noteLinkResultsQuery = useQuery({
+ queryKey: ["memo-link-search", noteLinkQuery],
+ queryFn: () => api.listMemos({ q: noteLinkQuery, limit: 20 }),
+ enabled: noteLinkPickerOpen,
+ });
const [editorScrollContainer, setEditorScrollContainer] = useState<HTMLDivElement | null>(null);
const setEditorScrollContainerRef = useCallback((element: HTMLDivElement | null) => {
editorScrollContainerRef.current = element;
@@ -1420,6 +1433,17 @@ const RichEditorPane = ({
});
return true;
},
+ handleClick: (_view, _pos, event) => {
+ const target = event.target instanceof HTMLElement ? event.target.closest("a[href^='#memo=']") : null;
+ const memoId = parseMemoLinkHref(target?.getAttribute("href"));
+ if (!memoId || !onOpenMemo) {
+ return false;
+ }
+
+ event.preventDefault();
+ onOpenMemo(memoId);
+ return true;
+ },
handlePaste: (_view, event) => {
const files = getResourceFilesFromDataTransfer(event.clipboardData);
@@ -1445,6 +1469,26 @@ const RichEditorPane = ({
},
});
+ const insertMemoLink = useCallback((target: MemoSummary) => {
+ if (!isEditorReady(editor) || effectiveReadOnly || target.id === memo?.id) {
+ return;
+ }
+
+ const { from, to } = editor.state.selection;
+ const selectedText = editor.state.doc.textBetween(from, to, " ").trim();
+ editor
+ .chain()
+ .focus()
+ .insertContent({
+ type: "text",
+ text: selectedText || target.title || t("common.untitledMemo"),
+ marks: [{ type: "link", attrs: { href: createMemoLinkHref(target.id), class: "edgeever-note-link" } }],
+ })
+ .run();
+ setNoteLinkPickerOpen(false);
+ setNoteLinkQuery("");
+ }, [editor, effectiveReadOnly, memo?.id, t]);
+
useEffect(() => {
imageCompressionEnabledRef.current = imageCompressionEnabled;
}, [imageCompressionEnabled]);
@@ -2585,6 +2629,53 @@ const RichEditorPane = ({
return (
<div className="relative flex h-full min-w-0 flex-col bg-white">
{selectionActionBar}
+ {noteLinkPickerOpen && (
+ <div className="absolute left-3 right-3 top-14 z-30 h-[min(22rem,calc(100%-4rem))] max-w-xl rounded-lg border border-slate-200 bg-white shadow-xl sm:left-5 sm:right-auto sm:w-[28rem]" role="dialog" aria-label={t("noteLinkPicker.title")}>
+ <Command shouldFilter={false}>
+ <div className="flex items-center justify-between border-b border-slate-100 pr-2">
+ <CommandInput
+ autoFocus
+ value={noteLinkQuery}
+ placeholder={t("noteLinkPicker.searchPlaceholder")}
+ onValueChange={setNoteLinkQuery}
+ onKeyDown={(event) => {
+ if (event.key === "Escape") {
+ setNoteLinkPickerOpen(false);
+ }
+ }}
+ />
+ <button
+ type="button"
+ className="rounded-md p-1.5 text-slate-400 hover:bg-slate-100 hover:text-slate-700"
+ aria-label={t("noteLinkPicker.close")}
+ onClick={() => setNoteLinkPickerOpen(false)}
+ >
+ <X className="h-4 w-4" />
+ </button>
+ </div>
+ <CommandList>
+ {noteLinkResultsQuery.isLoading ? (
+ <div className="p-6 text-center text-sm text-slate-500">{t("noteLinkPicker.loading")}</div>
+ ) : (
+ <>
+ <CommandEmpty>{t("noteLinkPicker.empty")}</CommandEmpty>
+ <CommandGroup>
+ {(noteLinkResultsQuery.data?.memos ?? [])
+ .filter((candidate) => candidate.id !== memo?.id && !candidate.isDeleted)
+ .map((candidate) => (
+ <CommandItem key={candidate.id} value={candidate.id} onSelect={() => insertMemoLink(candidate)}>
+ <Link2 className="h-4 w-4 shrink-0 text-emerald-600" />
+ <span className="min-w-0 flex-1 truncate">{candidate.title || t("common.untitledMemo")}</span>
+ <span className="max-w-40 truncate text-xs text-slate-400">{candidate.excerpt}</span>
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </>
+ )}
+ </CommandList>
+ </Command>
+ </div>
+ )}
<header className="shrink-0 border-b border-slate-200 bg-white">
<div className="flex min-h-12 items-center justify-between gap-2 border-b border-slate-100 px-3 py-2 sm:px-5">
<div className="flex min-w-0 items-center gap-2 text-sm">
@@ -3060,6 +3151,7 @@ const RichEditorPane = ({
markdownMode={useMarkdownSourceEditor}
onMarkdownModeChange={handleMarkdownModeChange}
onPickAttachment={() => fileInputRef.current?.click()}
+ onPickNoteLink={() => setNoteLinkPickerOpen(true)}
/>
)}
</header>
diff --git a/apps/web/src/components/EditorToolbar.tsx b/apps/web/src/components/EditorToolbar.tsx
index f9039434..d0fb8520 100644
--- a/apps/web/src/components/EditorToolbar.tsx
+++ b/apps/web/src/components/EditorToolbar.tsx
@@ -14,10 +14,11 @@ import {
ListIndentIncrease,
Quote,
SquareCode,
- Workflow,
+ ChartNoAxesCombined,
Minus,
Paperclip,
Blocks,
+ Link2,
} from "lucide-react";
import { cn } from "@/lib/utils";
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
@@ -39,18 +40,21 @@ const EditorToolbarButton = ({
disabled = false,
onClick,
title,
+ wide = false,
}: {
active?: boolean;
children: ReactNode;
disabled?: boolean;
onClick: () => void;
title: string;
+ wide?: boolean;
}) => (
<Tooltip>
<TooltipTrigger asChild>
<button
className={cn(
- "flex h-8 w-8 shrink-0 items-center justify-center rounded-md border text-slate-700 transition disabled:pointer-events-none disabled:opacity-40",
+ "flex h-8 shrink-0 items-center justify-center rounded-md border text-slate-700 transition disabled:pointer-events-none disabled:opacity-40",
+ wide ? "w-auto gap-1 px-2" : "w-8",
active
? "border-emerald-200 bg-emerald-50 text-emerald-800"
: "border-transparent bg-transparent hover:border-slate-200 hover:bg-slate-50"
@@ -141,12 +145,14 @@ export const EditorToolbar = ({
markdownMode = false,
onMarkdownModeChange,
onPickAttachment,
+ onPickNoteLink,
}: {
editor: Editor | null;
readOnly: boolean;
markdownMode?: boolean;
onMarkdownModeChange?: () => void;
onPickAttachment?: () => void;
+ onPickNoteLink?: () => void;
}) => {
const { t } = useTranslation();
const editorReady = isToolbarEditorReady(editor);
@@ -262,6 +268,20 @@ export const EditorToolbar = ({
<ToolbarDivider />
</>
)}
+ {onPickNoteLink && (
+ <>
+ <EditorToolbarButton
+ title={t("editorToolbar.noteLink")}
+ wide
+ disabled={readOnly}
+ onClick={onPickNoteLink}
+ >
+ <Link2 className="h-4 w-4" />
+ <span className="text-xs font-medium">{t("editorToolbar.noteLink")}</span>
+ </EditorToolbarButton>
+ <ToolbarDivider />
+ </>
+ )}
{markdownMode ? (
<span className="shrink-0 text-xs text-slate-500">{t("editorToolbar.markdownSource")}</span>
) : (
@@ -385,7 +405,7 @@ export const EditorToolbar = ({
disabled={disabled}
onClick={() => run(insertMermaidDiagram)}
>
- <Workflow className="h-4 w-4" />
+ <ChartNoAxesCombined className="h-4 w-4" />
</EditorToolbarButton>
{showCodeLanguageSelector && (
<Select
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index 53cf2bac..e4241b85 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -2776,6 +2776,12 @@ export const WorkspaceApp = ({
contentSearchQuery={search}
searchFocusToken={noteSearchFocusToken}
replaceFocusToken={noteReplaceFocusToken}
+ onOpenMemo={(memoId) => {
+ clearPendingCreatedMemo();
+ setMemoView("notebook");
+ setSelectedMemoId(memoId);
+ setActivePane("editor");
+ }}
imageCompressionEnabled={imageCompressionEnabled}
autoSaveIntervalMs={autoSaveIntervalMs}
selectionActionBar={memoSelectionActionBar}
diff --git a/apps/web/src/styles/globals.css b/apps/web/src/styles/globals.css
index fefd3299..760171ac 100644
--- a/apps/web/src/styles/globals.css
+++ b/apps/web/src/styles/globals.css
@@ -251,6 +251,59 @@ textarea {
margin: 0 0 var(--editor-paragraph-spacing);
}
+.ProseMirror a.edgeever-note-link,
+.ProseMirror a[href^="#memo="] {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.2rem;
+ max-width: min(100%, 32rem);
+ padding: 0.03rem 0.32rem 0.05rem;
+ border: 1px solid rgb(110 231 183 / 0.9);
+ border-radius: 0.35rem;
+ background: linear-gradient(135deg, rgb(236 253 245), rgb(209 250 229));
+ color: rgb(4 120 87);
+ font-size: 0.9em;
+ font-weight: 500;
+ text-decoration: underline;
+ text-decoration-color: rgb(52 211 153 / 0.7);
+ text-underline-offset: 2px;
+ vertical-align: baseline;
+ cursor: pointer;
+ transition: border-color 150ms ease, background-color 150ms ease, box-shadow 150ms ease, color 150ms ease;
+}
+
+.ProseMirror a.edgeever-note-link::before,
+.ProseMirror a[href^="#memo="]::before {
+ content: "↗";
+ flex: 0 0 auto;
+ font-size: 0.85em;
+ font-weight: 800;
+ line-height: 1;
+}
+
+.ProseMirror a.edgeever-note-link:hover,
+.ProseMirror a[href^="#memo="]:hover {
+ color: rgb(6 95 70);
+ border-color: rgb(16 185 129);
+ background: rgb(167 243 208);
+ text-decoration-color: rgb(4 120 87);
+ box-shadow: 0 2px 8px rgb(16 185 129 / 0.2);
+}
+
+:root.dark .ProseMirror a.edgeever-note-link,
+:root.dark .ProseMirror a[href^="#memo="] {
+ border-color: rgb(52 211 153 / 0.7);
+ background: linear-gradient(135deg, rgb(6 78 59), rgb(6 95 70));
+ color: rgb(110 231 183);
+}
+
+:root.dark .ProseMirror a.edgeever-note-link:hover,
+:root.dark .ProseMirror a[href^="#memo="]:hover {
+ border-color: rgb(110 231 183);
+ background: rgb(6 95 70);
+ color: rgb(167 243 208);
+}
+
.ProseMirror a.edgeever-attachment-link,
.ProseMirror a[href*="/api/v1/resources/"] {
display: inline-flex;
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 13d85b79..6704852c 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -667,6 +667,14 @@ export const enUS = {
switchToMarkdown: "Switch to Markdown mode",
switchToRichText: "Switch to rich text mode",
attachment: "Upload attachment",
+ noteLink: "Link a note",
+ },
+ noteLinkPicker: {
+ title: "Link a note",
+ searchPlaceholder: "Search notes to link",
+ empty: "No notes found",
+ loading: "Searching notes...",
+ close: "Close note picker",
},
mobileNotebookPicker: {
title: "Switch notebook",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 34fe5cce..bd02e42b 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -667,6 +667,14 @@ export const zhCN = {
switchToMarkdown: "切换到 Markdown 模式",
switchToRichText: "切换到富文本模式",
attachment: "上传附件",
+ noteLink: "引用笔记",
+ },
+ noteLinkPicker: {
+ title: "引用笔记",
+ searchPlaceholder: "搜索要引用的笔记",
+ empty: "没有找到可引用的笔记",
+ loading: "正在搜索笔记...",
+ close: "关闭引用笔记",
},
mobileNotebookPicker: {
title: "切换笔记本",
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index 9f8ee6fc..b0cffa0b 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -3,6 +3,7 @@ export * from "./backup";
export * from "./github-feedback";
export * from "./image-display";
export * from "./memo-safety";
+export * from "./note-links";
export * from "./notebooks";
export * from "./revision-diff";
export * from "./schemas";
diff --git a/packages/shared/src/note-links.test.mjs b/packages/shared/src/note-links.test.mjs
new file mode 100644
index 00000000..8edee99f
--- /dev/null
+++ b/packages/shared/src/note-links.test.mjs
@@ -0,0 +1,29 @@
+import { describe, expect, test } from "bun:test";
+import { createMemoLinkHref, parseMemoLinkHref } from "./note-links.ts";
+import { docToMarkdown, markdownToDoc } from "./content.ts";
+
+describe("stable memo links", () => {
+ test("round trips a memo id through the internal link format", () => {
+ const href = createMemoLinkHref("memo/中文 1");
+
+ expect(href).toBe("#memo=memo%2F%E4%B8%AD%E6%96%87%201");
+ expect(parseMemoLinkHref(href)).toBe("memo/中文 1");
+ });
+
+ test("rejects unrelated links", () => {
+ expect(parseMemoLinkHref("https://example.com")).toBeNull();
+ expect(parseMemoLinkHref("#memo=")).toBeNull();
+ });
+
+ test("preserves memo links in Markdown", () => {
+ const markdown = "查看 [项目笔记](#memo=memo_project)。";
+ const doc = markdownToDoc(markdown);
+
+ expect(doc.content[0]?.content?.[1]).toMatchObject({
+ type: "text",
+ text: "项目笔记",
+ marks: [{ type: "link", attrs: { href: "#memo=memo_project" } }],
+ });
+ expect(docToMarkdown(doc)).toBe(markdown);
+ });
+});
diff --git a/packages/shared/src/note-links.ts b/packages/shared/src/note-links.ts
new file mode 100644
index 00000000..8fbe2a91
--- /dev/null
+++ b/packages/shared/src/note-links.ts
@@ -0,0 +1,20 @@
+const MEMO_LINK_PREFIX = "#memo=";
+
+export const createMemoLinkHref = (memoId: string): string => `${MEMO_LINK_PREFIX}${encodeURIComponent(memoId)}`;
+
+export const parseMemoLinkHref = (href: unknown): string | null => {
+ if (typeof href !== "string" || !href.startsWith(MEMO_LINK_PREFIX)) {
+ return null;
+ }
+
+ const memoId = href.slice(MEMO_LINK_PREFIX.length);
+ if (!memoId) {
+ return null;
+ }
+
+ try {
+ return decodeURIComponent(memoId);
+ } catch {
+ return null;
+ }
+};