Skip to content

Keep changed code blocks inside a fence - #63

Open
tanghong123 wants to merge 1 commit into
davidar:masterfrom
tanghong123:fix-code-block-fence
Open

Keep changed code blocks inside a fence#63
tanghong123 wants to merge 1 commit into
davidar:masterfrom
tanghong123:fix-code-block-fence

Conversation

@tanghong123

Copy link
Copy Markdown

Fixes #62.

Two small changes in src/index.ts.

1. postprocess() dropped the <code> child of <pre>.

Assigning the unified diff to pre.textContent replaces every child of the <pre>, including its <code>. Pandoc's HTML reader only treats <pre> as a code block when it wraps a <code>, so the block round-tripped back as a paragraph — fence lost, lines reflowed, and smart punctuation rewriting the contents, so --config rendered as –config and "world." as “world.”. That last part is what makes this more than cosmetic: a command copied out of a diff can be silently wrong.

$ printf '<pre class="diff">-a --config\n+b --config</pre>' | pandoc -f html -t markdown
-a --config +b --config

$ printf '<pre class="diff"><code>-a --config\n+b --config</code></pre>' | pandoc -f html -t markdown
``` diff
-a --config
+b --config

**2. The intermediate conversion ran with syntax highlighting on.**

Pandoc then emits one `<span>` per code line and keeps the newlines as text nodes between them, which `node-htmldiff` discards — so a changed block carrying a language tag collapsed onto a single line even with the `<code>` restored. Plain blocks were unaffected, which is why `test/old.md` (an indented block) only ever exposed the first bug.

`--no-highlight` applies to the intermediate conversion only; the final render is a separate Pandoc call and still highlights, so ``` diff output stays colourised. I used `--no-highlight` rather than `--syntax-highlighting=none` for compatibility with older Pandoc, but 3.10 deprecates it and warns — happy to switch, or select by version, if you'd prefer.

**Verification**

`pandiff test/old.md test/new.md` on unmodified `master` does not reproduce the committed `test/diff.md`:

print(“Hello”) -print(“world.”) +print(“world!”) print(“Lorem ipsum
dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt”)


With this patch it matches the golden file, apart from the image paths that the test harness supplies via its `extract-media` / `resource-path` options.

I could not use `npm test` as a gate: it reports 0 passing / 27 failing on unmodified `master` in my environment (ENOENT on `test/*.md`, timeouts), identically before and after this change, so those failures look environmental. Your CI may differ.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

When a code block differed between the two inputs, postprocess() assigned the
unified diff to pre.textContent, which replaces every child of the <pre> and so
drops its <code>. Pandoc's HTML reader only treats <pre> as a code block when it
wraps a <code>, so on the way back to Markdown the block was read as a paragraph:
the fence was lost, the lines were reflowed together, and smart punctuation
rewrote the contents, turning --config into an en dash and "world." into curly
quotes. Build the <code> child explicitly instead.

convert() also rendered the intermediate HTML with Pandoc's default syntax
highlighting on. Pandoc then emits one <span> per code line and keeps the
newlines as text nodes between them, which node-htmldiff discards, so a changed
block carrying a language tag collapsed onto a single line even with the <code>
restored. Pass --no-highlight for the intermediate conversion only; the final
render is a separate Pandoc call and still highlights, so ``` diff output stays
colourised. --no-highlight is deprecated in Pandoc 3.10 in favour of
--syntax-highlighting=none but is kept here for compatibility with older
releases; happy to switch if you would rather require a newer Pandoc.

With both changes, `pandiff test/old.md test/new.md` reproduces the committed
test/diff.md apart from the image paths that the test harness supplies through
its extract-media and resource-path options.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changed code blocks lose their fence and get mangled (0.8.0 fails its own test/diff.md)

1 participant