Erweiterung:CodeMirror
- Nicht zu verwechseln mit CodeEditor .
Freigabestatus: stabil |
|
|---|---|
| Einbindung | Benutzer-Schnittstelle |
| Beschreibung | Bereitstellt Syntax-Highlighting in Editoren |
| Autor(en) | |
| Betreuer | Community Tech |
| Letzte Version | 6.0.0 |
| Kompatibilitätspolitik | Snapshots werden zusammen mit MediaWiki veröffentlicht. Der Master ist nicht abwärtskompatibel. |
| PHP | 8.2+ |
|
|
| Licence | GNU General Public License 2.0 oder neuer |
| Herunterladen | README |
| Übersetze die CodeMirror-Erweiterung, wenn sie auf translatewiki.net verfügbar ist | |
| Probleme | Offene Aufgaben · Einen Fehler melden |
The CodeMirror extension provides a standalone editor with syntax highlighting for a number of programming and markup languages, including wikitext, using the CodeMirror library. It can be used with other editors such as WikiEditor , the Wikitext-Editor 2017 , Proofread Page , and more. For usage and a list of features, see Help:Extension:CodeMirror .
In 2024–2025, the extension was upgraded to the new major version, CodeMirror 6, and with it many new features were added.
- For information about the older version, see Extension:CodeMirror/5 .
- For help documentation, see Help:Extension:CodeMirror .
- For the JavaScript documentation, see wmdoc:CodeMirror.
Installation
- Die Erweiterung herunterladen und die Datei(en) in ein Verzeichnis namens
CodeMirrorim Ordnerextensions/ablegen.
Entwickler und Code-Beitragende sollten stattdessen die Erweiterung von Git installieren, mit:cdextensions/ gitclonehttps://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror - Folgenden Code am Ende deiner LocalSettings.php -Datei einfügen:
wfLoadExtension( 'CodeMirror' );
- Configure as required.
- Yes Erledigt – Navigiere zu
Special:Versionin deinem Wiki, um zu überprüfen, ob die Erweiterung erfolgreich installiert wurde.
Konfiguration
To enable CodeMirror for all users by default, add the following to your LocalSettings.php :
# Enables use of CodeMirror by default in wikitext, but still allow users to disable it $wgDefaultUserOptions[ 'usecodemirror' ] = true;
If CodeMirror is being used instead of CodeEditor for code editing, a second user option, usecodemirror-code has effect, which by default is set to true.
$wgCodeMirrorEnabledModes- Temporary feature flag to control conflicts with Erweiterung:CodeEditor . See the Using CodeMirror instead of CodeEditor section below for more information.
$wgCodeMirrorConflictingGadgets- An array of gadget names that, if enabled, will prevent CodeMirror from loading. Defaults to wikEd.
$wgCodeMirrorPrimaryPreferences- The preferences to advertise in the in-editor preferences panel. These should have a clear visual effect or be commonly used to warrant easier access to toggling the feature. Preferences not listed here will still be available in the full preferences dialog.
$wgCodeMirrorDefaultPreferences- Control which features are enabled by default for all users. See below for more information.
$wgCodeMirrorDefaultPreferencesCode- Same as $wgCodeMirrorDefaultPreferences but applies to "code"; That is, any CodeMirror mode other than "mediawiki" (wikitext).
Default preferences
The default preferences are set on either wikitext ($wgCodeMirrorDefaultPreferences) or all other "modes" such as JavaScript, CSS and JSON ($wgCodeMirrorDefaultPreferencesCode). For each feature, use true or false to enable or disable it entirely, or provide an array containing namespace IDs (integers) indicating where the feature should be enabled by default.
Note this configuration setting only applies to the default value. Users can override the defaults in their own CodeMirror preferences.
When changing a preference, it will apply only to either wikitext (the mediawiki mode) or non-wikitext (all other modes).
For example:
# Limit highlighting of whitespace to non-wikitext $wgCodeMirrorDefaultPreferences[ 'whitespace' ] = false; $wgCodeMirrorDefaultPreferencesCode[ 'whitespace' ] = true; # Limit autocompletion to templates and code $wgCodeMirrorDefaultPreferences[ 'autocomplete' ] = [ NS_TEMPLATE ]; $wgCodeMirrorDefaultPreferencesCode[ 'autocomplete' ] = true;
| Funktion | 2017 editor compatibility |
Standardwert | Default value (code)[1] |
|---|---|---|---|
activeLine |
N | |
|
autocomplete |
N | |
|
autofocus |
Yes[2] | |
|
bidiIsolation[3] |
N | |
|
bracketMatching |
Yes | |
|
closeBrackets |
N | |
|
closeTags[4] |
N | |
|
codeFolding |
N | |
|
foldAllRefs [5] |
N | |
Nicht verfügbar |
highlightRefs [6] |
Yes | |
Nicht verfügbar |
lineNumbering |
Yes | |
|
lineWrapping |
Yes[7] | |
|
lint |
N | |
|
openLinks |
N | |
|
specialChars |
N | |
|
trailingWhitespace |
N | |
|
whitespace |
N | |
|
Unterschiede zu CodeMirror 5
Neue Funktionen
- Erfordert nicht mehr die Verwendung von WikiEditor .
- Support for Scribunto, JavaScript, CSS, JSON, and Vue as an alternative to CodeEditor .
- Significant performance improvements.
- Right-to-left support (T170001) with bidi isolation. (T358804)
- Code folding . (T30684)
- Autocompletion . (T95100)
- Linting . (T381577)
- Quickly open links with modifier key + click . (T303392)
- Improved search panel. (T371436)
- CodeMirror preferences . (T359498)
- Syntax highlighting in DiscussionTools . (T407918)
- Syntax highlighting on read-only pages. (T301615)
- Syntax highlighting at Special:Upload. (T170246)
- Syntax highlighting at Special:ExpandTemplates. (T384148)
- Syntax highlighting at Special:AbuseFilter. (T399673)
- Syntax highlighting at
Special:SecurePoll/translate(T397871) - Robust JavaScript API for integration with extensions, gadgets, and user scripts.
Deprecations and other changes
- The ResourceLoader modules have changed. See the migration guide.
- The
ext.CodeMirror.switchhook has been deprecated. Useext.CodeMirror.toggleinstead. - The
.cm-mw-mnemonicCSS class has been renamed to.cm-mw-html-entity. - The
.cm-mw-doubleUnderscoreCSS class has been renamed to.cm-mw-double-underscore. - The
.cm-mw-template-name-mnemonicclass has been removed. Use.cm-mw-template-ground.cm-html-entityinstead. - The
.cm-mw-apostrophes-boldand.cm-mw-apostrophes-italicCSS classes have been removed. Use.cm-mw-apostrophesinstead. - Line-level styling for
<nowiki>,<pre>, or any tag without an associated TagMode has been removed (T351686). - Mixed languages within wikitext are not yet supported (T357480).
- The browser's native search functionality (using Ctrl+F) has been replaced with search functionality built into CodeMirror. This is necessary to maintain performance (T303664).
Wechselleitfaden
This guide applies to MediaWiki 1.45 and later. As of MediaWiki 1.46, CodeMirror 5 is no longer supported.
MediaWiki-Konfiguration
$wgCodeMirrorLineNumberingNamespacesis deprecated.
Configure $wgCodeMirrorDefaultPreferences instead.
ResourceLoader-Module
Ensure you're using the correct ResourceLoader modules. Because CodeMirror 6 no longer relies on WikiEditor, there are some naming and behaviourial changes from the CodeMirror 5 counterparts:
Some module names are shown without the ext.CodeMirror prefix for brevity; replace the '...' when using.
| Altes Modul | Neues Modul (MW 1.45) | Neues Modul (MW 1.46+) | Beschreibung |
|---|---|---|---|
ext.CodeMirror
|
....v6.init
|
....init
|
CodeMirror integration for WikiEditor on #wpTextbox1 (the normal editing textarea).
|
| N/A | ....v6.WikiEditor
|
....WikiEditor
|
Exports the CodeMirrorWikiEditor class |
| N/A | ....v6.init
|
....init
|
CodeMirror for #wpTextbox1 and other supported pages.
|
....lib
|
....v6.lib
|
....lib
|
Exports CodeMirror internals. |
....addons
|
N/A | N/A | This packaged the bracket matching feature in CodeMirror 5. Bracket matching is default behaviour in CodeMirror 6. |
....mode.mediawiki
|
....v6.mode.mediawiki
|
....mode.mediawiki
|
The MediaWiki language mode. |
| N/A | ext.CodeMirror.v6
|
ext.CodeMirror
|
Exports the CodeMirror class. |
....visualEditor
|
....visualEditor.init
|
....visualEditor.init
|
Integration with the Wikitext-Editor 2017 . |
....lib.mode.php
|
N/A | CodeMirror 6 will eventually provide some or all of these modes, but they will not be available as separate modules. | |
....lib.mode.clike
|
|||
....lib.mode.htmlmixed
|
|||
....lib.mode.xml
|
|||
....lib.mode.javascript
|
....v6.modes
|
....modes
|
These languages are supported in CodeMirror 6. |
....lib.mode.css
| |||
| N/A | |||
| N/A | |||
| N/A |
With the release of MediaWiki 1.46, the old modules will be replaced with the new ones, and the .v6 modules will be removed entirely.
Gadgets and user scripts
The CodeMirror global has been removed entirely.
For example, CodeMirror.fromTextArea( myTextarea ) will no longer work.
Instead, first load the desired ResourceLoader modules, instantiate a CodeMirror object, and call the initialize() method.
If your script relies on the ext.CodeMirror.switch hook to change the way it interacts with the editor, you'll need to use ext.CodeMirror.toggle instead, or alternatively listen to an event.
See the JavaScript integration section for more information.
CSS
The .CodeMirror element no longer exists.
Use .cm-editor instead for the entire CodeMirror DOM, or .cm-content for the inner content (doesn't include the search panel, for example).
See deprecations and other changes to other CSS classes.
Integration
MediaWiki-Erweiterungen
Using CodeMirror instead of CodeEditor
As of MediaWiki 1.44, CodeMirror supports highlighting of the Lua, JavaScript, CSS, JSON, and Vue languages. It can be used as an alternative to Erweiterung:CodeEditor .
Note however that not all extensions that integrate with CodeEditor have been updated to support CodeMirror. If you are using any of these extensions, you may want to keep CodeEditor installed on your wiki:
- Extension:VisualEditor – uses CodeEditor modules to power syntax highlighting for other extensions integrating with VisualEditor (T400014):
- Erweiterung:WikiLambda – the
ext.wikilambda.appmodule relies on CodeEditor (T400015).
As of MediaWiki 1.45+, for this to work, you'll need to adjust $wgCodeMirrorEnabledModes accordingly, along with the *UseCodeEditor and *UseCodeMirror for any applicable extensions you also have installed:
// Desired modes that should use CodeMirror (mediawiki, i.e. wikitext, is enabled by default) $wgCodeMirrorEnabledModes['javascript'] = true; $wgCodeMirrorEnabledModes['json'] = true; $wgCodeMirrorEnabledModes['css'] = true; $wgCodeMirrorEnabledModes['lua'] = true; $wgCodeMirrorEnabledModes['vue'] = true; // If you're also using CodeEditor, disable the same modes there: $wgCodeEditorEnabledModes['javascript'] = false; $wgCodeEditorEnabledModes['json'] = false; $wgCodeEditorEnabledModes['css'] = false; $wgCodeEditorEnabledModes['lua'] = false; $wgCodeEditorEnabledModes['vue'] = false; // AbuseFilter (note that there is no corresponding "mode" that needs to be set above) $wgAbuseFilterUseCodeEditor = false; $wgAbuseFilterUseCodeMirror = true; // Gadgets (for editing Gadget definition JSON pages) $wgGadgetsDefinitionsUseCodeEditor = false; $wgGadgetsDefinitionsUseCodeMirror = true; // JsonConfig $wgJsonConfigUseCodeEditor = false; $wgJsonConfigUseCodeMirror = true; // Scribunto (for editing Module pages that use the "lua" mode) $wgScribuntoUseCodeEditor = false; $wgScribuntoUseCodeMirror = true; // TemplateStyles $wgTemplateStylesUseCodeEditor = false; $wgTemplateStylesUseCodeMirror = true; // UploadWizard (for editing Campaign JSON pages) $wgUploadWizardUseCodeEditor = false; $wgUploadWizardUseCodeMirror = true;
Registering a new tag for MediaWiki
If you simply want CodeMirror to recognize a tag that is added by an extension, you can do so using the CodeMirrorTagModes extension attribute .
For example, to register the tag <foo> as something containing wikitext, you would add the following to extension.json:
{ "attributes":{ "CodeMirror":{ "TagModes":[ "foo":"mediawiki" ] } } }
CodeMirror will then highlight the content inside <foo>...</foo> as wikitext.
Registering a tag so that CodeMirror treats the contents as something other than wikitext is currently not supported (T357480).
If a tag is not registered, CodeMirror will highlight the contents as non-wikitext in the same way it highlights the contents of a <nowiki>...</nowiki> tag.
PHP hook
Since MediaWiki 1.44, the preferred means to integrate with CodeMirror in PHP is implementing the CodeMirrorGetModeHook interface:
class CodeMirrorHooks implements CodeMirrorGetModeHook { public function onCodeMirrorGetMode(Title $title, ?string &$mode, string $model ): bool { // Logic to determine if CodeMirror should be used if ( $title->hasContentModel( 'JsonLikeContentModel' ) ) { $mode = 'json'; return false; } return true; } }
Register the hook in extension.json:
"Hooks":{ "CodeMirrorGetMode":"codemirror" }, "HookHandlers":{ "codemirror":{ "class":"MediaWiki\\Extension\\MyExtension\\CodeMirrorHooks" } }
PluginModules
CodeMirrorPluginModules is an extension attribute that allows side-loading a module with CodeMirror.
This unconditionally loads the module whenever the ext.CodeMirror module is loaded.
extension.json:
{ "attributes":{ "CodeMirror":{ "PluginModules":[ "ext.MyExtension.CodeMirror" ] } } }
JavaScript
The CodeMirror editor is not an actual textarea, but a contenteditable. Usually developers need to ensure their code works with both given that CodeMirror can be toggled off.
For detecting changes to the document, using an event or a hook is likely the simplest. For reading and making changes, jQuery.textSelection may be convenient.
For more complex integrations or those who need better performance, you can add your own extension to a new or an existing CodeMirror instance.
Using jQuery.textSelection
If you simply want to fetch or make changes to the document text, jQuery.textSelection is the easiest and most reliable means to do so. Usage of jQuery.textSelection on the textarea is bubbled up to CodeMirror, so you don't need to have any knowledge of whether CodeMirror is enabled:
const$textarea=$('#wpTextbox1') constcontent=$textarea.textSelection('getContents'); // Append "Foobar" to the content. $textarea.textSelection('setContents',content+'\nFoobar');
jQuery's .val() on #wpTextbox1 can be used,[8] but this isn't recommended and may not work in all editors, such as the Wikitext-Editor 2017 .
Using ResourceLoader
The CodeMirror extension provides a number of ResourceLoader modules for use by user scripts, gadgets, and extensions.
To make use of CodeMirror, you'll need at minimum the ext.CodeMirror module, along with the desired "mode".
Modes can be unpacked from the ResourceLoader modules, using an exported method with the same name as the mode.
For MediaWiki wikitext, you'd use ext.CodeMirror.mode.mediawiki, and for JavaScript, JSON, CSS, Vue and Lua, you'd use ext.CodeMirror.modes:
constrequire=awaitmw.loader.using(['ext.CodeMirror','ext.CodeMirror.mode.mediawiki']); constCodeMirror=require('ext.CodeMirror'); const{mediawiki}=require('ext.CodeMirror.mode.mediawiki'); constcm=newCodeMirror(myTextarea,mediawiki()); cm.initialize();
If you also want WikiEditor:
constrequire=awaitmw.loader.using([ 'ext.wikiEditor', 'ext.CodeMirror.WikiEditor', 'ext.CodeMirror.mode.mediawiki' ]); consttextarea=document.getElementById('wpTextbox1'); mw.addWikiEditor($(textarea)); constCodeMirrorWikiEditor=require('ext.CodeMirror.WikiEditor'); const{mediawiki}=require('ext.CodeMirror.mode.mediawiki'); constcmWe=newCodeMirrorWikiEditor(textarea,mediawiki()); cmWe.mode='mediawiki'; cmWe.initialize();
Creating a new JavaScript instance:
constrequire=awaitmw.loader.using(['ext.CodeMirror','ext.CodeMirror.modes']); constCodeMirror=require('ext.CodeMirror'); const{javascript}=require('ext.CodeMirror.modes'); constcm=newCodeMirror(myTextarea,javascript()); cm.initialize();
| Modul | Beschreibung |
|---|---|
ext.CodeMirror.lib
|
The core CodeMirror library. You shouldn't need to require this directly unless you need access to the upstream CodeMirror API. |
ext.CodeMirror
|
The basic CodeMirror integration for MediaWiki editors. This module exports the CodeMirror class. |
ext.CodeMirror.WikiEditor
|
CodeMirror integration for WikiEditor. This module exports the CodeMirrorWikiEditor class. |
ext.CodeMirror.mode.mediawiki
|
Exports the mediawiki mode for highlighting MediaWiki wikitext.
|
ext.CodeMirror.modes
|
Includes the modes javascript, json, css, vue and lua.
|
ext.CodeMirror.init(intern) |
The main entrypoint for action=edit requests. Not intended for external use. |
ext.CodeMirror.visualEditor.init(intern) |
CodeMirror integration with the Wikitext-Editor 2017 , and only for wikitext. |
Using hooks
You can also integrate with CodeMirror by using frontend hooks. These allow you to run code just before or after CodeMirror has loaded, or react to changes to the document.
| Hook | Description |
|---|---|
ext.CodeMirror.initialize
|
Called just before CodeMirror is initialized. This can be used to manipulate the DOM to suit CodeMirror (i.e. if you manipulate WikiEditor's DOM, you may need this).
Parameter
|
ext.CodeMirror.ready
|
Called just after CodeMirror is initialized.
Parameter
|
ext.CodeMirror.toggle
|
Called when CodeMirror is toggled on or off.
Parameter
|
ext.CodeMirror.destroy
|
Called just after CodeMirror is destroyed and the original textarea is restored.
Parameter
|
ext.CodeMirror.input
|
Called when document changes are made in CodeMirror. Note that the textarea may not be updated yet.
Parameter
|
ext.CodeMirror.preferences.ready
|
Fired just before CodeMirrorPreferences has been instantiated.
Parameter
|
ext.CodeMirror.preferences.apply
|
Fired when a CodeMirror preference is enabled or initially applied in an editing session.
Parameter
|
ext.CodeMirror.preferences.display(intern) |
Fired when the preferences panel is constructed, just before it is displayed.
Parameter
|
ext.CodeMirror.gotoLine(internal) |
Fired when the go-to line panel is opened or closed. |
ext.CodeMirror.keymap(intern) |
Fired when the keyboard shortcut help dialog is opened. |
ext.CodeMirror.search(intern) |
Fired when the search panel is opened or closed. |
Using events
Using these events, you can integrate with CodeMirror using the same code as the original textarea:
myTextarea.addEventListener('keyup',(event)=>{ console.log(event.key); });
Extending CodeMirror
You can import the ext.CodeMirror.lib module to get access to the upstream CodeMirror API.
With this you can provide your own Extension when instantiating a CodeMirror or CodeMirrorWikiEditor object.
For example, to provide your own Extension that reacts to changes made in CodeMirror:
constrequire=awaitmw.loader.using(['ext.CodeMirror','ext.CodeMirror.mode.mediawiki']); constCodeMirror=require('ext.CodeMirror'); const{mediawiki}=require('ext.CodeMirror.mode.mediawiki'); // ext.CodeMirror.lib is a dependency of ext.CodeMirror, so it's already loaded at this point const{EditorView}=require('ext.CodeMirror.lib'); constmyExtension=EditorView.updateListener.of((/** @type {ViewUpdate} */update)=>{ if(update.docChanged){ // do something console.log(update.changes); } }); constcm=newCodeMirror(myTextarea,mediawiki()); cm.initialize([cm.defaultExtensions,myExtension]);
Or if you need to interact with an existing CodeMirror instance:
// Ensure CodeMirror is initialized first mw.hook('ext.CodeMirror.ready').add((cm)=>{ const{EditorView}=require('ext.CodeMirror.lib'); constmyExtension=EditorView.updateListener.of((/** @type {ViewUpdate} */update)=>{ if(update.docChanged){ // do something console.log(update.changes); } }); cm.applyExtension(myExtension); });
Another means of listening to changes is using the ext.CodeMirror.input hook:
mw.hook('ext.CodeMirror.input').add((update)=>{ // Print the ChangeSet to the console console.log(update.changes.toJSON()); });
Release history
- November 2024 – The CodeMirror 6 version of the extension was introduced as a beta feature.
- – After two years of development and testing, "Improved Syntax Highlighting" (CodeMirror 6) was officially promoted out of beta. This update brought advanced features like code folding, autocompletion, and linting to all users of the standard syntax highlighter, while significantly improving wikitext readability.[10]
Siehe auch
- User:Remember the dot/Syntax highlighter
- Extension:VisualEditor
- Erweiterung:WikiEditor
- Erweiterung:CodeEditor
- EditorAPIs: A library that presents a common interface for wikitext editing. Support for the legacy editor, VE, 2017 source editor and CodeMirror 5/6.
- c:Category:MediaWiki extension CodeMirror
Anmerkungen
- ↑
$wgCodeMirrorDefaultPreferencesCode - ↑ Focus is always placed on the editor in the 2017 editor.
- ↑ This feature is not yet stable.
- ↑ Only has an effect in wikitext.
- ↑ Only applicable to wikitext.
- ↑ Only applicable to wikitext.
- ↑ Line wrapping cannot be disabled in the 2017 editor.
- ↑ phab:T384556
- ↑ This event is not fired for wikitext (T401271)
- ↑ Tech News 2026 – Issue 17, Wikimedia Diff. Published April 20, 2026.
- Stable extensions/de
- User interface extensions/de
- EditPage::showEditForm:initial extensions/de
- EditPage::showReadOnlyForm:initial extensions/de
- GetPreferences extensions/de
- SpecialPageAfterExecute extensions/de
- UploadForm:initial extensions/de
- GPL licensed extensions/de
- Extensions in Wikimedia version control/de
- All extensions/de
- Extensions used on Wikimedia/de
- Extensions included in Canasta/de
- Extensions included in Fandom/de
- Extensions included in Miraheze/de
- Extensions included in MyWikis/de
- Extensions included in ProWiki/de
- Extensions included in Telepedia/de
- Extensions included in Weird Gloop/de
- Extensions included in wiki.gg/de
- Syntax highlighting extensions/de