Configuring code highlighting | laravel-markdown-go | Spatie       

 SPATIE  

laravel-markdown-go
===================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-markdown-go](https://spatie.be/docs/laravel-markdown-go/v1)  Rendering-markdown  Configuring code highlighting

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-markdown-go/v1) 

  Configuring code highlighting    
- [ Introduction ](https://spatie.be/docs/laravel-markdown-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-markdown-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-markdown-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-markdown-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-markdown-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-markdown-go/v1/changelog)
- [ Resources and alternatives ](https://spatie.be/docs/laravel-markdown-go/v1/resources-and-alternatives)
- [ About us ](https://spatie.be/docs/laravel-markdown-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-markdown-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-markdown-go/v1/releasing)

Usage in templates
------------------

- [ General usage ](https://spatie.be/docs/laravel-markdown-go/v1/usage-in-blade/general-usage)
- [ Configuring code highlighting ](https://spatie.be/docs/laravel-markdown-go/v1/usage-in-blade/configuring-code-highlighting)
- [ Rendering anchors ](https://spatie.be/docs/laravel-markdown-go/v1/usage-in-blade/rendering-anchors)
- [ Adding custom attributes ](https://spatie.be/docs/laravel-markdown-go/v1/usage-in-blade/adding-custom-attributes)
- [ Passing options to Commonmark ](https://spatie.be/docs/laravel-markdown-go/v1/usage-in-blade/passing-options-to-commonmark)

Rendering markdown
------------------

- [ General usage ](https://spatie.be/docs/laravel-markdown-go/v1/rendering-markdown/general-usage)
- [ Configuring code highlighting ](https://spatie.be/docs/laravel-markdown-go/v1/rendering-markdown/configuring-code-highlighting)
- [ Rendering anchors ](https://spatie.be/docs/laravel-markdown-go/v1/rendering-markdown/rendering-anchors)
- [ Passing options to Commonmark ](https://spatie.be/docs/laravel-markdown-go/v1/rendering-markdown/passing-options-to-commonmark)

Advanced usage
--------------

- [ Highlighting lines ](https://spatie.be/docs/laravel-markdown-go/v1/advanced-usage/highlighting-lines)
- [ Caching results ](https://spatie.be/docs/laravel-markdown-go/v1/advanced-usage/caching-results)
- [ Customizing the rendering process ](https://spatie.be/docs/laravel-markdown-go/v1/advanced-usage/customizing-the-rendering-process)

 Configuring code highlighting
=============================

###  On this page 

1. [ Configuring a theme ](#content-configuring-a-theme)
2. [ Light and dark themes ](#content-light-and-dark-themes)
3. [ Disabling code highlighting ](#content-disabling-code-highlighting)

By default, [Chroma](https://github.com/alecthomas/chroma) highlights code blocks. Use a language name after the opening fence, such as `go`, `php`, `js` or `bash`. Unknown languages fall back to escaped plain text. Chroma's language coverage and token colors are not identical to Shiki's.

Configuring a theme
-----------------------------------------------------------------------------------------------------------------

Set `MARKDOWN_THEME=github-dark`, or assign a Chroma style in your configuration. The default style is `github`. See [Chroma's styles](https://github.com/alecthomas/chroma/tree/master/styles)for available names:

```
import "github.com/alecthomas/chroma/v2/styles"

renderer, err := markdown.NewRenderer(func(o *markdown.Options) {
    o.Theme = styles.Get("github-dark")
})
```

Chroma's `styles.Get` falls back for unknown names. Use `styles.Registry[name]`when you want to reject an unknown name, as the package does for `MARKDOWN_THEME`.

For a custom theme, load a Chroma XML style with `chroma.NewXMLStyle(reader)` or build a `*chroma.Style` with `chroma.NewStyle`. Pass the resulting style as `Theme`. This replaces PHP's path to a VSCode theme. Theme contents are included in cache identity, so replacing a theme does not reuse stale output. Do not mutate styles after constructing a renderer.

Light and dark themes
-----------------------------------------------------------------------------------------------------------------------

Set both palettes to emit CSS variables for each token:

```
renderer, err := markdown.NewRenderer(func(o *markdown.Options) {
    o.Theme = styles.Get("github")
    o.DarkTheme = styles.Get("github-dark")
})
```

The output uses the light palette until your site's CSS selects the dark values:

```
.dark .chroma, .dark .chroma span {
    color: var(--markdown-color-dark) !important;
}
.dark .chroma {
    background-color: var(--markdown-background-color-dark) !important;
}
```

The package does not install a theme switcher or global stylesheet.

Disabling code highlighting
-----------------------------------------------------------------------------------------------------------------------------------------

Code highlighting can be disabled globally by setting `MARKDOWN_HIGHLIGHT_CODE=false` or `cfg.Options.HighlightCode = false`. For an individual renderer:

```
renderer, err := markdown.NewRenderer(func(o *markdown.Options) {
    o.HighlightCode = false
})
```

Disabled highlighting preserves literal code, including `+ ` and `- ` markers, and still escapes HTML. It also disables line annotations.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
