Skip to content

Add fenced?/fenced= for code block nodes#443

Merged
gjtorikian merged 5 commits intogjtorikian:mainfrom
fukayatsu:add-fenced-getter
Mar 14, 2026
Merged

Add fenced?/fenced= for code block nodes#443
gjtorikian merged 5 commits intogjtorikian:mainfrom
fukayatsu:add-fenced-getter

Conversation

@fukayatsu
Copy link
Contributor

Summary

  • Add fenced? getter and fenced= setter on code block nodes for the underlying comrak NodeCodeBlock.fenced boolean field
  • The fenced field is already accepted as a required parameter in Node.new(:code_block, fenced: ...), but there were no getter/setter methods

Motivation

When walking the AST, it is currently impossible to distinguish between fenced code blocks (using ``` or ~~~) and indented code blocks (4 spaces), since both can have an empty fence_info string.

The current workaround is comparing source_position line counts against string_content line counts, but this is fragile (e.g., end_column == 0 edge cases when content follows the code block).

With fenced?, the distinction becomes trivial:

doc.walk do |node|
  next unless node.type == :code_block

  if node.fenced?
    # fenced code block (``` or ~~~)
  else
    # indented code block (4 spaces)
  end
end

Test plan

  • Added tests for fenced? returning true on fenced code blocks
  • Added tests for fenced? returning false on indented code blocks
  • Added test for fenced= setter
  • Added tests for fenced? raising TypeError on non-code-block nodes
  • All 796 tests pass

The `NodeCodeBlock` struct in comrak has a `fenced` boolean field that
indicates whether a code block uses fence syntax (``` or ~~~) or
indentation (4 spaces). This field is already accepted as a required
parameter in `Node.new(:code_block, fenced: ...)`, but there were no
corresponding getter/setter methods to read or update it.

This makes it impossible to distinguish between fenced and indented
code blocks when walking the AST, since both can have an empty
`fence_info` string.

This commit adds:
- `fenced?` method to read the boolean value
- `fenced=` method to update the boolean value

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

code_block.fenced = true

assert_predicate(code_block, :fenced?)
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add one more line to the test here which shows that once a code block is marked as fenced, converting the AST into HTML generates it as fenced?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an HTML output assertion in 4bbbab5.

However, since the generated HTML for fenced and indented code blocks is identical (both render as <pre><code>...</code></pre>), there's no way to distinguish between the two in the HTML output. So the assertion only verifies that the code block is rendered correctly after setting fenced = true, rather than confirming it's specifically rendered as a fenced block.

Let me know if this is sufficient or if you had something else in mind!

Copy link
Owner

Choose a reason for hiding this comment

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

ugh, sorry, I was typing quickly in between tasks. I meant that I wanted to assert that backticks were appearing, and not spaces. I added this in cbd7b81.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, that makes sense. Thank you!

…ed=true

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@fukayatsu fukayatsu requested a review from gjtorikian March 12, 2026 00:22
@gjtorikian
Copy link
Owner

thanks!

@gjtorikian gjtorikian merged commit 886dfb7 into gjtorikian:main Mar 14, 2026
14 of 17 checks passed
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.

2 participants