Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): serializer over-escape and list marker glue (#2597)

Nick Gomez4aad5e0de938
diff --git a/.changeset/serializer-overescape-list-glue.md b/.changeset/serializer-overescape-list-glue.md
new file mode 100644
index 000000000..cd14f5a47
--- /dev/null
+++ b/.changeset/serializer-overescape-list-glue.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Two markdown fidelity corrections. The editor no longer writes a redundant backslash into stored source when prose contains interior punctuation like `1)x`, `a@x`, `a{x`, `a![x]`, or an unclosed `a![x` — those characters now round-trip as the literal text you typed. And pasting a list item so it holds a nested list as its first block (common when rearranging to-do lists) no longer emits garbled bytes: the item and its nested list now serialize to a form that re-parses to the same structure, instead of leaking a stray `- [ ]` marker into the item text or silently re-indenting the trailing content.
diff --git a/packages/core/src/markdown/guard-flanking-matrix.json b/packages/core/src/markdown/guard-flanking-matrix.json
index c6f92c8c0..681988650 100644
--- a/packages/core/src/markdown/guard-flanking-matrix.json
+++ b/packages/core/src/markdown/guard-flanking-matrix.json
@@ -384,7 +384,7 @@
"delimiter": "*",
"witness": "*.y.*{b",
"side": "after-close",
- "roundTrip": "\\*.y.\\*\\{b\n"
+ "roundTrip": "\\*.y.\\*{b\n"
},
{
"alphabet": "r23",
@@ -393,7 +393,7 @@
"delimiter": "_",
"witness": "_.y._{b",
"side": "after-close",
- "roundTrip": "\\_.y.\\_\\{b\n"
+ "roundTrip": "\\_.y.\\_{b\n"
},
{
"alphabet": "r23",
@@ -402,7 +402,7 @@
"delimiter": "**",
"witness": "**.y.**{b",
"side": "after-close",
- "roundTrip": "\\*\\*.y.\\*\\*\\{b\n"
+ "roundTrip": "\\*\\*.y.\\*\\*{b\n"
},
{
"alphabet": "r23",
@@ -411,7 +411,7 @@
"delimiter": "~~",
"witness": "~~.y.~~{b",
"side": "after-close",
- "roundTrip": "\\~\\~.y.\\~\\~\\{b\n"
+ "roundTrip": "\\~\\~.y.\\~\\~{b\n"
},
{
"alphabet": "r23",
@@ -420,7 +420,7 @@
"delimiter": "==",
"witness": "==.y.=={b",
"side": "after-close",
- "roundTrip": "==.y.==\\{b\n"
+ "roundTrip": "==.y.=={b\n"
},
{
"alphabet": "backslash-escape",
diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.ts
index e1161455b..f91cd591f 100644
--- a/packages/core/src/markdown/to-markdown-handlers.ts
+++ b/packages/core/src/markdown/to-markdown-handlers.ts
@@ -415,11 +415,43 @@ export const toMarkdownHandlers = {
: contentCol;
const pad = ' '.repeat(padWidth);
const firstGap = child.checked === null || child.checked === undefined ? markerGap : ' ';
- const itemContent = state.containerFlow(child, info);
- const indented = itemContent
- .split('\n')
- .map((l, idx) => (idx === 0 ? `${marker}${firstGap}${l}` : l ? `${pad}${l}` : l))
- .join('\n');
+ const itemChildren = child.children ?? [];
+ const firstBlockIsList = itemChildren[0]?.type === 'list';
+ let indented: string;
+ if (firstBlockIsList) {
+ const isTask = child.checked !== null && child.checked !== undefined;
+ const nestedMd = state.containerFlow({ ...child, children: [itemChildren[0]] }, info);
+ const trailing = itemChildren.slice(1);
+ const trailingMd = trailing.length
+ ? state.containerFlow({ ...child, children: trailing }, info)
+ : '';
+ const nestedIndented = nestedMd
+ .split('\n')
+ .map((l, idx) =>
+ idx === 0 ? (isTask ? `${pad}${l}` : `${marker}${firstGap}${l}`) : l ? `${pad}${l}` : l,
+ )
+ .join('\n');
+ const parts: string[] = [];
+ if (isTask) {
+ parts.push(marker, '', nestedIndented);
+ } else {
+ parts.push(nestedIndented);
+ }
+ if (trailingMd) {
+ const trailingIndented = trailingMd
+ .split('\n')
+ .map((l) => (l ? `${pad}${l}` : l))
+ .join('\n');
+ parts.push('', trailingIndented);
+ }
+ indented = parts.join('\n');
+ } else {
+ const itemContent = state.containerFlow(child, info);
+ indented = itemContent
+ .split('\n')
+ .map((l, idx) => (idx === 0 ? `${marker}${firstGap}${l}` : l ? `${pad}${l}` : l))
+ .join('\n');
+ }
out.push(indented);
}
@@ -796,6 +828,11 @@ function safeText(state: State, value: string, info: Info): string {
if (u.character === '<') return false;
if (u.character === '[') return false;
if (u.character === '(') return false;
+ if (u.character === '!' && u.after === '\\[' && u.inConstruct === 'phrasing') return false;
+ if (u.character === '@' && u.before === '[+\\-.\\w]') return false;
+ if (u.character === '{' && Array.isArray(u.inConstruct) && u.inConstruct.includes('phrasing')) {
+ return false;
+ }
if (u.character === '`') return false;
if (
(u.character === '*' || u.character === '_' || u.character === '~') &&
@@ -812,6 +849,17 @@ function safeText(state: State, value: string, info: Info): string {
}
return true;
});
+ state.unsafe = state.unsafe.map((u) => {
+ if (u.character !== ')' || u.atBreak !== true || u.before !== '\\d+' || u.after !== undefined) {
+ return u;
+ }
+ const narrowed: typeof u & { _compiled?: RegExp | null } = {
+ ...u,
+ after: '(?:[ \t\r\n]|$)',
+ };
+ delete narrowed._compiled;
+ return narrowed;
+ });
let result: string;
try {
result = state.safe(value, info);