fix(docs): render Mermaid/KaTeX on swup navigation (HYP-10, #82)#83
Draft
markovejnovic wants to merge 2 commits into
Draft
fix(docs): render Mermaid/KaTeX on swup navigation (HYP-10, #82)#83markovejnovic wants to merge 2 commits into
markovejnovic wants to merge 2 commits into
Conversation
ExDoc uses swup for client-side page transitions, so DOMContentLoaded and the KaTeX auto-render onload fire only on the initial hard load. After any in-docs navigation (sidebar link, internal link, HexDocs search) both Mermaid diagrams and $$..$$ math were left as raw source. Drive all rendering from ExDoc's window `exdoc:loaded` event, which fires on the initial load and after every swup content swap. Also fix the Mermaid selector: ExDoc emits <pre><code class="mermaid">, not <pre class="mermaid">. Closes HYP-10 (#82).
Test Results 4 files ± 0 227 suites ±0 3m 21s ⏱️ - 1m 4s Results for commit 5567e24. ± Comparison against base commit f6f88dd. This pull request removes 2 and adds 8 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
Replace the hand-rolled DOM surgery with the snippet ExDoc's own README prescribes: two `exdoc:loaded` listeners, KaTeX via renderMathInElement, and Mermaid via mermaid.render() per <pre code.mermaid> block. Drops the unused \[ / \( delimiters (docs use only $ / $$), which removes the quad-backslash heredoc escaping, and loads mermaid with defer to match the reference. Same behavior, verified: KaTeX + Mermaid render on both hard load and swup nav.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
slop. need to review.
Problem
Mermaid diagrams and TeX math were broken on the published HexDocs site (e.g. https://hypervm.hexdocs.pm/architecture.html#storage) — reported in HYP-10 / #82.
Root cause (verified in a real headless browser)
ExDoc renders the docs as a swup single-page app: clicking a sidebar link, following an internal link, or landing via HexDocs search swaps page content client-side via
fetch, with no full reload. The oldbefore_closing_body_tag/1initialized Mermaid onDOMContentLoadedand KaTeX via a<script onload=...>attribute — both fire only on the initial hard load. After any in-docs navigation neither refires, so Mermaid fences and$$…$$math were left as raw source.Reproduced with Playwright/Chromium against the live site and a local build:
architecture.html: 33 KaTeX elements + 1 Mermaid SVG ✅ (why it looked intermittent)\begin{cases}and raw<code class="mermaid">visible ❌Two secondary bugs found along the way: the Mermaid selector queried
pre.mermaid, but ExDoc emits<pre><code class="mermaid">(it only worked by accident via baremermaid.run()); and the theme was read before ExDoc set the dark class.Fix
Drive all rendering from ExDoc's
windowexdoc:loadedevent, which fires on the initial load and after every swup content swap. Fix the Mermaid selector to liftpre > code.mermaidintodiv.mermaid, with an idempotent (:not([data-processed])) run so re-rendering across navigations is safe. Single-file change tomix.exs; KaTeX/Mermaid versions and the Mermaid SRI hash are unchanged.Verification
Playwright harness (serving the built
doc/over HTTP so swup'sfetchworks):{katex:0, mermaidSvg:0, leftoverRawMermaid:1}{katex:33, mermaidSvg:1, leftoverRawMermaid:0}mermaid.runis a safe no-op)Gate green:
mix format --check-formatted,compile --warnings-as-errors,credo --strict,test(438 passed),dialyzer(0 errors).Note: the change ships to readers on the next docs publish; nothing renders differently until HexDocs rebuilds from this
mix.exs.Closes #82.