Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): doc-end paste unclosed-fence content loss (#2741)

Nick Gomezb5764094a864
diff --git a/.changeset/unclosed-fence-fixed-point.md b/.changeset/unclosed-fence-fixed-point.md
new file mode 100644
index 000000000..1ce20999e
--- /dev/null
+++ b/.changeset/unclosed-fence-fixed-point.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Pasting source at the end of a document no longer corrupts or loses content when the paste lands against a code fence or a JSX container's closing tag. A paste that glued onto a closing ``` line used to reopen the fence to the end of the document; the editor's serializer then invented a closing fence the document never had, the collaboration bridge saw permanent divergence between the WYSIWYG and source views, and its repeated repair merges could drop lines from both — in multi-user sessions this surfaced as text vanishing for everyone. Documents that end in an unclosed code fence now round-trip byte-exactly (the fence stays unclosed until you type below it), and content pasted directly under a `</Steps>`-style container close is recognized as equivalent to its canonical blank-line-separated form instead of triggering endless repair cycles.
diff --git a/packages/core/schema-snapshot.json b/packages/core/schema-snapshot.json
index 10d862c45..7e82fb8e6 100644
--- a/packages/core/schema-snapshot.json
+++ b/packages/core/schema-snapshot.json
@@ -54,6 +54,10 @@
"sourceStyle": {
"hasDefault": true,
"default": "fenced"
+ },
+ "sourceUnclosedFence": {
+ "hasDefault": true,
+ "default": false
}
},
"content": "text*",
diff --git a/packages/core/src/bridge/normalize.ts b/packages/core/src/bridge/normalize.ts
index e67f33b40..4b8890476 100644
--- a/packages/core/src/bridge/normalize.ts
+++ b/packages/core/src/bridge/normalize.ts
@@ -157,7 +157,10 @@ export function normalizeBridge(s: string): string {
.replace(/^\n+/, '')
.replace(/^[*-]{3,}(?=\n|$)/, '---')
.replace(/(\n)([#>+-]|\d+[.)]|`{3,}|~{3,})/g, '\n\n$2')
- .replace(/^([#>+-].*|\d+[.)].*|`{3,}.*|~{3,}.*)\n([^\n])/gm, '$1\n\n$2')
+ .replace(
+ /^([#>+-].*|\d+[.)].*|`{3,}.*|~{3,}.*|<\/[A-Z][A-Za-z0-9]*>[ \t]*)\n([^\n])/gm,
+ '$1\n\n$2',
+ )
.split('\n'),
);
const tableRowLines = findTableRowLines(lines);
@@ -257,7 +260,8 @@ export function detectAppliedToleranceClasses(left: string, right: string): Brid
}
const blockSepBeforeRe = /\n\n([#>+-]|\d+[.)]|`{3,}|~{3,})/;
- const blockSepAfterRe = /^([#>+-].*|\d+[.)].*|`{3,}.*|~{3,}.*)\n\n[^\n]/m;
+ const blockSepAfterRe =
+ /^([#>+-].*|\d+[.)].*|`{3,}.*|~{3,}.*|<\/[A-Z][A-Za-z0-9]*>[ \t]*)\n\n[^\n]/m;
const beforeLeft = blockSepBeforeRe.test(leftNoBom);
const beforeRight = blockSepBeforeRe.test(rightNoBom);
const afterLeft = blockSepAfterRe.test(leftNoBom);
diff --git a/packages/core/src/extensions/code-block-fidelity.ts b/packages/core/src/extensions/code-block-fidelity.ts
index c53c3086f..4adb0d846 100644
--- a/packages/core/src/extensions/code-block-fidelity.ts
+++ b/packages/core/src/extensions/code-block-fidelity.ts
@@ -34,6 +34,11 @@ export const CodeBlockFidelity = CodeBlock.extend({
// between the fence and the info string. Captured at parse time;
// null = canonical form (mirror opener / flush-left / no gap).
sourceClosingFenceLength: { default: null, rendered: false },
+ // True when the source fence was EOF-truncated (no closing fence,
+ // CommonMark §4.5 auto-close). The serializer omits the closing
+ // fence while the block stays the document's last block, keeping
+ // serialize(parse(x)) a fixed point; else canonical closed form.
+ sourceUnclosedFence: { default: false, rendered: false },
sourceFenceIndent: { default: null, rendered: false },
sourceInfoPadding: { default: null, rendered: false },
// Per-line leading indent bytes of an indented (§4.4) block, captured
diff --git a/packages/core/src/markdown/index.ts b/packages/core/src/markdown/index.ts
index a5827a5d0..8b36807fd 100644
--- a/packages/core/src/markdown/index.ts
+++ b/packages/core/src/markdown/index.ts
@@ -612,6 +612,7 @@ function buildMdastToPmHandlers(
fenceLength: node.data?.sourceFenceLength ?? 3,
sourceStyle: node.data?.sourceStyle ?? 'fenced',
sourceClosingFenceLength: node.data?.sourceClosingFenceLength ?? null,
+ sourceUnclosedFence: node.data?.sourceUnclosedFence ?? false,
sourceFenceIndent: node.data?.sourceFenceIndent ?? null,
sourceInfoPadding: node.data?.sourceInfoPadding ?? null,
sourceIndents: node.data?.sourceIndents ?? null,
@@ -1113,6 +1114,7 @@ function buildPmToMdastHandlers(
sourceFenceLength: pmNode.attrs.fenceLength,
sourceStyle,
sourceClosingFenceLength: pmNode.attrs.sourceClosingFenceLength ?? undefined,
+ sourceUnclosedFence: pmNode.attrs.sourceUnclosedFence === true ? true : undefined,
sourceFenceIndent: pmNode.attrs.sourceFenceIndent ?? undefined,
sourceInfoPadding: pmNode.attrs.sourceInfoPadding ?? undefined,
sourceIndents: pmNode.attrs.sourceIndents ?? undefined,
diff --git a/packages/core/src/markdown/mdast-augmentation.ts b/packages/core/src/markdown/mdast-augmentation.ts
index ad309fe06..a686bd9dd 100644
--- a/packages/core/src/markdown/mdast-augmentation.ts
+++ b/packages/core/src/markdown/mdast-augmentation.ts
@@ -153,6 +153,7 @@ declare module 'mdast' {
sourceFenceChar?: string;
sourceFenceLength?: number;
sourceClosingFenceLength?: number;
+ sourceUnclosedFence?: true;
sourceFenceIndent?: number;
sourceInfoPadding?: number;
sourceStyle?: 'indented' | 'fenced';
diff --git a/packages/core/src/markdown/position-slice.ts b/packages/core/src/markdown/position-slice.ts
index 51f600876..0f399d34c 100644
--- a/packages/core/src/markdown/position-slice.ts
+++ b/packages/core/src/markdown/position-slice.ts
@@ -291,6 +291,13 @@ export function applyPositionSliceToNode(
if (closing && closing[1].length > count) {
node.data.sourceClosingFenceLength = closing[1].length;
}
+
+ if (parent?.type === 'root') {
+ const closerRe = new RegExp(`\\n {0,3}\\${ch}{${count},}[ \\t]*$`);
+ if (!closerRe.test(fenceSlice)) {
+ node.data.sourceUnclosedFence = true;
+ }
+ }
} else {
node.data.sourceStyle = 'indented';
if (parent?.type === 'root') {
diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.ts
index 10bfdfa56..8f4e9a41c 100644
--- a/packages/core/src/markdown/to-markdown-handlers.ts
+++ b/packages/core/src/markdown/to-markdown-handlers.ts
@@ -257,7 +257,7 @@ export const toMarkdownHandlers = {
return ' \n';
},
- code(node) {
+ code(node, parent) {
const value = node.value ?? '';
if (node.data?.sourceStyle === 'indented') {
const lines = value.split('\n');
@@ -293,6 +293,18 @@ export const toMarkdownHandlers = {
const lang = node.lang ?? '';
const langGap = lang && typeof infoGap === 'number' && infoGap >= 1 ? ' '.repeat(infoGap) : '';
const meta = node.meta ? ` ${node.meta}` : '';
+ if (
+ node.data?.sourceUnclosedFence === true &&
+ parent?.type === 'root' &&
+ parent.children[parent.children.length - 1] === node
+ ) {
+ const open = `${fence}${langGap}${lang}${meta}${value.length > 0 ? `\n${value}` : ''}`;
+ if (!indent) return open;
+ return open
+ .split('\n')
+ .map((l) => (l.length > 0 ? `${indent}${l}` : l))
+ .join('\n');
+ }
const body = `${fence}${langGap}${lang}${meta}\n${value}\n${char.repeat(closeLen)}`;
if (!indent) return body;
return body
diff --git a/packages/core/src/schema-snapshot.json b/packages/core/src/schema-snapshot.json
index dbd9ae711..e96b2f091 100644
--- a/packages/core/src/schema-snapshot.json
+++ b/packages/core/src/schema-snapshot.json
@@ -228,6 +228,9 @@
"sourceClosingFenceLength": {
"hasDefault": true
},
+ "sourceUnclosedFence": {
+ "hasDefault": true
+ },
"sourceFenceIndent": {
"hasDefault": true
},