Skip to content

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

fix: use TipTap Markdown converter (#38)

tianma-if769e6d4ab447
diff --git a/bun.lock b/bun.lock
index f22a954c..4b8cf329 100644
--- a/bun.lock
+++ b/bun.lock
@@ -158,6 +158,11 @@
"packages/shared": {
"name": "@edgeever/shared",
"version": "0.1.3",
+ "dependencies": {
+ "@tiptap/extension-image": "3.27.1",
+ "@tiptap/markdown": "3.27.1",
+ "@tiptap/starter-kit": "3.27.1",
+ },
},
},
"packages": {
@@ -1073,6 +1078,8 @@
"@tiptap/extensions": ["@tiptap/[email protected]", "", { "peerDependencies": { "@tiptap/core": "3.27.1", "@tiptap/pm": "3.27.1" } }, "sha512-1Tdx9faw8k0/83V6X+xCDVhV8yElGt95JxeW3YMkKQJI56QdlPz0xOdJPlMiSGJKinPyVier+x9LJD/YZUZIaw=="],
+ "@tiptap/markdown": ["@tiptap/[email protected]", "", { "dependencies": { "marked": "^17.0.1" }, "peerDependencies": { "@tiptap/core": "3.27.1", "@tiptap/pm": "3.27.1" } }, "sha512-4m2LZcj/uN0uLlGnXr3i7sfdxbQNNmeMn4wFyFrP2xpshcKPL5WujUAcqT2rgKfaDjKQ8gIK/GpgSQKuRENFwg=="],
+
"@tiptap/pm": ["@tiptap/[email protected]", "", { "dependencies": { "prosemirror-changeset": "^2.3.0", "prosemirror-commands": "^1.6.2", "prosemirror-dropcursor": "^1.8.1", "prosemirror-gapcursor": "^1.3.2", "prosemirror-history": "^1.4.1", "prosemirror-inputrules": "^1.4.0", "prosemirror-keymap": "^1.2.3", "prosemirror-model": "^1.25.7", "prosemirror-schema-list": "^1.5.0", "prosemirror-state": "^1.4.4", "prosemirror-tables": "^1.8.0", "prosemirror-transform": "^1.12.0", "prosemirror-view": "^1.41.8" } }, "sha512-Ffjx+vimmBU7zH/KrpXzJid3+pziCe/VL2aexSTP63cyQwKQ65LkFkCKaIsSpFdQQuakVZBGWjCA5RoBV852pw=="],
"@tiptap/react": ["@tiptap/[email protected]", "", { "dependencies": { "@types/use-sync-external-store": "^0.0.6", "fast-equals": "^5.3.3", "use-sync-external-store": "^1.4.0" }, "optionalDependencies": { "@tiptap/extension-bubble-menu": "^3.27.1", "@tiptap/extension-floating-menu": "^3.27.1" }, "peerDependencies": { "@tiptap/core": "3.27.1", "@tiptap/pm": "3.27.1", "@types/react": "^17.0.0 || ^18.0.0 || ^19.0.0", "@types/react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0", "react": "^17.0.0 || ^18.0.0 || ^19.0.0", "react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-/Wn2fc9zMtX08MXYScDFsm4wJ8lzfhfPEdbtls7WCDlbtrop48PWlkHDBBJrywARfAQTB2mFs9KiFy9yrQm5Lg=="],
@@ -3165,6 +3172,8 @@
"@testing-library/dom/pretty-format": ["[email protected]", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="],
+ "@tiptap/markdown/marked": ["[email protected]", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-gB0gkNafnonOw0obSTEGZTT86IuhILt2Wfx0mWH/1Au83kybTayroZ/V6nS25mN7u8ASy+5fMhgB3XPNrOZdmA=="],
+
"ansi-align/string-width": ["[email protected]", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="],
"ansi-escapes/type-fest": ["[email protected]", "", {}, "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w=="],
diff --git a/packages/shared/package.json b/packages/shared/package.json
index 6d005536..5d072270 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -6,5 +6,10 @@
"exports": {
".": "./src/index.ts"
},
- "types": "./src/index.ts"
+ "types": "./src/index.ts",
+ "dependencies": {
+ "@tiptap/extension-image": "3.27.1",
+ "@tiptap/markdown": "3.27.1",
+ "@tiptap/starter-kit": "3.27.1"
+ }
}
diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.ts
index a3166144..e9980cde 100644
--- a/packages/shared/src/content.ts
+++ b/packages/shared/src/content.ts
@@ -1,6 +1,16 @@
+import Image from "@tiptap/extension-image";
+import { Markdown, MarkdownManager } from "@tiptap/markdown";
+import StarterKit from "@tiptap/starter-kit";
+
export type TiptapTextNode = {
type: "text";
text: string;
+ marks?: TiptapMark[];
+};
+
+export type TiptapMark = {
+ type: string;
+ attrs?: Record<string, unknown>;
};
export type TiptapNode = {
@@ -21,86 +31,16 @@ export const emptyDoc = (): TiptapDoc => ({
content: [{ type: "paragraph" }],
});
-export const markdownToDoc = (markdown: string): TiptapDoc => {
- const lines = markdown.replace(/\r\n/g, "\n").split("\n");
- const content: TiptapNode[] = [];
-
- for (let index = 0; index < lines.length; ) {
- if (!lines[index].trim()) {
- index += 1;
- continue;
- }
-
- const fence = /^```([^\s`]*)\s*$/.exec(lines[index].trim());
-
- if (fence) {
- const codeLines: string[] = [];
- index += 1;
-
- while (index < lines.length && lines[index].trim() !== "```") {
- codeLines.push(lines[index]);
- index += 1;
- }
-
- if (index < lines.length) {
- index += 1;
- }
-
- content.push({
- type: "codeBlock",
- attrs: { language: fence[1] || "plaintext" },
- content: [{ type: "text", text: codeLines.join("\n") }],
- });
- continue;
- }
-
- const blockLines: string[] = [];
- while (index < lines.length && lines[index].trim()) {
- blockLines.push(lines[index]);
- index += 1;
- }
-
- const block = blockLines.join("\n").trim();
- const heading = /^(#{1,3})\s+(.+)$/.exec(block);
- const image = /^!\[([^\]]*)\]\((\S+?)(?:\s+"([^"]+)")?\)$/.exec(block);
-
- if (heading) {
- content.push({
- type: "heading",
- attrs: { level: heading[1].length },
- content: [{ type: "text", text: heading[2] }],
- });
- continue;
- }
-
- if (image) {
- content.push({
- type: "image",
- attrs: {
- src: image[2],
- alt: image[1] || null,
- title: image[3] || null,
- },
- });
- continue;
- }
-
- if (/^-{3,}$/.test(block)) {
- content.push({ type: "horizontalRule" });
- continue;
- }
-
- content.push({
- type: "paragraph",
- content: [{ type: "text", text: block }],
- });
- }
+const markdownManager = new MarkdownManager({
+ extensions: [StarterKit, Image, Markdown],
+});
- if (content.length === 0) {
+export const markdownToDoc = (markdown: string): TiptapDoc => {
+ if (!markdown.trim()) {
return emptyDoc();
}
- return { type: "doc", content };
+ return markdownManager.parse(markdown.replace(/\r\n?/g, "\n")) as TiptapDoc;
};
export const docToText = (doc: unknown): string => {
@@ -151,147 +91,7 @@ export const docToMarkdown = (doc: unknown): string => {
return "";
}
- return root.content
- .map((node) => blockToMarkdown(node))
- .filter(Boolean)
- .join("\n\n");
-};
-
-const blockToMarkdown = (node: unknown): string => {
- if (!node || typeof node !== "object") {
- return "";
- }
-
- const current = node as {
- type?: unknown;
- attrs?: Record<string, unknown>;
- content?: unknown;
- text?: unknown;
- };
-
- if (current.type === "heading") {
- const level = typeof current.attrs?.level === "number" ? current.attrs.level : 1;
- const text = inlineToMarkdown(current.content);
- return text ? `${"#".repeat(Math.min(Math.max(level, 1), 6))} ${text}` : "";
- }
-
- if (current.type === "image") {
- return imageToMarkdown(current.attrs);
- }
-
- if (current.type === "horizontalRule") {
- return "---";
- }
-
- if (current.type === "bulletList" && Array.isArray(current.content)) {
- return current.content
- .map((item) => inlineToMarkdown((item as { content?: unknown })?.content))
- .filter(Boolean)
- .map((item) => `- ${item.replace(/\n/g, "\n ")}`)
- .join("\n");
- }
-
- if (current.type === "orderedList" && Array.isArray(current.content)) {
- return current.content
- .map((item, index) => {
- const text = inlineToMarkdown((item as { content?: unknown })?.content);
- return text ? `${index + 1}. ${text.replace(/\n/g, "\n ")}` : "";
- })
- .filter(Boolean)
- .join("\n");
- }
-
- if (current.type === "blockquote") {
- const text = inlineToMarkdown(current.content);
- return text
- .split("\n")
- .map((line) => `> ${line}`)
- .join("\n");
- }
-
- if (current.type === "codeBlock") {
- const language = getStringAttr(current.attrs, "language");
- const languageSuffix = language && language !== "plaintext" ? language : "";
- const code = contentToPlainText(current.content);
- return `\`\`\`${languageSuffix}\n${code}\n\`\`\``;
- }
-
- return inlineToMarkdown(current.content);
-};
-
-const contentToPlainText = (content: unknown): string => {
- if (!Array.isArray(content)) {
- return "";
- }
-
- return content
- .map((node) => {
- if (!node || typeof node !== "object") {
- return "";
- }
-
- const current = node as { type?: unknown; text?: unknown; content?: unknown };
-
- if (typeof current.text === "string") {
- return current.text;
- }
-
- if (current.type === "hardBreak") {
- return "\n";
- }
-
- return contentToPlainText(current.content);
- })
- .join("");
-};
-
-const inlineToMarkdown = (content: unknown): string => {
- if (!Array.isArray(content)) {
- return "";
- }
-
- return content
- .map((node) => {
- if (!node || typeof node !== "object") {
- return "";
- }
-
- const current = node as {
- type?: unknown;
- text?: unknown;
- attrs?: Record<string, unknown>;
- content?: unknown;
- };
-
- if (typeof current.text === "string") {
- return current.text;
- }
-
- if (current.type === "hardBreak") {
- return "\n";
- }
-
- if (current.type === "image") {
- return imageToMarkdown(current.attrs);
- }
-
- return inlineToMarkdown(current.content);
- })
- .join("");
-};
-
-const imageToMarkdown = (attrs: Record<string, unknown> | undefined): string => {
- const src = getStringAttr(attrs, "src");
-
- if (!src) {
- return "";
- }
-
- const alt = getStringAttr(attrs, "alt");
- const title = getStringAttr(attrs, "title");
- const titleSuffix = title ? ` "${title.replace(/"/g, '\\"')}"` : "";
-
- return `![${alt.replace(/\]/g, "\\]")}](${src}${titleSuffix})`;
+ return markdownManager.serialize(doc as Parameters<typeof markdownManager.serialize>[0]);
};
const getStringAttr = (attrs: Record<string, unknown> | undefined, key: string) => {
diff --git a/tests/content-markdown.test.ts b/tests/content-markdown.test.ts
new file mode 100644
index 00000000..42157f5e
--- /dev/null
+++ b/tests/content-markdown.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, test } from "bun:test";
+import { docToMarkdown, markdownToDoc } from "@edgeever/shared";
+
+describe("shared Markdown conversion", () => {
+ test("parses adjacent headings, lists, quotes, and inline formatting into TipTap nodes", () => {
+ const doc = markdownToDoc(`## 1. 公司主体信息 (Organization Info)
+* **公司中文全称**:郑州市冰硅网络科技有限公司
+* **公司英文名称**:Example Co., Ltd.
+## 2. 联系方式 (Domain & Contact)
+* **项目官方网站**:[EdgeEver](https://www.edgeever.org)
+> [!WARNING]
+> 请勿绑定其他账号。`);
+
+ expect(doc.content.map((node) => node.type)).toEqual([
+ "heading",
+ "bulletList",
+ "heading",
+ "bulletList",
+ "blockquote",
+ ]);
+
+ const firstList = doc.content[1];
+ const firstText = firstList.content?.[0]?.content?.[0]?.content?.[0];
+ expect(firstText).toEqual({
+ type: "text",
+ text: "公司中文全称",
+ marks: [{ type: "bold" }],
+ });
+
+ const secondList = doc.content[3];
+ const linkText = secondList.content?.[0]?.content?.[0]?.content?.[2];
+ expect(linkText).toMatchObject({
+ type: "text",
+ text: "EdgeEver",
+ marks: [{ type: "link", attrs: { href: "https://www.edgeever.org" } }],
+ });
+ });
+
+ test("serializes TipTap marks and block nodes back to Markdown", () => {
+ const markdown = `# 标题
+
+- **粗体**与*斜体*
+- [链接](https://example.com)
+
+> 引用
+
+\`code\` 和 ~~删除~~`;
+
+ expect(docToMarkdown(markdownToDoc(markdown))).toBe(markdown);
+ });
+
+ test("preserves fenced code blocks and standalone images", () => {
+ const markdown = `\`\`\`ts
+const answer = 42;
+\`\`\`
+
+![示例](/api/v1/resources/res_1/blob "标题")`;
+
+ const doc = markdownToDoc(markdown);
+ expect(doc.content.map((node) => node.type)).toEqual(["codeBlock", "image"]);
+ expect(docToMarkdown(doc)).toBe(markdown);
+ });
+});