Syntax-highlight fenced code blocks and fix their skin styling#41
Merged
Conversation
Fixes #37 A fenced code block whose info string names a language (```python) is now highlighted server-side by Extension:SyntaxHighlight (Pygments) when that extension is installed, matching how a wikitext <syntaxhighlight> block renders. Without it -- or for a block with no info string, an unknown language, an over-size block or a highlighter error -- the block keeps the default <pre><code class="language-..."> rendering, so this is a silent progressive enhancement with no hard dependency and no new configuration. Delegation goes through a pure CodeHighlighter port with a SyntaxHighlight adapter in Persistence and a no-op fallback, selected in the composition root by ExtensionRegistry. Highlight attempts are capped per render (mirroring the template-expansion cap) because each block can shell out to Pygments and there is no expensive-function budget in the ContentHandler context. The required ResourceLoader modules (ext.pygments, ext.pygments.view) are registered on the ParserOutput only when a block was actually highlighted. Possible follow-up: a tracking category for pages whose fenced languages could not be highlighted. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41 +/- ##
============================================
- Coverage 84.56% 83.25% -1.32%
- Complexity 379 401 +22
============================================
Files 37 40 +3
Lines 985 1039 +54
============================================
+ Hits 833 865 +32
- Misses 152 174 +22 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #36 Skins style bare `code` for inline use, giving it a background, a border and padding. Since `code` is an inline element, that background paints once per line box, so a code block showed a pill behind every one of its lines inside the box `pre` already draws. Wikitext never nests the two elements, so no skin resets this, while CommonMark renders every code block as `pre > code`. Ships the extension's first style module, loaded on every markdown page: it is tiny, it also covers indented code blocks, and it gives future markdown-specific styling a home. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The MediaWiki cache turned out to keep extensions/, despite the `!mediawiki/extensions/` exclusion, so once the cache was warm the restored tree already contained SyntaxHighlight and the clone step failed on a non-empty destination. It only passed the first time round because the cache was still cold. Remove the directory before cloning, so the step is correct with a cold or a warm cache without needing a cache key bump, and drop the comment claiming the cache excludes extensions/. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
JeroenDeDauw
marked this pull request as ready for review
July 16, 2026 16:53
Renames ext.nativeMarkdown.content to ext.nativeMarkdown.styles and moves its CSS from modules/ to resources/, matching both Karsten's independent fix in #39 and the house convention WikibaseFacetedSearch and Maps follow. Sets remoteExtPath to NativeMarkdown/resources so it agrees with localBasePath, which matters as soon as the CSS references a file. Drops the border-radius reset: it does nothing once the border is gone and the background is transparent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37
Fixes #36
A fenced code block whose info string names a language (a
pythonfence, say) is now highlighted server-side byExtension:SyntaxHighlight (Pygments) when that extension is installed, matching how a wikitext
<syntaxhighlight>block renders. Without it — or for a block with no info string, an unknown language, an over-size block or a
highlighter error — the block keeps the default
<pre><code class="language-…">rendering, so this is a silentprogressive enhancement with no hard dependency and no new configuration.
Delegation goes through a pure
CodeHighlighterport with a SyntaxHighlight adapter in Persistence and a no-opfallback, selected in the composition root by
ExtensionRegistry. Highlight attempts are capped per render(mirroring the template-expansion cap) because each block can shell out to Pygments and there is no
expensive-function budget in the ContentHandler context. The required ResourceLoader modules (
ext.pygments,ext.pygments.view) are registered on the ParserOutput only when a block was actually highlighted.Those plain-path blocks were also mis-styled, which the second commit fixes. Skins style bare
codefor inline use(background, border, padding), and since
codeis inline that background paints once per line box — so a blockrendered as CommonMark's
pre > codeshowed a pill behind every line, inside the boxprealready draws. Wikitextnever nests the two elements, so no skin resets it. This matters most on wikis without SyntaxHighlight
installed, where every fenced block takes the plain path. The fix is the extension's first style module, loaded on
every markdown page: it is tiny, it also covers indented code blocks, and it gives future markdown-specific styling
a home.
CI gains a step cloning SyntaxHighlight so the new integration tests run (they skip when it is absent), and
phpstan/psalm scan its source to resolve the adapter's class reference.
Possible follow-up: a tracking category for pages whose fenced languages could not be highlighted.
Production notes
Design and spec came from a
Fable 5 (max)-orchestrated session; implementation, tests and verification byOpus 4.8 (max).Two notes on the styling fix. The intended browser check was not possible (the Playwright profile was locked by another process), so it was verified instead by asserting the served CSS: the module's stylesheet link is in the page head,
load.phpreturns the reset rule, the rendered page's plain blocks really are.mw-parser-output pre > code, and the selector's specificity (0,1,2) beats the skin's barecoderules (0,0,1), which carry no!important— so the reset wins regardless of load order. A pixel-level confirmation is still worth a glance on review.A third commit fixes CI rather than the extension: the SyntaxHighlight clone added by the first commit passed only while the MediaWiki cache was cold. The cache keeps
extensions/despite its!mediawiki/extensions/exclusion, so once warm it restored SyntaxHighlight and the clone failed on a non-empty destination — this branch's second CI run went red for that reason alone. The clone now removes the directory first, which is correct with a cold or warm cache and needs no cache key bump.One spec deviation on the highlighting side: empty or whitespace-only code is guarded before calling SyntaxHighlight, since it would otherwise wrap empty input in a non-empty highlight div (isGood, non-empty value) rather than returning something the "value is empty" check would catch.