Skip to content

Syntax highlighting Turtle, ShEx, SPARQL, etc in docs

Jose Emilio Labra Gayo edited this page Oct 18, 2024 · 1 revision

Syntax highlighting Turtle, ShEx, SPARQL, and other custom languages

After some search, we found a way to highlight the syntax of source code snippets in custom languages which are not currently supported by default in mdbook.

The trick is more or less explained in this issue although we followed a different approach.

We didn't have to use a custom highlight.js and remained with the one that comes by default.

We added our three custom syntax highlight functions which were borrowed from the following repos:

We adapted those files to register only the toplevel function and added our own version to the docs folder.

We created a single line file init_highlight_on_load.js which contains this line:

hljs.initHighlightingOnLoad();

And we added all those files in the book.toml section:

additional-js = [
    "turtle.js",
    "shexc.js",
    "sparql.js",
    "init_highlight_on_load.js", # This is required as the last one after the custom highlight files to restart highlight
    ...

And it seems to work.

Clone this wiki locally