Skip to content

fix: coalesce duplicate class attributes in CodeBlock and Heading#356

Merged
kjk merged 2 commits intogomarkdown:masterfrom
lawrence3699:fix/coalesce-class-attrs
Apr 12, 2026
Merged

fix: coalesce duplicate class attributes in CodeBlock and Heading#356
kjk merged 2 commits intogomarkdown:masterfrom
lawrence3699:fix/coalesce-class-attrs

Conversation

@lawrence3699
Copy link
Copy Markdown
Contributor

Fixes #209.

Problem: When a CodeBlock or Heading node has both a language/special class and a custom class attribute set via the AST, the renderer produced two separate class="..." attributes:

<code class="language-yml" class="my-class">

Browsers only use the first class attribute, so the custom class was silently dropped.

Fix: Add coalesceClassAttrs helper that merges multiple class attributes into one, applied in both CodeBlock and HeadingEnter. This resolves the TODO comments at both call sites.

After:

<code class="language-yml my-class">

Validation:

  • go test ./... passes (excluding pre-existing failures in md/TestRenderCodeBlock and examples/)
  • go vet ./html/ clean
  • New unit tests for the helper function (8 cases including edge cases)
  • Integration test reproducing the exact scenario from Code Block classes overwriting when language is annotated #209
  • Updated testdata/mmark.test expected output to reflect correct coalesced class

When a CodeBlock or Heading node has both a language/special class and
a custom class attribute set via the AST, the renderer produced two
separate class="..." attributes in the HTML output. Browsers only use
the first one, so the custom class was silently dropped.

Add coalesceClassAttrs helper that merges multiple class attributes
into a single one, and apply it in both CodeBlock and HeadingEnter.

Fixes gomarkdown#209.
Copilot AI review requested due to automatic review settings April 12, 2026 11:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes duplicate class="..." attributes emitted for CodeBlock and Heading nodes when both a renderer-generated class (e.g., language-* / special) and an AST-provided class are present, by coalescing them into a single class attribute (per #209).

Changes:

  • Add coalesceClassAttrs helper to merge multiple class="..." attributes into one.
  • Apply the helper in Renderer.HeadingEnter and Renderer.CodeBlock.
  • Add unit tests for the helper plus an integration test reproducing the reported issue; update expected mmark test output.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
html/renderer.go Coalesces duplicate class attributes for headings and code blocks via new helper.
html/coalesce_test.go Unit tests for coalesceClassAttrs across common and edge cases.
html_renderer_test.go Integration test ensuring language + custom class are merged into one attribute.
testdata/mmark.test Updates expected HTML output to reflect coalesced heading classes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +83 to +89
ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
if cb, ok := node.(*ast.CodeBlock); ok {
cb.Attribute = &ast.Attribute{
Classes: [][]byte{[]byte("my-class")},
}
}
return ast.GoToNext
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST walk in this test sets cb.Attribute on both the entering and exiting visits because it doesn't check the entering flag. Adding if !entering { return ast.GoToNext } (or equivalent) would avoid redundant mutation and make the intent clearer.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@kjk kjk merged commit 134a5b2 into gomarkdown:master Apr 12, 2026
1 check passed
@kjk
Copy link
Copy Markdown
Contributor

kjk commented Apr 12, 2026

Thanks!

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.

Code Block classes overwriting when language is annotated

3 participants