Markup round-trip drops fence language in "``` bash", trailing "\#" in headings, and "\|" inside inline code in table cells

Đang mở Phù hợp với người mới
#1,452 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
74/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, markdown, typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Bắt đầu từ ba vị trí mà issue nêu tên: CodeBlockSpecs/index.ts (cắt tỉa fence info string trước split), HeadingSpecs/utils.ts (headingToMarkdown phải escape một # ở cuối), và CodeSpecs (escape | bên trong inline code trong ô bảng). Chạy repro.js được cung cấp với package để thấy từng chuỗi đã lưu được parse lại thành tài liệu bị hỏng. Hoàn thành nghĩa là cả ba trường hợp round-trip giữ nguyên ngôn ngữ của code block, hash ở cuối heading và các ô bảng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Parsing Markdown into the WYSIWYG document and serializing it back (markupParser.parse -> serializer.serialize, used when opening Markdown in the default WYSIWYG mode and saving or switching to Markup mode) corrupts the document in three cases:

  1. A fenced code block with leading whitespace in its info string (``` bash) loses its language ("bash" becomes "").
  2. A heading ending in whitespace and an escaped hash (# not heading \#) is serialized without the backslash (# not heading #), so the trailing # is stripped as a closing ATX sequence on the next parse.
  3. Inline code containing \| inside a table cell (| `x \| y` | z |) is serialized with an unescaped pipe (|`x | y`|z|), so the next parse splits the cell at | and drops trailing cells.

Playground (opens in WYSIWYG mode; switch to Markdown markup via the gear menu to see the serialized output, then back to WYSIWYG to see the re-parsed document):
https://preview.gravity-ui.com/md-editor/?path=%2Fstory%2Fplayground--story&markup=lz%3AAYgECMEMGcAsCgB2AHAtqAlqAHvEx54BiURAewBdRYBTSAEw0QHNQAdIwgH1ElB-D94XAFwBaCaIliuw0MGzseAT2D9QAL35A

Reproduction (repro.js):

const {ExtensionsManager} = require('@gravity-ui/markdown-editor/core');
const {
  BaseSchemaSpecs,
  CodeSpecs,
  CodeBlockSpecs,
  HeadingSpecs,
  TableSpecs,
} = require('@gravity-ui/markdown-editor/specs');

const {markupParser, serializer} = new ExtensionsManager({
  extensions: (b) =>
    b
      .use(BaseSchemaSpecs, {})
      .use(CodeSpecs)
      .use(CodeBlockSpecs, {})
      .use(HeadingSpecs, {})
      .use(TableSpecs),
}).buildDeps();

for (const md of [
  '``` bash\nnpm i x\n```',
  '# not heading \\#',
  '| a | b |\n|:---|:---|\n| `x \\| y` | z |',
]) {
  const doc1 = markupParser.parse(md);
  const saved = serializer.serialize(doc1);
  const doc2 = markupParser.parse(saved);
  console.log('saved:', JSON.stringify(saved));
  console.log('doc1 :', doc1.toString(), JSON.stringify(doc1.firstChild.attrs));
  console.log('doc2 :', doc2.toString());
  console.log('---');
}

Run:

npm init -y && npm i @gravity-ui/[email protected] && node repro.js

Actual output:

saved: "```\nnpm i x\n```"
doc1 : doc(code_block("npm i x")) {"data-language":"","data-markup":"```","data-line":null}
doc2 : doc(code_block("npm i x"))
---
saved: "# not heading #"
doc1 : doc(heading("not heading #")) {"level":1,"data-line":null}
doc2 : doc(heading("not heading"))
---
saved: "\n|a|b|\n|:---|:---|\n|`x | y`|z|\n"
doc1 : doc(table(thead(tr(th("a"), th("b"))), tbody(tr(td(code("x | y")), td("z"))))) {}
doc2 : doc(table(thead(tr(th("a"), th("b"))), tbody(tr(td("`x"), td("y`")))))
---

Expected output:

  • Case 1 keeps "data-language": "bash" on doc1 and serializes as "```bash\nnpm i x\n```".
  • Case 2 escapes the trailing # so doc2 stays doc(heading("not heading #")).
  • Case 3 keeps | escaped as \| inside inline code in table cells (per https://github.github.com/gfm/#tables-extension-) so doc2 stays doc(table(thead(tr(th("a"), th("b"))), tbody(tr(td(code("x | y")), td("z"))))).

In CodeBlockSpecs/index.ts, tok.info.split(/\s+/) runs without .trim() first (unlike markdown-it's fence renderer); in HeadingSpecs/utils.ts, headingToMarkdown only escapes hash-only headings (/^#+[ \t\r\n]*$/); and CodeSpecs sets escape: false without escaping | when inside a table cell.

Versions tested: @gravity-ui/[email protected] (npm latest) and main (b633420) on Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
TypeScript
Star
428
Fork
48
Merge trung bình
5 ngày 15 giờ
Pull request đã merge (30 ngày)
47

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của gravity-ui/markdown-editor

Tất cả issue của gravity-ui/markdown-editor

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.