Skip to content

Commit 98ed0ad

Browse files
timhoffmdrammock
andauthored
Fix display of mermaid diagrams in dark mode (#2250)
Closes: #2249 The handling is completely analoguous to the existing handling of graphviz [here](https://github.com/pydata/pydata-sphinx-theme/blob/main/src/pydata_sphinx_theme/assets/styles/extensions/_graphviz.scss). sphinxcontrib-mermaid generates the following HTML <img width="323" height="118" alt="475613612-a34f1441-ca54-4f31-a51c-668731175b41" src="https://github.com/user-attachments/assets/fa926e53-eb55-4bed-9500-301b73bb35f9" /> Therefore, the selector `pre.mermaid > svg` is suitable. --------- Co-authored-by: Daniel McCloy <dan@mccloy.info>
1 parent 4704de4 commit 98ed0ad

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"myst_parser",
4848
"ablog",
4949
"jupyter_sphinx",
50+
"sphinxcontrib.mermaid",
5051
"sphinxcontrib.youtube",
5152
"nbsphinx",
5253
"numpydoc",

docs/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ See the sections in the primary sidebar and below to explore.
1919
pydata
2020
execution
2121
graphviz
22+
mermaid
2223

2324

2425
.. Note: the caption below is intentionally long in order to test out what long captions look like.

docs/examples/mermaid.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
========
2+
Mermaid
3+
========
4+
5+
If you use `sphinxcontrib-mermaid <https://github.com/mgaitan/sphinxcontrib-mermaid>`_
6+
to generate flowcharts or diagrams, and you output the diagrams in "raw" format,
7+
they will automatically adapt to this theme's light or dark mode.
8+
9+
To use this feature, you'll need to install `sphinxcontrib-mermaid`_ and add it to your
10+
list of extensions in ``conf.py``:
11+
12+
.. code-block:: python
13+
14+
# conf.py
15+
extensions = [
16+
...,
17+
"sphinxcontrib.mermaid",
18+
]
19+
20+
This will enable the ``.. mermaid::`` directive. For example:
21+
22+
.. begin-example-mermaid
23+
.. mermaid::
24+
25+
gitGraph:
26+
commit
27+
branch newbranch
28+
checkout newbranch
29+
commit id:"1111"
30+
commit tag:"test"
31+
checkout main
32+
commit type: HIGHLIGHT
33+
commit
34+
merge newbranch
35+
commit
36+
branch b2
37+
commit
38+
.. end-example-mermaid
39+
40+
is generated by the following code:
41+
42+
.. include:: ./mermaid.rst
43+
:start-after: begin-example-mermaid
44+
:end-before: .. end-example-mermaid
45+
:code: rst
46+
:class: highlight
47+
48+
See the `Mermaid documentation <https://mermaid.js.org/syntax/examples>`__ for additional examples.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ doc = [
7373
"sphinx-design",
7474
"sphinx-togglebutton",
7575
"jupyterlite-sphinx",
76+
"sphinxcontrib-mermaid",
7677
"sphinxcontrib-youtube>=1.4.1",
7778
"sphinx-favicon>=1.0.1",
7879
"ipykernel",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* sphinxcontrib-mermaid
3+
* ref: https://github.com/mgaitan/sphinxcontrib-mermaid
4+
*/
5+
6+
/* Style the diagram such that it has a dark mode */
7+
html[data-theme="dark"] pre.mermaid > svg {
8+
filter: brightness(0.8) invert(0.82) contrast(1.2);
9+
}

src/pydata_sphinx_theme/assets/styles/pydata-sphinx-theme.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
@import "./extensions/execution";
7979
@import "./extensions/gallery_grid";
8080
@import "./extensions/graphviz";
81+
@import "./extensions/mermaid";
8182
@import "./extensions/pydata";
8283
@import "./extensions/sphinx_design";
8384
@import "./extensions/togglebutton";

0 commit comments

Comments
 (0)