Highlight code
Highlight code in the browser with MicroLighter and add your own grammar, such as Razor, to any Bevel site.
On this page
<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.
<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
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 inVENDORED.md. Keep theLICENSEfile with the code.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.Run it
Load it as a module and call
highlightAllafter 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:
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" }]
},
// ...
}
};
Name the grammar file
MicroLighter loads
grammars/<name>.json demand. Name the file after the language:razor.js.Declare what it embeds
List other grammars in
dependencies. Razor embeds C# and HTML, so it namescsharpandhtml. Use{ include: "source.cs" }where C# belongs.Write rules with JavaScript regular expressions
Use
beginandendfor blocks, andmatchfor single tokens. Eachnamebecomes 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.Register the aliases
Map the names people write to your grammar:
razorandcshtmlboth point atrazor. If a grammar loads another through a scope name likesource.cs, MicroLighter looks for a module namedcs, so also addcs: "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.