Skip to content

Keep the code language when stripping CSS classes - #51

Merged
trsdn merged 1 commit into
test/23-converter-testsfrom
fix/34-code-block-language
Aug 28, 2026
Merged

trsdn merged 1 commit into
test/23-converter-testsfrom
fix/34-code-block-language

Conversation

@trsdn

@trsdn trsdn commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Fixes #34

Markdig records a fenced code block's language as class="language-xxx" on the <code> element. LoopHtmlConverter then stripped every class attribute with a blanket regex, which destroyed the only record of the language. A round-trip turned ```csharp back into a bare ```.

The HTML to Markdown direction already reads the language-* class correctly (that came in with the <pre> fix for #19), so the bug was entirely in the stripping step. The regex now skips class attributes whose value starts with language- and strips everything else, which is what Loop actually needs.

Test changes worth a look

Un-skipping the round-trip test made two existing tests fail, both because they asserted the old buggy behaviour:

  • Convert_StripsCssClasses asserted class= never appears in the output at all. Replaced with two narrower tests: one that the language class survives, one that a non-language class does not.
  • FencedCode_SurvivesARoundTrip_ExceptTheLanguage expected the language to be lost. Removed, since FencedCode_KeepsItsLanguage now covers the same input with the correct expectation.

For the "non-language classes are still stripped" case I used a custom container (:::warning), because I wanted a class that Markdig genuinely emits rather than a guess. I verified the actual renderer output first: :::warning produces <div class="warning"> before stripping and <div> after, so the assertion cannot pass vacuously.

Verification

Full suite: 85 passed, 0 failed, 2 skipped. The two remaining skips are the still-open bugs #32 and #33.

Base branch

This targets test/23-converter-tests (PR #41) because the converters live in md2loop.Core and the test suite only exists on that branch. It needs to merge after #41.

Markdig records a fenced code block's language as class="language-xxx" on the
<code> element. The cleanup regex stripped every class attribute, so the only
record of the language was destroyed and a round-trip turned ```csharp back
into a bare ```.

The HTML to Markdown direction already reads the language-* class correctly, so
the fix is limited to not throwing it away: the regex now skips class attributes
whose value starts with "language-" and continues to strip everything else,
which is what Loop needs.

Un-skips the round-trip test that documented the bug. Two tests that asserted
the old behaviour are updated: one claimed no class attribute survives at all,
and the other expected the language to be lost. Non-language stripping is now
covered by a custom container, which renders <div class="warning">.

Fixes #34

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b721ad5f-9add-4f55-b9c8-7e6d7b0fb3e7
@trsdn
trsdn changed the base branch from master to test/23-converter-tests August 28, 2026 20:20
@trsdn
trsdn merged commit f8af640 into test/23-converter-tests Aug 28, 2026
@trsdn
trsdn deleted the fix/34-code-block-language branch August 28, 2026 21:14
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.

Class stripping removes language-* from code blocks, losing the code language

1 participant