Highlights the code blocks of a post's content with Makeup, once, when the post is written.
A block is a <pre> holding one <code class="language-x"> whose code is still plain text, and
whose language names a lexer Makeup has registered — by name (elixir, iex, heex, eex,
erlang, erl, html, json, diff, sql, js, javascript, ts, typescript, css) or
by file extension (ex, exs, hrl, htm, patch). It is stored in the shape posts imported from the old site already hold:
<pre class="highlight"><code class="language-x"> with one <span class="…"> per token, the
classes the kit's blog-code-highlight stylesheet colours. Reading a post highlights nothing.
Left as written:
- a block whose code already holds markup — highlighted here before, or on the old site;
- a block with no
language-class, or one no lexer answers to (sh,bash,shell…); - a
<code>that is not the only thing in its<pre>, and inline<code>; - a block whose lexer raises or takes longer than five seconds. Each block is lexed in its own
task under
MishkaBlog.HighlightSupervisor, so that block stays plain and the post is still written.
After the sanitizer
content is sanitized by the guardedstruct change, which Ash runs after every change an action
lists. This one does its work in a before_action hook, which runs after all of them, so what it
reads is the sanitizer's HTML: attributes double-quoted, and text escaped as &, < and
> and nothing else. The blocks are found in that shape and the tokens are written in it, so
the code stays text — a <script> in a code block is <script> before and after — and
saving a highlighted post again leaves its content as it is.
Usage
update :update do
change MishkaBlog.Resources.Changes.HighlightCode
end
Summary
Functions
html with every plain code block of a known language highlighted, and nothing else changed.