External highlighters for code blocks

Submitting the following concurrently with a draft PR, https://github.com/getzola/zola/pull/883.


This is close enough to being a solution to #563 that I’d be fine with closing it if this were to be merged. I would also consider this to be a reasonable temporary solution for #878 (though I do think that in the long term, tree-sitter would be preferable to syntect). This provides GraphViz support easily (ref https://zola.discourse.group/t/269), and should provide PlantUML support too (though I haven’t used that tool, so I can’t say for certain).

I’d like to be able to get this working with LaTeX with all the proper fallbacks, so MathML falls back to MathJax falls back to a static image. Unfortunately, the static image bit proves to be the problem – see below / the commit message.


This feature is activated by specifying shell commands in config.toml.
For example, an external “highlighter” that renders GraphViz to SVG
could be defined as:

# ...
[external_highlighters]
dot = "echo -n '<p>'; dot -Tsvg; echo '</p>'"
# ...

A code block like:

```dot
digraph G {
A -> B;
B -> C;
A -> C;
}
```

then results in the HTML output by piping the contents of the code block
to the command specified as external_highlighters.dot.

The following environment variables are passed to external highlighter
commands:

  • ZOLA_BASE_URL: The base URL in the config
  • ZOLA_CURRENT_PAGE: The permalink URL to the current page
  • ZOLA_OUTPUT_DIR: The directory being output to

The last one would be more useful if the output directory weren’t
cleared before the content is translated from Markdown (ref
new user, max 2 links, see github). This would be particularly useful
for e.g. generating images from LaTeX, and replacing the code blocks
with <img> tags.

There are many highlighting improvements that I would like to add to Zola: line numbers and selection highlight are the main two. This external highlighter approach seems like it could be confusing once we add those.
What about having a shortcode to run a command based on some body rather than overloading the syntax highlighting?

Big advantage for me of overloading highlighting is that I get highlighting for the appropriate language in my editor.