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

未关闭
#1,416 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
html, rust

调研方向

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.

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

描述

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).

主要语言
Rust
星标
316
派生
23
平均合并
11 小时 49 分钟
30 天内合并 PR
245

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

quarto-dev/q2 的其他 Issue

查看 quarto-dev/q2 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

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