CRLF source puts raw carriage returns inside rendered code blocks (HTML and docx)

Đang mở
#1,416 1 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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
html, rust
Lĩnh vực
backend, documentation

Hướng nghiên cứu

Start with the linked code_fence_content.rs and fenced_code_block.rs locations to trace how fenced code preserves CRLF bytes, then follow the HTML writer and the JSON sent to Pandoc for docx. Compare CRLF and LF fixtures while keeping source offsets and qmd round-tripping intact. Done means rendered HTML and docx contain no stray carriage returns in code, with existing CRLF source behavior preserved.

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

Mô tả

bug readers-writers windows

A .qmd file with CRLF line endings, which is what a Windows checkout with core.autocrlf gives, renders its fenced code blocks with raw \r bytes inside the code. I believe this is because the reader keeps the CRLF of the code body as written in the AST, and the writers then copy that text through while they write every structural newline as LF. The HTML ends up with mixed line endings and a lone \r before </code>, and docx gets a \r at the end of each code line inside <w:t>. Pandoc on the same file produces no \r at all.

Reproduction

I can reproduce with the 0.48.0-nightly.20261128 nightly on Windows. The file is created with printf so the CRLF bytes don't depend on the checkout or the editor:

printf -- '---\r\ntitle: crlf\r\n---\r\n\r\n```\r\nx = 1\r\ny = 2\r\n```\r\n' > crlf.qmd

q2 render crlf.qmd
tr -cd '\r' < crlf.html | wc -c
#> 2

q2 render crlf.qmd --to docx
unzip -p crlf.docx word/document.xml | tr -cd '\r' | wc -c
#> 2

The code block in crlf.html, as bytes (od -c), is <pre><code>x = 1\r\ny = 2\r</code></pre>\n, while the rest of the page uses \n only. In word/document.xml the runs are <w:t xml:space="preserve">x = 1\r</w:t> and <w:t xml:space="preserve">y = 2\r</w:t>. With syntax highlighting (a python fence), the \r lands in its own run at the end of each line. The same file with LF endings gives 0 in both counts.

For comparison, pandoc 3.14 normalizes the line endings when it reads the file:

pandoc -f markdown -t native crlf.qmd
#> [ CodeBlock ( "" , [] , [] ) "x = 1\ny = 2" ]

pandoc -f markdown -t html --eol=lf crlf.qmd -o pandoc.html
tr -cd '\r' < pandoc.html | wc -c
#> 0

pandoc -f markdown -t docx crlf.qmd -o pandoc.docx
unzip -p pandoc.docx word/document.xml | tr -cd '\r' | wc -c
#> 0

Where the \r comes from

The \r is already in the AST: pampa -t native crlf.qmd prints CodeBlock ( "" , [] , [] ) "x = 1\ny = 2" with a raw \r byte after 1 and after 2 (the native writer doesn't escape it either). The body of a fenced block is built by copying the input byte range, and blank lines inside a list item are re-added "as written, \n or \r\n":

https://github.com/quarto-dev/q2/blob/b90599b3eb4ed5d179e9fc9bb4b0c09e74fa3fbc/crates/pampa/src/pandoc/treesitter_utils/code_fence_content.rs#L41-L56

Then the trailing newline strip pops only the final \n, which leaves the lone \r before </code>:

https://github.com/quarto-dev/q2/blob/b90599b3eb4ed5d179e9fc9bb4b0c09e74fa3fbc/crates/pampa/src/pandoc/treesitter_utils/fenced_code_block.rs#L68-L72

From there, the HTML writer escapes only & < > " ' in the code text, and docx goes through pandoc via JSON, which writes the \r into the run text. I think this is also what was behind the mermaid docx parity failure in quarto-dev/q2#1347, which quarto-dev/q2#1392 fixed by pinning the fixture to LF, so the product behaviour stayed as it was.

Other constructs I checked with the same CRLF-vs-LF comparison in HTML: raw blocks ({=html}) and display math ($$) keep the \r the same way, and so does a fence inside a list item. Paragraph soft breaks, multi-line inline code, blockquotes, a YAML block scalar (abstract: |) and inline math come out without \r. An executed {python} cell (Jupyter) also comes out clean.

What a fix needs to keep

Keeping the CRLF as written looks deliberate: source locations are byte offsets into the original file, and the qmd writer, including the incremental writer from quarto-dev/q2#966, needs to round-trip a CRLF document byte for byte. So normalizing the input before parsing is not an option. We could instead:

  • normalize \r\n to \n at the rendered-output boundaries only (the HTML writer's text output, and the JSON handed to pandoc for docx and the other pandoc-based formats), leaving the AST and the qmd writer as they are;
  • or have the writers emit the input's convention everywhere, so a CRLF source gives uniform CRLF HTML. This would not help docx, where a \r inside <w:t> is never right.

Either way the lone trailing \r from the newline strip should go. I don't know yet which of the two the team prefers for HTML, since pandoc's default --eol=native gives uniform CRLF HTML on Windows (2 \r for this file, one per output line).

Ngôn ngữ chính
Rust
Star
316
Fork
23
Merge trung bình
11 giờ 49 phút
Pull request đã merge (30 ngày)
245

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

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 quarto-dev/q2

Tất cả issue của quarto-dev/q2

Issue tương tự

Thêm issue về Rust

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.