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): keep extra blank lines in Markdown mode

tianma-ifd21adb72d0e0
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index e3868fc7..b24ec99b 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -2206,10 +2206,10 @@ const RichEditorPane = ({
setTitle(nextTitle);
setTagsText(nextTagsText);
setMobilePlainText(nextMarkdown);
- hydrateMarkdownSource(memo.id, nextContent, nextMarkdown);
+ const keptLiveMarkdown = hydrateMarkdownSource(memo.id, nextContent, nextMarkdown);
setMobilePlainTextElementValue(mobileTextAreaRef.current, nextMarkdown);
- if (isEditorReady(currentEditor) && shouldReplaceDocument) {
+ if (isEditorReady(currentEditor) && shouldReplaceDocument && !keptLiveMarkdown) {
try {
currentEditor.commands.setContent(nextContent);
} catch (err) {
@@ -3010,7 +3010,7 @@ const RichEditorPane = ({
setTitle(nextTitle);
setTagsText(nextTagsText);
setMobilePlainText(nextMarkdown);
- hydrateMarkdownSource(remoteMemo.id, nextContent, nextMarkdown);
+ hydrateMarkdownSource(remoteMemo.id, nextContent, nextMarkdown, { force: true });
setMobilePlainTextElementValue(mobileTextAreaRef.current, nextMarkdown);
const currentEditor = editorRef.current;
diff --git a/apps/web/src/components/editor/editor-draft-state.test.mjs b/apps/web/src/components/editor/editor-draft-state.test.mjs
index e40cf173..08c4cea9 100644
--- a/apps/web/src/components/editor/editor-draft-state.test.mjs
+++ b/apps/web/src/components/editor/editor-draft-state.test.mjs
@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
+import { markdownToDoc } from "@edgeever/shared";
import {
resolveEditorDraftState,
shouldReplaceEditorDocument,
@@ -53,6 +54,30 @@ describe("editor draft source resolution", () => {
expect(state.hasUnsavedChanges).toBe(false);
});
+ test("preserves stored markdown that a JSON roundtrip would collapse", () => {
+ const stored = "时代公馆\n\n\n*提示:表格*";
+ const state = resolveEditorDraftState({
+ memo: {
+ ...memo,
+ contentJson: markdownToDoc(stored),
+ contentMarkdown: stored,
+ },
+ });
+
+ expect(state.contentMarkdown).toBe(stored);
+ });
+
+ test("falls back to serialized markdown when the stored source is empty", () => {
+ const state = resolveEditorDraftState({
+ memo: {
+ ...memo,
+ contentMarkdown: "",
+ },
+ });
+
+ expect(state.contentMarkdown).toBe("remote body");
+ });
+
test("repairs invalid saved gallery images and marks the memo for autosave", () => {
const state = resolveEditorDraftState({
memo: {
@@ -141,6 +166,24 @@ describe("editor draft source resolution", () => {
expect(state.hasUnsavedChanges).toBe(false);
});
+ test("preserves queued markdown that a JSON roundtrip would collapse", () => {
+ const stored = "时代公馆\n\n\n*提示:表格*";
+ const state = resolveEditorDraftState({
+ memo,
+ queuedUpdate: {
+ ...queue,
+ payload: {
+ ...queue.payload,
+ contentJson: markdownToDoc(stored),
+ contentMarkdown: stored,
+ },
+ },
+ });
+
+ expect(state.source).toBe("queue");
+ expect(state.contentMarkdown).toBe(stored);
+ });
+
test("keeps the draft authoritative while its update remains queued", () => {
const state = resolveEditorDraftState({
memo,
diff --git a/apps/web/src/components/editor/editor-draft-state.ts b/apps/web/src/components/editor/editor-draft-state.ts
index 3b6667bf..69aeda27 100644
--- a/apps/web/src/components/editor/editor-draft-state.ts
+++ b/apps/web/src/components/editor/editor-draft-state.ts
@@ -94,7 +94,14 @@ export const resolveEditorDraftState = ({
const repairedSavedContent = source === "memo" && Boolean(
memo.contentJson && normalizeImageGalleries(memo.contentJson) !== memo.contentJson,
);
- const contentMarkdown = docToMarkdown(contentJson);
+ const storedMarkdown = source === "draft"
+ ? null
+ : source === "queue"
+ ? (typeof queuedPayload?.contentMarkdown === "string" ? queuedPayload.contentMarkdown : null)
+ : (typeof memo.contentMarkdown === "string" ? memo.contentMarkdown : null);
+ // Prefer the stored source so Markdown-mode extra blank lines survive
+ // hydration. JSON→Markdown collapses `\n\n\n` into a single paragraph break.
+ const contentMarkdown = storedMarkdown || docToMarkdown(contentJson);
const sourceVersion = source === "draft" && draft
? draft.updatedAt
: source === "queue" && queuedUpdate
diff --git a/apps/web/src/components/editor/editor-mode-content.test.mjs b/apps/web/src/components/editor/editor-mode-content.test.mjs
index 9ce86103..bbe16cc1 100644
--- a/apps/web/src/components/editor/editor-mode-content.test.mjs
+++ b/apps/web/src/components/editor/editor-mode-content.test.mjs
@@ -1,9 +1,11 @@
import { describe, expect, test } from "bun:test";
-import { docToMarkdown } from "@edgeever/shared";
+import { docToMarkdown, markdownToDoc } from "@edgeever/shared";
import {
createMarkdownModeSnapshot,
isMarkdownSourceUnchanged,
resolveMarkdownModeContent,
+ selectMarkdownSourceForDocument,
+ shouldKeepLiveMarkdownSource,
} from "./editor-mode-content.ts";
const tableTaskListDocument = {
@@ -80,4 +82,63 @@ describe("Markdown editor mode content preservation", () => {
expect(isMarkdownSourceUnchanged(snapshot, "memo-2", snapshot.markdownSource)).toBe(false);
expect(resolved).not.toEqual(tableTaskListDocument);
});
+
+ test("keeps extra blank lines that a JSON roundtrip would rewrite", () => {
+ const live = "时代公馆\n\n\n*提示:表格*";
+ const content = markdownToDoc(live);
+ const incomingMarkdown = docToMarkdown(content);
+ const snapshot = createMarkdownModeSnapshot("memo-1", content, live);
+
+ expect(incomingMarkdown).not.toBe(live);
+ expect(shouldKeepLiveMarkdownSource({
+ snapshot,
+ memoId: "memo-1",
+ liveMarkdownSource: live,
+ incomingMarkdown,
+ incomingContent: content,
+ })).toBe(true);
+ expect(selectMarkdownSourceForDocument(snapshot, "memo-1", content, incomingMarkdown)).toBe(live);
+ });
+
+ test("restores the exact markdown source when switching back to an unedited rich document", () => {
+ const live = "LINE_A\n\n\nLINE_B";
+ const content = markdownToDoc(live);
+ const snapshot = createMarkdownModeSnapshot("memo-1", content, live);
+
+ expect(selectMarkdownSourceForDocument(
+ snapshot,
+ "memo-1",
+ JSON.parse(JSON.stringify(content)),
+ docToMarkdown(content),
+ )).toBe(live);
+ });
+
+ test("replaces the live markdown buffer when incoming content actually changed", () => {
+ const live = "时代公馆\n\n\n*提示:表格*";
+ const snapshot = createMarkdownModeSnapshot("memo-1", markdownToDoc(live), live);
+ const incomingMarkdown = "另一段内容";
+ const incomingContent = markdownToDoc(incomingMarkdown);
+
+ expect(shouldKeepLiveMarkdownSource({
+ snapshot,
+ memoId: "memo-1",
+ liveMarkdownSource: live,
+ incomingMarkdown,
+ incomingContent,
+ })).toBe(false);
+ });
+
+ test("does not keep a live buffer that belongs to another memo", () => {
+ const live = "时代公馆\n\n\n*提示:表格*";
+ const content = markdownToDoc(live);
+ const snapshot = createMarkdownModeSnapshot("memo-1", content, live);
+
+ expect(shouldKeepLiveMarkdownSource({
+ snapshot,
+ memoId: "memo-2",
+ liveMarkdownSource: live,
+ incomingMarkdown: docToMarkdown(content),
+ incomingContent: content,
+ })).toBe(false);
+ });
});
diff --git a/apps/web/src/components/editor/editor-mode-content.ts b/apps/web/src/components/editor/editor-mode-content.ts
index f0a0776a..9dafd2f6 100644
--- a/apps/web/src/components/editor/editor-mode-content.ts
+++ b/apps/web/src/components/editor/editor-mode-content.ts
@@ -156,3 +156,62 @@ export const resolveMarkdownModeContent = (
editedProjection,
);
};
+
+/**
+ * After autosave the editor hydrates from JSON. Serialization is lossy (extra
+ * blank lines, ` ` markers). If the incoming document is only a projection
+ * of the live buffer, keep the buffer so the caret and keystrokes stay put.
+ */
+export const shouldKeepLiveMarkdownSource = ({
+ snapshot,
+ memoId,
+ liveMarkdownSource,
+ incomingMarkdown,
+ incomingContent,
+}: {
+ snapshot: MarkdownModeSnapshot | null;
+ memoId: string | null | undefined;
+ liveMarkdownSource: string;
+ incomingMarkdown: string;
+ incomingContent: TiptapDoc;
+}) => {
+ if (!snapshot || !memoId || snapshot.memoId !== memoId) {
+ return false;
+ }
+
+ const live = normalizeMarkdownSource(liveMarkdownSource);
+ const incoming = normalizeMarkdownSource(incomingMarkdown);
+ if (live === incoming) {
+ return true;
+ }
+
+ if (nodesEqual(resolveMarkdownModeContent(snapshot, memoId, liveMarkdownSource), incomingContent)) {
+ return true;
+ }
+
+ // Regenerated Markdown (JSON → Markdown) of equivalent prose must not replace
+ // the author's buffer. A real remote/history body has different parsed text.
+ return incoming === normalizeMarkdownSource(docToMarkdown(incomingContent))
+ && nodesEqual(markdownToDoc(live), markdownToDoc(incoming));
+};
+
+/** Restore the exact source the user last had when the rich document was not edited. */
+export const selectMarkdownSourceForDocument = (
+ snapshot: MarkdownModeSnapshot | null,
+ memoId: string | null | undefined,
+ contentJson: TiptapDoc,
+ serializedMarkdown: string,
+) => {
+ if (!snapshot || !memoId || snapshot.memoId !== memoId) {
+ return serializedMarkdown;
+ }
+
+ if (
+ nodesEqual(snapshot.contentJson, contentJson)
+ || docToMarkdown(snapshot.contentJson) === docToMarkdown(contentJson)
+ ) {
+ return snapshot.markdownSource;
+ }
+
+ return serializedMarkdown;
+};
diff --git a/apps/web/src/components/editor/useEditorMarkdownMode.ts b/apps/web/src/components/editor/useEditorMarkdownMode.ts
index 48e16b35..20bf0c53 100644
--- a/apps/web/src/components/editor/useEditorMarkdownMode.ts
+++ b/apps/web/src/components/editor/useEditorMarkdownMode.ts
@@ -10,6 +10,8 @@ import type { TiptapDoc } from "@edgeever/shared";
import {
createMarkdownModeSnapshot,
resolveMarkdownModeContent,
+ selectMarkdownSourceForDocument,
+ shouldKeepLiveMarkdownSource,
type MarkdownModeSnapshot,
} from "./editor-mode-content";
import { getEditorScrollProgress, restoreEditorScrollProgress } from "./editor-mode-scroll";
@@ -31,6 +33,8 @@ export const useEditorMarkdownMode = ({
}) => {
const [isMarkdownMode, setIsMarkdownMode] = useState(false);
const [markdownSource, setMarkdownSource] = useState("");
+ const markdownSourceRef = useRef(markdownSource);
+ markdownSourceRef.current = markdownSource;
const markdownSourceEditorRef = useRef<MarkdownSourceEditorRef | null>(null);
const markdownModeSnapshotRef = useRef<MarkdownModeSnapshot | null>(null);
@@ -55,19 +59,23 @@ export const useEditorMarkdownMode = ({
return;
}
- hydratingRef.current = true;
- editor.commands.setContent(resolveMarkdownModeContent(
+ const currentMemoId = getMemoId();
+ const content = resolveMarkdownModeContent(
markdownModeSnapshotRef.current,
- getMemoId(),
- markdownSource,
- ));
- markdownModeSnapshotRef.current = null;
+ currentMemoId,
+ markdownSourceRef.current,
+ );
+ hydratingRef.current = true;
+ editor.commands.setContent(content);
+ markdownModeSnapshotRef.current = currentMemoId
+ ? createMarkdownModeSnapshot(currentMemoId, content, markdownSourceRef.current)
+ : null;
setIsMarkdownMode(false);
restoreScrollAfterModeChange("rich", scrollProgress);
window.setTimeout(() => {
hydratingRef.current = false;
}, 0);
- }, [editor, getMemoId, hydratingRef, markdownSource, restoreScrollAfterModeChange]);
+ }, [editor, getMemoId, hydratingRef, restoreScrollAfterModeChange]);
const handleMarkdownModeChange = useCallback(() => {
if (effectiveReadOnly || !isEditorReady(editor)) {
@@ -87,12 +95,20 @@ export const useEditorMarkdownMode = ({
if (!currentMemoId) {
return;
}
- const snapshot = createMarkdownModeSnapshot(
+ const contentJson = editor.getJSON() as TiptapDoc;
+ const serialized = createMarkdownModeSnapshot(currentMemoId, contentJson);
+ const markdown = selectMarkdownSourceForDocument(
+ markdownModeSnapshotRef.current,
+ currentMemoId,
+ contentJson,
+ serialized.markdownSource,
+ );
+ markdownModeSnapshotRef.current = createMarkdownModeSnapshot(
currentMemoId,
- editor.getJSON() as TiptapDoc,
+ contentJson,
+ markdown,
);
- markdownModeSnapshotRef.current = snapshot;
- setMarkdownSource(snapshot.markdownSource);
+ setMarkdownSource(markdown);
setIsMarkdownMode(true);
restoreScrollAfterModeChange("markdown", scrollProgress);
}, [
@@ -111,11 +127,27 @@ export const useEditorMarkdownMode = ({
setIsMarkdownMode(false);
}, []);
- const hydrateMarkdownSource = useCallback((memoId: string, content: TiptapDoc, markdown: string) => {
- setMarkdownSource(markdown);
- markdownModeSnapshotRef.current = isMarkdownMode
- ? createMarkdownModeSnapshot(memoId, content, markdown)
- : null;
+ const hydrateMarkdownSource = useCallback((
+ memoId: string,
+ content: TiptapDoc,
+ markdown: string,
+ options?: { force?: boolean },
+ ) => {
+ const keepLiveSource = !options?.force
+ && isMarkdownMode
+ && shouldKeepLiveMarkdownSource({
+ snapshot: markdownModeSnapshotRef.current,
+ memoId,
+ liveMarkdownSource: markdownSourceRef.current,
+ incomingMarkdown: markdown,
+ incomingContent: content,
+ });
+ const nextSource = keepLiveSource ? markdownSourceRef.current : markdown;
+ if (!keepLiveSource) {
+ setMarkdownSource(markdown);
+ }
+ markdownModeSnapshotRef.current = createMarkdownModeSnapshot(memoId, content, nextSource);
+ return keepLiveSource;
}, [isMarkdownMode]);
const clearMarkdownSnapshot = useCallback(() => {
diff --git a/packages/shared/src/content.test.mjs b/packages/shared/src/content.test.mjs
index dba8ea96..6295f00c 100644
--- a/packages/shared/src/content.test.mjs
+++ b/packages/shared/src/content.test.mjs
@@ -411,6 +411,26 @@ describe("Theme block compatibility", () => {
});
});
+describe("extra blank lines", () => {
+ test("keeps two visual blank lines as an empty paragraph", () => {
+ const doc = markdownToDoc("A\n\n\nB");
+ const emptyParagraphs = doc.content.filter((node) =>
+ node.type === "paragraph" && (!node.content || node.content.length === 0)
+ );
+
+ expect(emptyParagraphs).toHaveLength(1);
+ expect(doc.content.map((node) => node.type)).toEqual(["paragraph", "paragraph", "paragraph"]);
+ });
+
+ test("does not rewrite extra blank lines inside fenced code", () => {
+ const markdown = "before\n\n```\nline\n\n\nline\n```\n\nafter";
+ const doc = markdownToDoc(markdown);
+ const code = doc.content.find((node) => node.type === "codeBlock");
+
+ expect(code?.content?.[0]?.text).toBe("line\n\n\nline");
+ });
+});
+
describe("image gallery compatibility", () => {
const galleryDoc = {
type: "doc",
diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.ts
index 1de2b421..f892f3a3 100644
--- a/packages/shared/src/content.ts
+++ b/packages/shared/src/content.ts
@@ -109,12 +109,29 @@ const markdownManager = createEdgeEverMarkdownManager({
mathematics: createEdgeEverMarkdownMathematics(),
});
+const PROTECTED_MARKDOWN_SEGMENT = /(```[\s\S]*?```|~~~[\s\S]*?~~~|\$\$[\s\S]*?\$\$)/g;
+
+/**
+ * CommonMark treats one or more blank lines as a single paragraph break, so two
+ * visual blank lines disappear on parse. Expand those extra newlines into the
+ * empty-paragraph spacing TipTap already understands, without touching fenced
+ * code or display-math blocks.
+ */
+const expandExtraBlankLinesForParse = (markdown: string) =>
+ markdown.split(PROTECTED_MARKDOWN_SEGMENT).map((segment) => {
+ if (segment.startsWith("```") || segment.startsWith("~~~") || segment.startsWith("$$")) {
+ return segment;
+ }
+ return segment.replace(/\n{3,}/g, (run) => "\n\n".repeat(run.length - 1));
+ }).join("");
+
export const markdownToDoc = (markdown: string): TiptapDoc => {
- if (!markdown.trim()) {
+ const normalized = markdown.replace(/\r\n?/g, "\n");
+ if (!normalized) {
return emptyDoc();
}
- return markdownManager.parse(markdown.replace(/\r\n?/g, "\n")) as TiptapDoc;
+ return markdownManager.parse(expandExtraBlankLinesForParse(normalized)) as TiptapDoc;
};
const docContainsNodeType = (doc: TiptapDoc, nodeType: string): boolean => {