diff --git a/.changeset/code-block-patch.md b/.changeset/code-block-patch.md new file mode 100644 index 0000000000..ff009d5b0d --- /dev/null +++ b/.changeset/code-block-patch.md @@ -0,0 +1,6 @@ +--- +'@platejs/code-block': patch +--- + +- Fixed normalizer using `setNodes` instead of `wrapNodes` for bare text children in `code_block`, which produced malformed `code_line` nodes (e.g. `{ text: "...", type: "code_line" }` instead of proper element structure). This caused syntax highlighting to only work on the first line. +- Fixed `insertBreak` not resetting the decoration cache after splitting, so new lines now get syntax highlighting immediately. diff --git a/packages/code-block/src/lib/withCodeBlock.ts b/packages/code-block/src/lib/withCodeBlock.ts index e83cfa88af..9d700acffc 100644 --- a/packages/code-block/src/lib/withCodeBlock.ts +++ b/packages/code-block/src/lib/withCodeBlock.ts @@ -53,6 +53,9 @@ export const withCodeBlock: OverrideEditor = (ctx) => { indentDepth, }); + // Reset decoration cache so all lines get re-highlighted + resetCodeBlockDecorations(codeBlock[0] as TCodeBlockElement); + return true; }; diff --git a/packages/code-block/src/lib/withNormalizeCodeBlock.tsx b/packages/code-block/src/lib/withNormalizeCodeBlock.tsx index 665f32afd6..9a82ca129c 100644 --- a/packages/code-block/src/lib/withNormalizeCodeBlock.tsx +++ b/packages/code-block/src/lib/withNormalizeCodeBlock.tsx @@ -45,7 +45,13 @@ export const withNormalizeCodeBlock: OverrideEditor = ({ ); if (nonCodeLine) { - editor.tf.setNodes({ type: codeLineType }, { at: nonCodeLine[1] }); + // Use wrapNodes instead of setNodes to properly wrap bare text + // children in code_line elements (setNodes just adds a type + // property to text leaves, producing malformed nodes) + editor.tf.wrapNodes( + { type: codeLineType, children: [] }, + { at: nonCodeLine[1] } + ); } } },