Add fenced?/fenced= for code block nodes#443
Conversation
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?) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I see, that makes sense. Thank you!
…ed=true Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
98cab4c to
446a0d6
Compare
|
thanks! |
Summary
fenced?getter andfenced=setter on code block nodes for the underlying comrakNodeCodeBlock.fencedboolean fieldfencedfield is already accepted as a required parameter inNode.new(:code_block, fenced: ...), but there were no getter/setter methodsMotivation
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_infostring.The current workaround is comparing
source_positionline counts againststring_contentline counts, but this is fragile (e.g.,end_column == 0edge cases when content follows the code block).With
fenced?, the distinction becomes trivial:Test plan
fenced?returningtrueon fenced code blocksfenced?returningfalseon indented code blocksfenced=setterfenced?raisingTypeErroron non-code-block nodes