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

Aperta
#1,416 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
html, rust

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Rust
Stelle
316
Fork
23
Merge medio
11h 49m
PR unite (30g)
245

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di quarto-dev/q2

Tutte le issue di quarto-dev/q2

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.