obsidian/components/syntax-highlighting-component
Contains class SyntaxHighlightingComponent that registers syntax highlighting languages.
Obsidian highlights code in two independent places, through two separate registries, and a plugin adding its own language usually has to touch both:
- Prism (
prism.languages[language]) highlights code in reading view and in every<pre><code>the library renders — including the CodeHighlighterComponent settings field. - The CodeMirror 5 mode registry (window.CodeMirror.modes[language]) highlights a fenced code block inside the editor.
The CodeMirror 5 half looks like legacy that should be migrated to CodeMirror 6. It is not — verified against the Obsidian 1.13.4 runtime:
- Obsidian’s own CodeMirror 6 editor language is a bridged CodeMirror 5 mode:
StreamLanguage.define(CodeMirror.getMode({}, { name: 'hypermd' })). - Obsidian registers thathypermdmode throughwindow.CodeMirror.defineModewithfencedCodeBlockHighlighting: true, and that option resolves each fence’s language through the CodeMirror 5 mode registry. - There is no public or internal CodeMirror 6 language-registration API for fences.
So window.CodeMirror.defineMode is the sanctioned extension point, not legacy debt. Do not reopen this as a migration.
Teardown REMOVES the mode rather than redefining it to null. Plugins predating this component redefined the mode to null on unload, because it was unclear whether CodeMirror’s getMode needs the entry to stay present. It does not — verified against a live Obsidian 1.13.4 by syntax-highlighting-component.obsidian.integration.test.ts: after the entry is deleted, the fence renders its text with no error and drops back to the exact token count it had before the language was ever registered.
Classes
Section titled “Classes”| Class | Description |
|---|---|
| SyntaxHighlightingComponent | A component that registers and unregisters syntax highlighting languages. |
Interfaces
Section titled “Interfaces”| Interface | Description |
|---|---|
| SyntaxHighlightingComponentGrammarFactoryParams | The loaded Prism module, handed to a grammar factory. |
| SyntaxHighlightingComponentRegisterCodeBlockLanguageParams | Parameters for SyntaxHighlightingComponent.registerCodeBlockLanguage. |
| SyntaxHighlightingComponentRegisterPrismLanguageParams | Parameters for SyntaxHighlightingComponent.registerPrismLanguage. |
| Type | Description |
|---|---|
| SyntaxHighlightingComponentGrammarSource | A grammar, the name of an already registered language to alias, or a factory that builds the grammar from the loaded Prism module. |