Skip to content

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

fix(mobile): render notes with attachment nodes

tianma-if75d5ead4f1ec
diff --git a/apps/mobile/src/components/LocalTiptapEditor.tsx b/apps/mobile/src/components/LocalTiptapEditor.tsx
index c809ea30..036af794 100644
--- a/apps/mobile/src/components/LocalTiptapEditor.tsx
+++ b/apps/mobile/src/components/LocalTiptapEditor.tsx
@@ -74,6 +74,7 @@ import {
isMobileImageUploadPlaceholderSource,
stripMobileImageUploadPlaceholders,
} from "../lib/mobile-image-upload-placeholder";
+import { resolveMobileAttachmentContent } from "../lib/mobile-attachment-content";
import {
getMobileAiSourceRange,
resolveMobileAiSelectionTriggerPosition,
@@ -695,7 +696,7 @@ function LocalTiptapEditorImpl(props: LocalTiptapEditorProps) {
placeholder: getMobileEditorPlaceholder(props.locale),
})]),
],
- content: resolveImageSources(props.content, props.baseUrl),
+ content: resolveImageSources(resolveMobileAttachmentContent(props.content), props.baseUrl),
editorProps: {
attributes: getMobileEditorInputAttributes(
isViewer ? "edgeever-editor-content edgeever-viewer-content" : "edgeever-editor-content"
@@ -745,7 +746,7 @@ function LocalTiptapEditorImpl(props: LocalTiptapEditorProps) {
}
try {
const parsed = JSON.parse(contentJsonSerialized) as EditorDoc;
- const next = resolveImageSources(parsed, props.baseUrl);
+ const next = resolveImageSources(resolveMobileAttachmentContent(parsed), props.baseUrl);
// Do not focus while replacing content. Callers decide when the editor should
// take focus and place the caret via focusEnd().
// This command synchronizes native-owned state (draft restore/template/new
@@ -1291,7 +1292,7 @@ function LocalTiptapEditorImpl(props: LocalTiptapEditorProps) {
if (!editor || editor.isDestroyed || !isViewer) {
return;
}
- const next = resolveImageSources(props.content, props.baseUrl);
+ const next = resolveImageSources(resolveMobileAttachmentContent(props.content), props.baseUrl);
const current = JSON.stringify(editor.getJSON());
const incoming = JSON.stringify(next);
if (current !== incoming) {
diff --git a/apps/mobile/src/lib/mobile-attachment-content.test.ts b/apps/mobile/src/lib/mobile-attachment-content.test.ts
new file mode 100644
index 00000000..fec8ae44
--- /dev/null
+++ b/apps/mobile/src/lib/mobile-attachment-content.test.ts
@@ -0,0 +1,95 @@
+import { describe, expect, test } from "bun:test";
+import { resolveMobileAttachmentContent } from "./mobile-attachment-content";
+
+describe("resolveMobileAttachmentContent", () => {
+ test("converts PDF and file attachment nodes into mobile-safe links", () => {
+ expect(resolveMobileAttachmentContent({
+ type: "doc",
+ content: [
+ {
+ type: "paragraph",
+ content: [{
+ type: "edgeeverPdfAttachment",
+ attrs: {
+ url: "/api/v1/resources/res_pdf/blob",
+ label: "附件:报告.pdf",
+ displayMode: "compact",
+ },
+ }],
+ },
+ {
+ type: "paragraph",
+ content: [{
+ type: "edgeeverFileAttachment",
+ attrs: {
+ url: "/api/v1/resources/res_doc/blob",
+ label: "附件:说明.docx",
+ filename: "说明.docx",
+ mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ },
+ }],
+ },
+ ],
+ })).toEqual({
+ type: "doc",
+ content: [
+ {
+ type: "paragraph",
+ content: [{
+ type: "text",
+ text: "附件:报告.pdf",
+ marks: [{
+ type: "link",
+ attrs: {
+ href: "/api/v1/resources/res_pdf/blob",
+ target: "_blank",
+ class: "edgeever-attachment-link",
+ },
+ }],
+ }],
+ },
+ {
+ type: "paragraph",
+ content: [{
+ type: "text",
+ text: "附件:说明.docx",
+ marks: [{
+ type: "link",
+ attrs: {
+ href: "/api/v1/resources/res_doc/blob",
+ target: "_blank",
+ class: "edgeever-attachment-link",
+ },
+ }],
+ }],
+ },
+ ],
+ });
+ });
+
+ test("preserves surrounding rich content and falls back for incomplete nodes", () => {
+ const originalParagraph = {
+ type: "paragraph",
+ content: [{ type: "text", text: "正文", marks: [{ type: "bold" }] }],
+ };
+ const result = resolveMobileAttachmentContent({
+ type: "doc",
+ content: [
+ originalParagraph,
+ {
+ type: "paragraph",
+ content: [{
+ type: "edgeeverFileAttachment",
+ attrs: { filename: "fallback.zip" },
+ }],
+ },
+ ],
+ });
+
+ expect(result.content[0]).toEqual(originalParagraph);
+ expect(result.content[1]).toEqual({
+ type: "paragraph",
+ content: [{ type: "text", text: "fallback.zip" }],
+ });
+ });
+});
diff --git a/apps/mobile/src/lib/mobile-attachment-content.ts b/apps/mobile/src/lib/mobile-attachment-content.ts
new file mode 100644
index 00000000..a9aa531a
--- /dev/null
+++ b/apps/mobile/src/lib/mobile-attachment-content.ts
@@ -0,0 +1,63 @@
+import {
+ FILE_ATTACHMENT_NODE_TYPE,
+ PDF_ATTACHMENT_NODE_TYPE,
+ type TiptapDoc,
+ type TiptapNode,
+ type TiptapTextNode,
+} from "@edgeever/shared";
+
+const ATTACHMENT_NODE_TYPES = new Set<string>([
+ FILE_ATTACHMENT_NODE_TYPE,
+ PDF_ATTACHMENT_NODE_TYPE,
+]);
+
+const getStringAttr = (node: TiptapNode, key: string) => {
+ const value = node.attrs?.[key];
+ return typeof value === "string" ? value : "";
+};
+
+const toLegacyAttachmentLink = (node: TiptapNode): TiptapTextNode => {
+ const url = getStringAttr(node, "url");
+ const label = getStringAttr(node, "label") || getStringAttr(node, "filename") || "Attachment";
+
+ return {
+ type: "text",
+ text: label,
+ ...(url
+ ? {
+ marks: [{
+ type: "link",
+ attrs: {
+ href: url,
+ target: "_blank",
+ class: "edgeever-attachment-link",
+ },
+ }],
+ }
+ : {}),
+ };
+};
+
+/**
+ * The native WebView editor intentionally keeps the stable link-based attachment
+ * schema used by its resource action bridge. Convert richer cross-client nodes at
+ * the boundary so Tiptap never rejects an otherwise valid note as unknown content.
+ */
+export const resolveMobileAttachmentContent = (doc: TiptapDoc): TiptapDoc => {
+ const visit = (node: TiptapNode | TiptapTextNode): TiptapNode | TiptapTextNode => {
+ if (ATTACHMENT_NODE_TYPES.has(node.type)) {
+ return toLegacyAttachmentLink(node as TiptapNode);
+ }
+
+ if ("content" in node && node.content) {
+ return { ...node, content: node.content.map(visit) };
+ }
+
+ return node;
+ };
+
+ return {
+ ...doc,
+ content: doc.content.map((node) => visit(node) as TiptapNode),
+ };
+};