Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): escape user-typed whitespace char-refs on serialize (#2716)

Nick Gomeza5d0b25c931c
diff --git a/.changeset/typed-whitespace-charref-preserved.md b/.changeset/typed-whitespace-charref-preserved.md
new file mode 100644
index 000000000..80952fe9c
--- /dev/null
+++ b/.changeset/typed-whitespace-charref-preserved.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Typing or pasting a literal whitespace character reference such as ` ` (space), `	` (tab), or ` ` (non-breaking space) into the editor no longer silently turns it into the actual whitespace character. Previously these typed characters were stored unescaped, so on the next reopen or sync every reader saw an invisible space instead of the text you typed. They now survive edit, save, and reopen exactly as written. Spaces the editor itself preserves at bold/italic boundaries are unaffected, and existing files on disk keep their exact bytes.
diff --git a/packages/core/src/markdown/guard-flanking-matrix.json b/packages/core/src/markdown/guard-flanking-matrix.json
index 681988650..83749d685 100644
--- a/packages/core/src/markdown/guard-flanking-matrix.json
+++ b/packages/core/src/markdown/guard-flanking-matrix.json
@@ -150,7 +150,7 @@
"delimiter": "==",
"witness": "==.y.==Ab",
"side": "after-close",
- "roundTrip": "==.y.==\\Ab\n"
+ "roundTrip": "==.y.==Ab\n"
},
{
"alphabet": "entity-ref",
@@ -195,7 +195,7 @@
"delimiter": "==",
"witness": "aA==.y.==",
"side": "before-open",
- "roundTrip": "a\\A==.y.==\n"
+ "roundTrip": "aA==.y.==\n"
},
{
"alphabet": "r23",
diff --git a/packages/core/src/markdown/highlight-promoter.ts b/packages/core/src/markdown/highlight-promoter.ts
index f758cf737..f55912407 100644
--- a/packages/core/src/markdown/highlight-promoter.ts
+++ b/packages/core/src/markdown/highlight-promoter.ts
@@ -41,6 +41,24 @@ function getLastChar(node: PhrasingContent): string | null {
return null;
}
+interface EntityRefSpan {
+ offset: number;
+ length: number;
+ raw: string;
+}
+
+function sliceTextNode(source: Text, from: number, to: number): Text {
+ const out: Text = { type: 'text', value: source.value.slice(from, to) };
+ const spans = (source.data as { entityRefSpans?: EntityRefSpan[] } | undefined)?.entityRefSpans;
+ if (spans?.length) {
+ const inside = spans
+ .filter((s) => s.offset >= from && s.offset + s.length <= to)
+ .map((s) => ({ ...s, offset: s.offset - from }));
+ if (inside.length > 0) out.data = { entityRefSpans: inside } as Text['data'];
+ }
+ return out;
+}
+
function promoteCrossChildren(parent: Parent): void {
const children = parent.children as PhrasingContent[];
let outerI = 0;
@@ -146,9 +164,11 @@ function promoteCrossChildren(parent: Parent): void {
const tailValue = closeChild.value.slice(closePos + 2);
const bodyChildren: PhrasingContent[] = [];
- if (openTrailing.length > 0) bodyChildren.push({ type: 'text', value: openTrailing });
+ if (openTrailing.length > 0) {
+ bodyChildren.push(sliceTextNode(openChild, openPos + 2, openValue.length));
+ }
for (let k = outerI + 1; k < closeChildIdx; k++) bodyChildren.push(children[k]);
- if (closeLeading.length > 0) bodyChildren.push({ type: 'text', value: closeLeading });
+ if (closeLeading.length > 0) bodyChildren.push(sliceTextNode(closeChild, 0, closePos));
if (!bodyCrossesInline) {
outerI++;
@@ -161,9 +181,11 @@ function promoteCrossChildren(parent: Parent): void {
};
const replacement: PhrasingContent[] = [];
- if (leadValue.length > 0) replacement.push({ type: 'text', value: leadValue });
+ if (leadValue.length > 0) replacement.push(sliceTextNode(openChild, 0, openPos));
replacement.push(markNode as unknown as PhrasingContent);
- if (tailValue.length > 0) replacement.push({ type: 'text', value: tailValue });
+ if (tailValue.length > 0) {
+ replacement.push(sliceTextNode(closeChild, closePos + 2, closeChild.value.length));
+ }
children.splice(outerI, closeChildIdx - outerI + 1, ...replacement);
outerI += (leadValue.length > 0 ? 1 : 0) + 1;
@@ -203,12 +225,12 @@ export function highlightPromoterPlugin() {
const start = match.index;
const end = start + match[0].length;
if (start > cursor) {
- const lead: Text = { type: 'text', value: value.slice(cursor, start) };
+ const lead = sliceTextNode(node, cursor, start);
const pos = deriveFragmentPosition(source, node, cursor, start);
if (pos) lead.position = pos;
replacements.push(lead);
}
- const innerText: Text = { type: 'text', value: match[1] };
+ const innerText = sliceTextNode(node, start + 2, end - 2);
const innerPos = deriveFragmentPosition(source, node, start + 2, end - 2);
if (innerPos) innerText.position = innerPos;
const markNode: MarkMdast = {
@@ -221,7 +243,7 @@ export function highlightPromoterPlugin() {
cursor = end;
}
if (cursor < value.length) {
- const tail: Text = { type: 'text', value: value.slice(cursor) };
+ const tail = sliceTextNode(node, cursor, value.length);
const pos = deriveFragmentPosition(source, node, cursor, value.length);
if (pos) tail.position = pos;
replacements.push(tail);
diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.ts
index 052f4a9fd..10bfdfa56 100644
--- a/packages/core/src/markdown/to-markdown-handlers.ts
+++ b/packages/core/src/markdown/to-markdown-handlers.ts
@@ -4,6 +4,7 @@ import type { Handle, Info, State } from 'mdast-util-to-markdown';
import { classifyCharacter } from 'micromark-util-classify-character';
import { isValidSourceLiteralRaw } from '../extensions/source-literal-mark.ts';
import { TO_MARKDOWN_EXT } from './remark-mdx-agnostic.ts';
+import { isInlineWhitespaceNumericCharRef } from './whitespace-char-ref.ts';
declare module 'mdast-util-to-markdown' {
interface ConstructNameMap {
@@ -826,6 +827,16 @@ function isWhitespaceNumericCharRef(body: string): boolean {
);
}
+const TYPED_WS_REF_PUA = '';
+
+const NUMERIC_CHAR_REF_TOKEN_RE = /&#(?:x[0-9A-Fa-f]+|X[0-9A-Fa-f]+|[0-9]+);/g;
+
+function markTypedInlineWhitespaceRefs(value: string): string {
+ return value.replace(NUMERIC_CHAR_REF_TOKEN_RE, (ref) =>
+ isInlineWhitespaceNumericCharRef(ref) ? TYPED_WS_REF_PUA + ref.slice(1) : ref,
+ );
+}
+
function safeText(state: State, value: string, info: Info): string {
const originalUnsafe = state.unsafe;
state.unsafe = originalUnsafe.filter((u) => {
@@ -867,10 +878,11 @@ function safeText(state: State, value: string, info: Info): string {
});
let result: string;
try {
- result = state.safe(value, info);
+ result = state.safe(markTypedInlineWhitespaceRefs(value), info);
} finally {
state.unsafe = originalUnsafe;
}
+ result = result.replaceAll(TYPED_WS_REF_PUA, '\\&');
return escapeEntityAmpersands(escapeActiveDelimiterRuns(result, info));
}