[Bug]: deserializeMd keeps `\r` at the end of code-block lines for CRLF input, and serializeMd writes mixed line endings inside the fence

未关闭 适合新手
#5,322 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
backend

调研方向

从 packages/markdown/src/lib/rules/defaultRules.ts 中的 code_block 反序列化规则开始,查看报告中提到的 .split('\n') 附近。比较复现中的 CRLF 和 LF 输入,并检查现有的 Markdown 代码块测试。完成标准是:CRLF 输入生成与 LF 输入相同的代码行文本和序列化输出,同时 LF 行为保持不变。

由索引模型根据 Issue 内容生成。

描述

Bug type

Behavior bug (incorrect output/state without crash)

Browser surface

No

Owner surface

Plate framework or plugin

Summary

For a fenced code block with CRLF line endings, deserializeMd keeps \r at the end of every code line except the last, and serializeMd then writes those lines with CRLF inside an LF fence.

Public reproduction URL

https://gist.github.com/venmakli/b48c1f9fe4cc9318e54515b5da0fcc74

Steps to reproduce
  1. Download the reproduction (package.json and repro.ts).
  2. Run bun install && bun repro.ts.
  3. For the CRLF input the first code_line is "const a = 1;\r", and the serialized fence contains const a = 1;\r\n between LF lines. The LF input gives clean lines.
Expected behavior

CRLF is a line ending (CommonMark spec, "Characters and lines"), so the CRLF input gives the same value as its LF version, which repro.ts prints next to it: code lines "const a = 1;" and "const b = 2;". Serialization gives the same output as for the LF version: ```js\nconst a = 1;\nconst b = 2;\n```\n.

Actual behavior

On @platejs/markdown 53.3.18 (output of repro.ts):

"```js\r\nconst a = 1;\r\nconst b = 2;\r\n```\r\n"
  value:     [{"children":[{"children":[{"text":"const a = 1;\r"}],"type":"code_line"},{"children":[{"text":"const b = 2;"}],"type":"code_line"}],"lang":"js","type":"code_block"}]
  serialize: "```js\nconst a = 1;\r\nconst b = 2;\n```\n"
"```js\nconst a = 1;\nconst b = 2;\n```\n"
  value:     [{"children":[{"children":[{"text":"const a = 1;"}],"type":"code_line"},{"children":[{"text":"const b = 2;"}],"type":"code_line"}],"lang":"js","type":"code_block"}]
  serialize: "```js\nconst a = 1;\nconst b = 2;\n```\n"

The \r is part of the code-line text in the editor value. The serialized fence mixes CRLF (inside) and LF (fence and last line).

Acceptance criteria
  • For CRLF input, no code_line text contains \r from a line ending; the value equals the value for the LF version of the same input.
  • serializeMd output for that value equals the output for the LF version: no \r inside the fence.
  • Output for LF input is unchanged.
Regression range
last-known-good: NOT_ENOUGH_INFO
first-known-bad: NOT_ENOUGH_INFO
evidence: >
  Same value and output observed on @udecode/plate-markdown 47.0.4 (with @udecode/plate 46.0.10),
  @platejs/markdown 53.1.5 and 53.3.18. The code_block rule splits `mdastNode.value` with `.split('\n')`
  (v53.3.18 packages/markdown/src/lib/rules/defaultRules.ts:296; the same split is in the 47.0.4 dist).
  git blame at v53.3.18 points to the lint commit d13624190 (PR #4732), so the split is older; the introducing commit was not identified.
Reproducibility

Always

Plate / Plite / React versions
plate: "@platejs/markdown 53.3.18, platejs 53.3.18 (mdast-util-from-markdown 2.1.0, mdast-util-to-markdown 2.2.1)"
plite: not used
plite-react: not used
react: not used
Operating system

macOS 15 (Darwin 24.6.0)

Install method

bun 1.4.4: bun install && bun repro.ts

Framework / app setup

Headless createSlateEditor with MarkdownPlugin only, no React.

Affected packages or entry points

@platejs/markdown: editor.api.markdown.deserialize / deserializeMd (code_block rule), editor.api.markdown.serialize.

Minimal code or config excerpt
import { MarkdownPlugin } from '@platejs/markdown';
import { createSlateEditor } from 'platejs';

const editor = createSlateEditor({ plugins: [MarkdownPlugin] });

for (const input of ['```js\r\nconst a = 1;\r\nconst b = 2;\r\n```\r\n', '```js\nconst a = 1;\nconst b = 2;\n```\n']) {
  const value = editor.api.markdown.deserialize(input);
  editor.tf.setValue(value);
  console.log(JSON.stringify(input));
  console.log('  value:    ', JSON.stringify(value));
  console.log('  serialize:', JSON.stringify(editor.api.markdown.serialize()));
}
Logs, screenshots, and evidence
# mdast from mdast-util-from-markdown 2.1.0 for the CRLF input: the code value keeps CRLF
"const a = 1;\r\nconst b = 2;"

# @udecode/plate-markdown 47.0.4 + @udecode/plate 46.0.10, CRLF input
[{"children":[{"children":[{"text":"const a = 1;\r"}],"type":"code_line"},{"children":[{"text":"const b = 2;"}],"type":"code_line"}],"lang":"js","type":"code_block"}]
"```js\nconst a = 1;\r\nconst b = 2;\n```\n"
Maintainer Codex context
route: N/A (headless)
local command: bun install && bun repro.ts
fixture/data: the two inputs in repro.ts
rule location: >
  v53.3.18 and main (170d73db6, 2026-10-23): packages/markdown/src/lib/rules/defaultRules.ts:296 (code_block deserialize).
  next: not checked; the code-block plugin lives in packages/platejs/src/features/code-block there.
known blocker: none
Impact and severity
  • Affected: @platejs/markdown users who deserialize Markdown files with CRLF line endings that contain fenced code blocks. Observed on Windows-style input bytes; the OS running the code does not matter.
  • Severity: data risk inside code. Code text in the editor carries a hidden \r, and the written file gets mixed line endings.
  • Frequency: always for CRLF input with a code block of two or more lines.
  • Consequence: diffs of the Markdown file show changed code lines, and copied code keeps the stray \r.
Additional information

Paragraph and blockquote text under CRLF is a separate report (lone \r and extra \ after PR #5026). This report is only about code-block lines. LF output between blocks (mdast-util-to-markdown always joins with \n) is not part of it.

主要语言
TypeScript
星标
16.6k
派生
993
平均合并
20 小时 2 分钟
30 天内合并 PR
62

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

udecode/plate 的其他 Issue

查看 udecode/plate 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。