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:
Each syntax highlighting color scheme in user preferences has a preview thumbnail in
app/assets/images/<scheme>-scheme-preview.png. A single HTML template renders every thumbnail with
GitLab Mono, so the thumbnails stay consistent across schemes and match each scheme's palette.
After you add a scheme or change a theme's colors, regenerate the thumbnails:
bundle exec rake gitlab:color_schemes:preview_images
The task tokenizes a sample snippet with the Rouge lexer. It colors each token with the value that
the token's class has in the scheme stylesheet
(app/assets/stylesheets/highlight/themes/<scheme>.scss), so a new theme gets a matching preview
from its color map. The task renders at 2x and downscales the result so the text edges stay smooth,
then runs pngquant to keep each thumbnail small.
The task needs a headless Chrome or Chromium binary. Set CHROME_BIN when the binary is not on
PATH. Install pngquant to optimize the output. The task skips optimization with a warning when
pngquant is missing.