Skip to content

fix: set language on CodeNode for markdown fenced code blocks - #1302

Merged
jaapio merged 1 commit into
phpDocumentor:mainfrom
jonathanhefner:fix-markdown-code-block-syntax-highlighting
Feb 27, 2026
Merged

jaapio merged 1 commit into
phpDocumentor:mainfrom
jonathanhefner:fix-markdown-code-block-syntax-highlighting

Conversation

@jonathanhefner

Copy link
Copy Markdown
Contributor

⚠️ Disclaimer: This was vibe-coded. I am not a PHP developer, but I am helping https://github.com/modelcontextprotocol/php-sdk set up documentation using phpDocumentor, and I encountered this issue. I guided Claude through the investigation and self-review, but I am unable to personally judge the fix.


CodeBlockParser::parse() stored the fenced code info string in options['caption'] via withOptions(), but nothing ever read that value — CodeNode::getCaption() returns the typed $caption property, not getOption('caption'). As a result, CodeNode::getLanguage() always returned null for markdown code blocks, producing class="language-" in the rendered HTML and preventing syntax highlighting.

Replace the dead withOptions(['caption' => ...]) call with setLanguage() using getInfoWords()[0] from the CommonMark FencedCode node, matching both the RST parser's behavior (CodeBlockDirective) and league/commonmark's own FencedCodeRenderer.

`CodeBlockParser::parse()` stored the fenced code info string in
`options['caption']` via `withOptions()`, but nothing ever read that
value — `CodeNode::getCaption()` returns the typed `$caption` property,
not `getOption('caption')`. As a result, `CodeNode::getLanguage()`
always returned `null` for markdown code blocks, producing
`class="language-"` in the rendered HTML and preventing syntax
highlighting.

Replace the dead `withOptions(['caption' => ...])` call with
`setLanguage()` using `getInfoWords()[0]` from the CommonMark
`FencedCode` node, matching both the RST parser's behavior
(`CodeBlockDirective`) and league/commonmark's own `FencedCodeRenderer`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jaapio
jaapio force-pushed the fix-markdown-code-block-syntax-highlighting branch from df01e74 to 96075a2 Compare February 27, 2026 16:08
@jaapio

jaapio commented Feb 27, 2026

Copy link
Copy Markdown
Member

This is not bad at all, thanks for helping out. This is good to merge.

@jaapio
jaapio enabled auto-merge February 27, 2026 16:13
@jaapio
jaapio merged commit 81ec11f into phpDocumentor:main Feb 27, 2026
58 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