Skip to content

docs: activate mermaid dark theme for hexdocs - #35

Closed
clsource wants to merge 3 commits into
GenericJam:masterfrom
NinjasCL-labs:mermaid-dark
Closed

docs: activate mermaid dark theme for hexdocs#35
clsource wants to merge 3 commits into
GenericJam:masterfrom
NinjasCL-labs:mermaid-dark

Conversation

@clsource

Copy link
Copy Markdown
Contributor

Improved hexdocs rendering

@clsourceclsource changed the title Mermaid darkdocs: activate mermaid dark theme for hexdocsMay 28, 2026
@clsource

clsource commented May 29, 2026

Copy link
Copy Markdown
ContributorAuthor

This PR solves two problems in Hexdocs

  1. Does not render diagrams in architecture
imagen

This is due to the odd code used for triggering renders.
It was used a standard code for rendering all .mermaid code blocks.

  1. Added dark theme
imagen

The current white theme does not look good in hexdocs dark mode.
Maybe it can be improved to be the same as the hexdocs option.

imagen

@clsourceclsource closed this Jun 5, 2026
@clsource

Copy link
Copy Markdown
ContributorAuthor

maybe is better other approach

GenericJam added a commit that referenced this pull request Aug 30, 2026
Closes#42.
ex_doc marks dark mode by toggling `dark` on <body> (its stylesheet is written
against `body.dark`). Reading that class rather than prefers-color-scheme
covers all three of ex_doc's settings — light, dark and system — because
ex_doc re-toggles the class itself when the OS scheme changes under "system".
Three things the naive version gets wrong, all handled here:
- The old code replaced each <pre> with the rendered SVG, discarding the
diagram source. A theme switch then has nothing to re-render from, so the
source is captured up front.
- mermaid.render is async, so a fast light→dark→light toggle can land results
out of order and leave the diagram on the wrong theme. Each pass is stamped
and stale results are dropped.
- <body>'s class list also churns for search focus and sticky scroll, so a
bare MutationObserver would re-render diagrams on unrelated DOM activity.
Only an actual change in dark-ness triggers a re-render.
Supersedes the approach in #35 (closed by its author, "maybe is better other
approach"), which forced the dark theme unconditionally and so broke light
mode. That PR also reported diagrams not rendering at all; that half is
already fixed on master — the generated markup is <pre><code class="mermaid">
and the existing selector matches it.
Verified in a real browser against the built docs, not just by eye: light
renders (#333 text fill), toggling body.dark re-renders on pass 2 (#ccc),
toggling back re-renders on pass 3 (#333), and adding search-focused /
scroll-sticky does NOT re-render. No orphan mermaid error divs after three
passes, so no failed renders or leaks.
Note this only reaches hexdocs.pm via a published release — doc changes
without a version bump never ship.
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
GenericJam added a commit that referenced this pull request Sep 1, 2026
Closes#42.
ex_doc marks dark mode by toggling `dark` on <body> (its stylesheet is written
against `body.dark`). Reading that class rather than prefers-color-scheme
covers all three of ex_doc's settings — light, dark and system — because
ex_doc re-toggles the class itself when the OS scheme changes under "system".
Three things the naive version gets wrong, all handled here:
- The old code replaced each <pre> with the rendered SVG, discarding the
diagram source. A theme switch then has nothing to re-render from, so the
source is captured up front.
- mermaid.render is async, so a fast light→dark→light toggle can land results
out of order and leave the diagram on the wrong theme. Each pass is stamped
and stale results are dropped.
- <body>'s class list also churns for search focus and sticky scroll, so a
bare MutationObserver would re-render diagrams on unrelated DOM activity.
Only an actual change in dark-ness triggers a re-render.
Supersedes the approach in #35 (closed by its author, "maybe is better other
approach"), which forced the dark theme unconditionally and so broke light
mode. That PR also reported diagrams not rendering at all; that half is
already fixed on master — the generated markup is <pre><code class="mermaid">
and the existing selector matches it.
Verified in a real browser against the built docs, not just by eye: light
renders (#333 text fill), toggling body.dark re-renders on pass 2 (#ccc),
toggling back re-renders on pass 3 (#333), and adding search-focused /
scroll-sticky does NOT re-render. No orphan mermaid error divs after three
passes, so no failed renders or leaks.
Note this only reaches hexdocs.pm via a published release — doc changes
without a version bump never ship.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@clsource