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): open assistant note links in the workspace

tianma-if3ab3d59c06f0
diff --git a/apps/web/src/components/ai-sidebar/AiSidebar.tsx b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
index 1ae7f6f9..cf54611c 100644
--- a/apps/web/src/components/ai-sidebar/AiSidebar.tsx
+++ b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
@@ -1421,7 +1421,7 @@ function AiSidebarSession({
}
};
- const openLinkedNote = (event: MouseEvent<HTMLDivElement>, turn: CompanionTurn) => {
+ const openLinkedNote = (event: MouseEvent<HTMLElement>, sources: Array<{ id: string; notebookId?: string }>) => {
const target = event.target;
if (!(target instanceof Element) || !onOpenCompanionNote) return;
const link = target.closest("a");
@@ -1430,10 +1430,8 @@ function AiSidebarSession({
?? parseMemoLinkHref(link.hash)
?? parseMemoLinkHref(link.href);
if (!linkedId) return;
- const source = turn.sources.find((item) => item.id === linkedId);
- if (!source?.notebookId) return;
event.preventDefault();
- onOpenCompanionNote(linkedId, source.notebookId);
+ onOpenCompanionNote(linkedId, sources.find((item) => item.id === linkedId)?.notebookId ?? "");
};
const companionThreads = useMemo(() => chatThreadsFromTurns(turns), [turns]);
@@ -1552,7 +1550,7 @@ function AiSidebarSession({
/>
) : null}
{visibleCompanion ? threadTurns.map((turn) => (
- <div key={turn.id} className="space-y-2">
+ <div key={turn.id} className="space-y-2" onClick={(event) => openLinkedNote(event, turn.sources)}>
<Message from="user">
<MessageContent className={sidebarUserMessageClassName}>{turn.message}</MessageContent>
<AttachmentChips items={turn.attachments ?? []} />
@@ -1580,11 +1578,9 @@ function AiSidebarSession({
</ul>
) : null}
{turn.response ? (
- <div onClick={(event) => openLinkedNote(event, turn)}>
- <AiSidebarMessage isAnimating={turn.status === "running"}>
- {linkedCompanionText(turn.response, turn)}
- </AiSidebarMessage>
- </div>
+ <AiSidebarMessage isAnimating={turn.status === "running"}>
+ {linkedCompanionText(turn.response, turn)}
+ </AiSidebarMessage>
) : null}
{turn.status === "interrupted" && turn.questions?.length && !running ? (
<CompanionQuestionForm questions={turn.questions} busy={busy || acting} onSubmit={(answers) => resume(turn, answers)} />
@@ -1602,7 +1598,7 @@ function AiSidebarSession({
</Message>
</div>
)) : localThreadTurns.map((turn) => (
- <div key={turn.id} className="space-y-2">
+ <div key={turn.id} className="space-y-2" onClick={(event) => openLinkedNote(event, [])}>
<Message from="user">
<MessageContent className={sidebarUserMessageClassName}>{turn.message}</MessageContent>
<AttachmentChips items={turn.attachments} />
diff --git a/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx b/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx
index 4c514558..53f649c3 100644
--- a/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx
+++ b/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx
@@ -6,18 +6,23 @@ import { memo } from "react";
import { Streamdown } from "streamdown";
import "katex/dist/katex.min.css";
import { cn } from "@/lib/utils";
+import { rewriteSidebarNoteLinks, sidebarNoteLinkAllowedTags, sidebarNoteLinkComponents } from "./sidebar-note-links";
const sidebarStreamdownPlugins = { cjk, code, mermaid, math };
export const AiSidebarMessage = memo(
- ({ className, ...props }: { children: string; className?: string; isAnimating?: boolean }) => (
+ ({ className, children, isAnimating }: { children: string; className?: string; isAnimating?: boolean }) => (
<Streamdown
+ allowedTags={sidebarNoteLinkAllowedTags}
className={cn("size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0", className)}
+ components={sidebarNoteLinkComponents}
+ isAnimating={isAnimating}
plugins={sidebarStreamdownPlugins}
- {...props}
- />
+ >
+ {rewriteSidebarNoteLinks(children)}
+ </Streamdown>
),
- (prev, next) => prev.children === next.children && prev.isAnimating === next.isAnimating,
+ (prev, next) => prev.children === next.children && prev.isAnimating === next.isAnimating && prev.className === next.className,
);
AiSidebarMessage.displayName = "AiSidebarMessage";
diff --git a/apps/web/src/components/ai-sidebar/sidebar-note-links.test.mjs b/apps/web/src/components/ai-sidebar/sidebar-note-links.test.mjs
new file mode 100644
index 00000000..02403ebb
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/sidebar-note-links.test.mjs
@@ -0,0 +1,38 @@
+import { describe, expect, test } from "bun:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { Streamdown } from "streamdown";
+import { rewriteSidebarNoteLinks, sidebarNoteLinkAllowedTags, sidebarNoteLinkComponents } from "./sidebar-note-links.tsx";
+
+const render = (markdown) => renderToStaticMarkup(createElement(Streamdown, {
+ allowedTags: sidebarNoteLinkAllowedTags,
+ components: sidebarNoteLinkComponents,
+ mode: "static",
+ children: rewriteSidebarNoteLinks(markdown),
+}));
+
+describe("sidebar note links", () => {
+ test("turns a note link into an in-app anchor and leaves other links alone", () => {
+ const markdown = "已保存至“等待分类”:\n\n[模型蒸馏流程图](#memo=memo_03de025fc0134b7da0d155c2c1686b92)\n\n详见 [文档](https://example.com/docs)。";
+ expect(rewriteSidebarNoteLinks(markdown)).toContain(
+ '<edgeever-note data-memo-id="memo_03de025fc0134b7da0d155c2c1686b92">模型蒸馏流程图</edgeever-note>',
+ );
+ expect(rewriteSidebarNoteLinks(markdown)).toContain("[文档](https://example.com/docs)");
+
+ const markup = render(markdown);
+ expect(markup).toContain('href="#memo=memo_03de025fc0134b7da0d155c2c1686b92"');
+ expect(markup).toContain("模型蒸馏流程图");
+ expect(markup).not.toContain('href="#memo=memo_03de025fc0134b7da0d155c2c1686b92" target="_blank"');
+ expect(markup).toContain(">文档</button>");
+ });
+
+ test("keeps note-link examples inside code, and escapes the label", () => {
+ const markdown = "```\n[示例](#memo=memo_secret)\n```\n\n`[行内](#memo=memo_inline)`\n\n[a<b \"c\"](#memo=memo_quoted)";
+ const rewritten = rewriteSidebarNoteLinks(markdown);
+ expect(rewritten).toContain("[示例](#memo=memo_secret)");
+ expect(rewritten).toContain("[行内](#memo=memo_inline)");
+ expect(rewritten).toContain('data-memo-id="memo_quoted"');
+ expect(rewritten).toContain("a&lt;b &quot;c&quot;");
+ expect(rewritten).not.toContain("<b");
+ });
+});
diff --git a/apps/web/src/components/ai-sidebar/sidebar-note-links.tsx b/apps/web/src/components/ai-sidebar/sidebar-note-links.tsx
new file mode 100644
index 00000000..8bdbd248
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/sidebar-note-links.tsx
@@ -0,0 +1,59 @@
+import { createMemoLinkHref, parseMemoLinkHref } from "@edgeever/shared";
+import type { ComponentProps, ReactNode } from "react";
+
+const CODE_REGION = /(```[\s\S]*?```|```[\s\S]*$|`[^`\n]*`)/g;
+const MARKDOWN_LINK = /\[([^\]\n]+)\]\(([^)\s]+)\)/g;
+
+const escapeText = (value: string) => value
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;");
+
+const escapeAttr = (value: string) => escapeText(value).replace(/"/g, "&quot;");
+
+/**
+ * Streamdown treats every markdown link as an external website and asks
+ * before opening it in a new tab. Rewrite in-app note links so they stay
+ * anchors the sidebar can open in the current workspace.
+ */
+export function rewriteSidebarNoteLinks(markdown: string): string {
+ return markdown.split(CODE_REGION).map((part, index) => {
+ if (index % 2 === 1) return part;
+ return part.replace(MARKDOWN_LINK, (match, label: string, href: string) => {
+ const memoId = parseMemoLinkHref(href);
+ if (!memoId) return match;
+ return `<edgeever-note data-memo-id="${escapeAttr(memoId)}">${escapeAttr(label)}</edgeever-note>`;
+ });
+ }).join("");
+}
+
+// hast-util-sanitize matches property names, so this is `dataMemoId`, not the HTML attribute.
+export const sidebarNoteLinkAllowedTags = { "edgeever-note": ["dataMemoId"] };
+
+const memoIdFromProps = (props: Record<string, unknown>) => {
+ const value = props["data-memo-id"] ?? props.dataMemoId;
+ return typeof value === "string" ? value : "";
+};
+
+export function SidebarNoteLink({
+ children,
+ node: _node,
+ ...props
+}: { children?: ReactNode; node?: unknown } & Record<string, unknown>) {
+ const memoId = memoIdFromProps(props);
+ if (!memoId) return <>{children}</>;
+ return (
+ <a
+ className="wrap-anywhere rounded-sm font-medium text-primary underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-400"
+ data-streamdown="link"
+ href={createMemoLinkHref(memoId)}
+ onClick={(event) => event.preventDefault()}
+ >
+ {children}
+ </a>
+ );
+}
+
+export const sidebarNoteLinkComponents = {
+ "edgeever-note": SidebarNoteLink,
+} satisfies Record<string, (props: ComponentProps<typeof SidebarNoteLink>) => ReactNode>;