Highlight code

Highlight code in the browser with MicroLighter and add your own grammar, such as Razor, to any Bevel site.

On this page
Layouthtml
<script type="module">
  import { highlightAll } from "/vendor/microlighter/index.js";
  highlightAll({ selector: "pre code[class*=language-]" });
</script>

Bevel does not highlight code. You choose how. This site uses MicroLighter, which highlights in the browser with the CSS Custom Highlight API. It adds no <span> tags, so the exported HTML stays small and clean.

Where the browser does not support the API, the code stays plain text. It is still readable.

How this site does it

The build writes a fixed shape of HTML for every code block. A small script finds those blocks and highlights them.

html
<figure class="code-block" data-lang="csharp">
  <figcaption class="code-block__header">
    <span class="code-block__title">Program.cs</span>
    <span class="code-block__lang">csharp</span>
  </figcaption>
  <pre class="code-block__pre"><code class="language-csharp">…escaped code…</code></pre>
</figure>

The language-* class on <code> tells MicroLighter which grammar to use. The script that does this is code-blocks.js. It also adds line numbers, highlighted lines, diff markers and a copy button.

Use MicroLighter on your site

  1. Copy the files

    Copy MicroLighter into wwwroot/vendor/microlighter/. This site vendors version 2.2.0 from the npm package and lists every change it made in VENDORED.md. Keep the LICENSE file with the code.

  2. Add a theme

    Include a theme CSS file, or write your own rules for the highlight names. This site styles them in wwwroot/css/code-theme.css.

  3. Run it

    Load it as a module and call highlightAll after the page loads.

    wwwroot/js/code.jsjavascript
    import { highlightAll } from "../vendor/microlighter/index.js";
    
    highlightAll({
      selector: "pre code[class*=language-]",
      languageAliases: { cs: "csharp", cshtml: "razor", csproj: "xml", yml: "yaml", sh: "bash" },
    });

Add a Razor grammar

MicroLighter has grammars for many languages, but upstream ships none for Razor or XML. This site added both by dropping a file into grammars/.

A grammar is a JavaScript module that exports a TextMate-style description. Its shape is:

wwwroot/vendor/microlighter/grammars/razor.jsjavascript
export default {
  scopeName: "text.aspnetcorerazor",
  dependencies: ["csharp", "html"],
  patterns: [
    { include: "#transitions" },
    { include: "#directives" },
    { include: "#code-block" },
    { include: "text.html.basic" }
  ],
  repository: {
    directives: {
      begin: "(@)(page|inject|using|layout)\\b(?!\\s*\\()", end: "$",
      beginCaptures: { 1: { name: "keyword.control.cshtml.transition" },
                       2: { name: "keyword.control.razor.directive" } },
      patterns: [{ include: "source.cs" }]
    },
    // ...
  }
};
  1. Name the grammar file

    MicroLighter loads grammars/<name>.js on demand. Name the file after the language: razor.js.

  2. Declare what it embeds

    List other grammars in dependencies. Razor embeds C# and HTML, so it names csharp and html. Use { include: "source.cs" } where C# belongs.

  3. Write rules with JavaScript regular expressions

    Use begin and end for blocks, and match for single tokens. Each name becomes a highlight scope your theme can color. MicroLighter's tokenizer takes JavaScript regular expressions only. It has no injections and no patterns inside captures. For matching braces, use a rule that includes itself.

  4. Register the aliases

    Map the names people write to your grammar: razor and cshtml both point at razor. If a grammar loads another through a scope name like source.cs, MicroLighter looks for a module named cs, so also add cs: "csharp".

The full file is razor.js. A page of test cases lives in docs/Bevel.Docs/dev/grammar-tests.html, served at /dev/ under dotnet watch.

Choose a different highlighter

You can highlight at build time instead. Run your own Markdig renderer or a processor that emits highlighted HTML, and skip the script. The cost is bigger HTML. See Markdown extensions for where such a step would go.