MishkaDocument.Resources.Changes.HighlightCode (Mishka Document v0.0.1)

Copy Markdown View Source

Highlights the code blocks of a page's content — a section's, a note's or a draft of either — with Makeup, once, when it 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 as <pre class="highlight"><code class="language-x"> with one <span class="…"> per token, the classes the kit's doc-code-highlight stylesheet colours. Reading a page highlights nothing.

Left as written:

  • a block whose code already holds markup — highlighted here before;
  • 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 MishkaDocument.HighlightSupervisor, so that block stays plain and the page 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 &amp;, &lt; and &gt; 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 &lt;script&gt; before and after — and saving a highlighted page again leaves its content as it is.

Usage

update :update do
  change MishkaDocument.Resources.Changes.HighlightCode
end

Summary

Functions

html with every plain code block of a known language highlighted, and nothing else changed.

Functions

highlight(html)

@spec highlight(String.t()) :: String.t()

html with every plain code block of a known language highlighted, and nothing else changed.