Skip to content

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

fix(editor): preserve external links with file-like labels

tianma-if12daebbd0812
diff --git a/packages/shared/src/content.test.mjs b/packages/shared/src/content.test.mjs
index bed597da..57c05b2b 100644
--- a/packages/shared/src/content.test.mjs
+++ b/packages/shared/src/content.test.mjs
@@ -156,6 +156,46 @@ describe("file attachment Markdown compatibility", () => {
expect(doc.content[0]?.content?.[0]?.type).toBe("text");
});
+ test("keeps a research category ending in .AI as a link, including an older rich-text link", () => {
+ const markdown = "[arXiv cs.AI](https://arxiv.org/list/cs.AI/recent)";
+ const parsed = markdownToDoc(markdown);
+ expect(parsed.content[0]?.content?.[0]).toMatchObject({
+ type: "text",
+ text: "arXiv cs.AI",
+ marks: [{ type: "link", attrs: { href: "https://arxiv.org/list/cs.AI/recent" } }],
+ });
+ expect(resolveMemoContentDoc(parsed, markdown).content[0]?.content?.[0]?.type).toBe("text");
+ expect(markdownToDoc("[arXiv cs\\.AI](https://arxiv.org/list/cs.AI/recent)").content[0]?.content?.[0]?.type)
+ .toBe("text");
+ });
+
+ test("repairs an already saved false attachment card without changing real .ai attachments", () => {
+ const url = "https://arxiv.org/list/cs.AI/recent";
+ const savedDoc = {
+ type: "doc",
+ content: [{
+ type: "paragraph",
+ content: [{ type: FILE_ATTACHMENT_NODE_TYPE, attrs: { label: "arXiv cs.AI", filename: "arXiv cs.AI", url } }],
+ }, {
+ type: "paragraph",
+ content: [{ type: FILE_ATTACHMENT_NODE_TYPE, attrs: { label: "icon.ai", url: "/api/v1/resources/res_ai/blob" } }],
+ }],
+ };
+ const resolved = resolveMemoContentDoc(savedDoc, "");
+ expect(resolved.content[0]?.content?.[0]).toMatchObject({
+ type: "text",
+ text: "arXiv cs.AI",
+ marks: [{ type: "link", attrs: { href: url } }],
+ });
+ expect(resolved.content[1]?.content?.[0]?.type).toBe(FILE_ATTACHMENT_NODE_TYPE);
+ expect(docToMarkdown(resolved).split("\n")[0]).toBe(`[arXiv cs.AI](${url})`);
+ });
+
+ test("recognizes an explicitly marked external attachment", () => {
+ expect(markdownToDoc("[Attachment: icon.ai](https://example.com/download?id=1)").content[0]?.content?.[0]?.type)
+ .toBe(FILE_ATTACHMENT_NODE_TYPE);
+ });
+
test("preserves an empty external link through Markdown and JSON", () => {
const markdown = "Before [](https://example.com/path) and [named](https://example.com).";
const doc = markdownToDoc(markdown);
diff --git a/packages/shared/src/file-attachment.ts b/packages/shared/src/file-attachment.ts
index 06608456..50da5a4a 100644
--- a/packages/shared/src/file-attachment.ts
+++ b/packages/shared/src/file-attachment.ts
@@ -20,11 +20,11 @@ const escapeMarkdownLabel = (value: string) =>
value.replace(/\\/g, "\\\\").replace(/\[/g, "\\[").replace(/\]/g, "\\]");
export const isFileAttachmentLink = (label: string, url: string) => {
- const filename = getAttachmentFilenameFromLabel(label) || label || url;
- if (resolveAttachmentKind(null, filename) === "pdf" || resolveAttachmentKind(null, url) === "pdf") {
+ if (resolveAttachmentKind(null, getAttachmentFilenameFromLabel(label)) === "pdf" || resolveAttachmentKind(null, url) === "pdf") {
return false;
}
- return ATTACHMENT_LABEL_PATTERN.test(label) || RESOURCE_URL_PATTERN.test(url) || resolveAttachmentKind(null, filename) !== "file";
+ // A link title can end in a file extension without linking to a file (for example, "arXiv cs.AI").
+ return ATTACHMENT_LABEL_PATTERN.test(label) || RESOURCE_URL_PATTERN.test(url);
};
export const FileAttachment = Node.create({
@@ -141,6 +141,20 @@ const getLinkAttachmentAttrs = (link: TiptapMark | undefined, label: string) =>
/** Upgrade legacy standalone attachment links while preserving ordinary web links. */
export const upgradeStandaloneFileLinks = (doc: TiptapDoc): TiptapDoc => {
let changed = false;
+ const visitChild = (child: TiptapNode | TiptapTextNode): TiptapNode | TiptapTextNode => {
+ if (child.type === "text") return child;
+ if (child.type === FILE_ATTACHMENT_NODE_TYPE) {
+ const label = typeof child.attrs?.label === "string" ? child.attrs.label : "";
+ const url = typeof child.attrs?.url === "string" ? child.attrs.url : "";
+ const mimeType = typeof child.attrs?.mimeType === "string" ? child.attrs.mimeType : "";
+ const byteSize = normalizeAttachmentByteSize(child.attrs?.byteSize);
+ if (label && url && !mimeType && byteSize === null && !isFileAttachmentLink(label, url)) {
+ changed = true;
+ return { type: "text", text: label, marks: [{ type: "link", attrs: { href: url, target: "_blank" } }] };
+ }
+ }
+ return visit(child);
+ };
const visit = (node: TiptapNode): TiptapNode => {
if (node.type === "paragraph" && node.content?.length === 1) {
const child = node.content[0];
@@ -163,9 +177,7 @@ export const upgradeStandaloneFileLinks = (doc: TiptapDoc): TiptapDoc => {
}
}
}
- return node.content
- ? { ...node, content: node.content.map((child) => child.type === "text" ? child : visit(child)) }
- : node;
+ return node.content ? { ...node, content: node.content.map(visitChild) } : node;
};
const content = doc.content.map(visit);
return changed ? { ...doc, content } : doc;