Page MenuHomePhabricator

Interface for syntax highlighting in core
Open, Needs TriagePublicFeature

Description

Provide an interface for syntax highlighting in core, without an implementation. Implementation would be provided by SyntaxHighlight.

This would solve the following problems:

  • Core currently puts JS/CSS/Vue code within <pre> tags in the parser output. SyntaxHighlight then overwrites the whole parser output using ContentAlterParserOutput hook. This unclean design is confusing as any changes done to fillParserOutput() in core content handlers have no effect on any wiki that uses SyntaxHighlight.
  • In Scribunto, parser output includes highlighted code along with other things. This requires a tight coupling between Scribunto and SyntaxHighlight. Scribunto checks if SyntaxHighlight is installed and calls its methods directly.
    • If there were a core content model with similar requirements (eg. the experimental T407709: SVG content model), it won't be possible at all as core can't call an extension.
  • T146771: Transclusion of pages with CSS/JS/JSON/LUA content model transcludes the plain text without styling asks for transclusions of code pages to be styled. The proposed patch styles them using plain <pre> tags. There is no way for SyntaxHighlight to improve the styling, short of creating a new hook called from TextContent::getWikitextForTransclusion().

TL;DR: core providing such an interface helps reduce the use of hooks.

Event Timeline

LBLaiSiNanHai changed the subtype of this task from "Task" to "Feature Request".Dec 24 2025, 1:36 PM
LBLaiSiNanHai subscribed.

Agree, after Echo merged into core, I agree merge syntax highlight to core.

Agree, after Echo merged into core, I agree merge syntax highlight to core.

Please note that this task is not about actually doing syntax highlighting in core, only about providing an interface. Doing syntax highlighting in core doesn’t seem feasible/worth it, as the SyntaxHighlight extension has non-trivial dependencies (Pygments and Python).


How would the interface look like? In particular, how would one specify the language in which the code needs to be highlighted? Different syntax highlighters have different language identifiers, with large overlaps, but also differences. If it’s a generic interface in core, it shouldn’t be Pygments-specific. https://www.mediawiki.org/wiki/Extension:SyntaxHighlight#Lexers_previously_supported_by_GeSHi lists differences between two versions of the same extensions; the differences between the latest version of SyntaxHighlight (i.e. Pygments) and other extensions won’t be smaller.

@Tacsipacsi do you mean add a ISyntaxHighlightingProvider or other interface like it?

In particular, how would one specify the language in which the code needs to be highlighted? Different syntax highlighters have different language identifiers, with large overlaps, but also differences. If it’s a generic interface in core, it shouldn’t be Pygments-specific.

Core wouldn't use any language identifiers – it would just pass it on to the highlighting backend. New highlighting implementations would be expected to provide back-compatibility with language codes supported by older implementations (like Pygments did for GeSHi).

The problem with this is that, unlike the migration of SyntaxHighlight from GeSHi to Pygments, there is no “new” and “old” implementation: an interface allows multiple implementations to co-exist, so extension A should be “backward-compatible” (I quote this because it’s not really backward due to the lack of “new” and “old”) with extension B, but at the same time extension B should also be “backward-compatible” with extension A.

Change #1222739 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/core@master] Add interface for syntax highlighting

https://gerrit.wikimedia.org/r/1222739

Change #1222740 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/core@master] ApiFormatBase: use CodeHighlighter for syntax highlighting

https://gerrit.wikimedia.org/r/1222740

Change #1222741 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/SyntaxHighlight_GeSHi@master] Implement CodeHighlighterProvider for syntax highlighting

https://gerrit.wikimedia.org/r/1222741

Change #1222742 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/SyntaxHighlight_GeSHi@master] Don't use ApiFormatHighlight hook

https://gerrit.wikimedia.org/r/1222742

Change #1222743 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/TemplateStyles@master] Use CodeHighlighter interface for syntax highlighting

https://gerrit.wikimedia.org/r/1222743

Change #1222744 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/Scribunto@master] Use CodeHighlighter interface for syntax highlighting

https://gerrit.wikimedia.org/r/1222744