Skip to content

Commit

Permalink
only copy mermaid dist if needed in docs build
Browse files Browse the repository at this point in the history
also keep mermaid dist in its own static folder
  • Loading branch information
2bndy5 committed Apr 28, 2023
1 parent b8e6270 commit 1270b3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions sphinx_immaterial/mermaid_diagrams.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""A custom directive that allows using mermaid diagrams"""
from pathlib import Path
import shutil
from typing import List
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.docutils import SphinxDirective
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment

# name of a flag to track if the mermaid dist is needed in the docs build
_COPY_MERMAID_DIST_ENV_KEY = "sphinx_immaterial_copy_mermaid_dist"


class mermaid_node(nodes.General, nodes.Element):
Expand Down Expand Up @@ -34,6 +40,7 @@ def run(self) -> List[nodes.Node]:
self.add_name(diagram_div)
diagram_div += diagram
self.set_source_info(diagram_div)
setattr(self.env, _COPY_MERMAID_DIST_ENV_KEY, True)
return [diagram_div]


Expand All @@ -54,13 +61,31 @@ def depart_mermaid_node_latex(self, node: mermaid_node):
self.body.append("\n\\end{sphinxVerbatim}\n")


def on_builder_init(app: Sphinx):
"""Init the copy-mermaid-dist flag to False"""
setattr(app.env, _COPY_MERMAID_DIST_ENV_KEY, False)


def copy_mermaid_dist(app: Sphinx, env: BuildEnvironment):
if app.builder.name not in ("html", "dirhtml"):
return # mermaid src is only used in HTML output
if getattr(app.env, _COPY_MERMAID_DIST_ENV_KEY, False) is True:
# copy the mermaid dist file (if not already done)
dst = Path(app.outdir, "_static", "mermaid")
if dst.exists():
return
shutil.copytree(str(Path(__file__).parent / "static" / "mermaid"), dst)


def setup(app: Sphinx):
app.add_directive("md-mermaid", MermaidDirective)
app.add_node(
mermaid_node,
html=(visit_mermaid_node_html, depart_mermaid_node_html),
latex=(visit_mermaid_node_latex, depart_mermaid_node_latex),
)
app.connect("builder-inited", on_builder_init)
app.connect("env-check-consistency", copy_mermaid_dist)
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const config = configuration()
*/
function fetchScripts(): Observable<void> {
return typeof mermaid === "undefined" || mermaid instanceof Element
? watchScript(`${config.base}_static/mermaid.min.js`) // sphinx-immaterial uses cached dist
? watchScript(`${config.base}_static/mermaid/mermaid.min.js`) // sphinx-immaterial uses cached dist
: of(undefined)
}

Expand Down
2 changes: 1 addition & 1 deletion tools/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const assets$ = concat(
...["mermaid.min.js*"]
.map(pattern => copyAll(pattern, {
from: "node_modules/mermaid/dist",
to: `${base}/static`
to: `${base}/static/mermaid`
})),

/* Copy mathjax dist */
Expand Down

0 comments on commit 1270b3b

Please sign in to comment.