doc/development/fe_guide/blob_syntax_highlighting.md
This guide outlines best practices and implementation details for syntax highlighting in the repository source code viewer. GitLab uses two syntax highlighting libraries:
The source code viewer uses this dual approach to ensure broad language support and optimal performance when viewing files in the repository.
The syntax highlighting implementation consists of several key components:
blob_content_viewer.vue: Main component for displaying file contentsource_viewer.vue: Handles the rendering of source codehighlight_mixin.js: Manages the highlighting process and WebWorker communicationhighlight_utils.js: Provides utilities for content chunking and processingWe optimize the display of content through a staged rendering approach:
To maintain optimal browser performance:
You can add syntax highlighting support for new languages by:
The method you choose depends on whether the language already has a Highlight.js compatible definition available.
We can add third-party dependencies to our package.json and import the dependency in highlight_js_language_loader.
Example:
package.json:// package.json
//...
"dependencies": {
"@gleam-lang/highlight.js-gleam": "^1.5.0",
//...
highlight_js_language_loader.js:// highlight_js_language_loader.js
//...
gleam: () => import(/* webpackChunkName: 'hl-gleam' */ '@gleam-lang/highlight.js-gleam'),
//...
If the language is still displayed as plaintext, you might need to add language detection based on the file extension in highlight_mixin.js:
if (name.endsWith('.gleam')) {
language = 'gleam';
}
New language definitions can be added to our codebase under ~/vue_shared/components/source_viewer/languages/.
To add support for a new language:
highlight_js_language_loader.js.highlight_mixin.js if needed.Here are two examples of custom language implementations: