Skip to content

Commit c2660e1

Browse files
authored
Merge pull request #1037 from sphinx-contrib/support-html-macros-for-mermaid
Allow users to feed mermaid diagrams into html-macros
2 parents 7edabcf + 9085046 commit c2660e1

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

doc/configuration.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,54 @@ Advanced processing configuration
21742174
- |confluence_disable_autogen_title|_
21752175
- |confluence_title_overrides|_
21762176

2177+
Third-party related options
2178+
---------------------------
2179+
2180+
.. note::
2181+
2182+
The configurations in this section are specific to supporting
2183+
:ref:`third-party extensions <extensions_third_party>`; results may
2184+
vary.
2185+
2186+
.. confval:: confluence_mermaid_html_macro
2187+
2188+
.. versionadded:: 2.7
2189+
2190+
.. warning::
2191+
2192+
This option relies on an HTML macro which is not available in a
2193+
default Confluence configuration. Using this option is only useful
2194+
for users that have instances where a system administrator has
2195+
enabled their use.
2196+
2197+
.. note::
2198+
2199+
This option will most likely require additional configuration to
2200+
function. Setting this option only produces HTML macros with Mermaid
2201+
content but does not automatically include the JavaScript required to
2202+
process this content.
2203+
2204+
There can be various ways an instance/page can be configured to
2205+
include Mermaid JS support. For example, adding the following content
2206+
on the page planning to render diagrams:
2207+
2208+
.. code-block:: rst
2209+
2210+
.. confluence_html::
2211+
2212+
<script type="module">
2213+
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
2214+
</script>
2215+
2216+
When using the `sphinxcontrib-mermaid`_ extension, this option can be
2217+
used pass raw Mermaid figures into an HTML macro.
2218+
2219+
.. code-block:: python
2220+
2221+
confluence_mermaid_html_macro = True
2222+
2223+
See also |confluence_html_macro|_.
2224+
21772225
Other options
21782226
-------------
21792227

@@ -2281,6 +2329,7 @@ Deprecated options
22812329
.. _root_doc: https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-root_doc
22822330
.. _sphinx-build: https://www.sphinx-doc.org/en/master/man/sphinx-build.html
22832331
.. _sphinx.ext.imgmath: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.imgmath
2332+
.. _sphinxcontrib-mermaid: https://pypi.org/project/sphinxcontrib-mermaid/
22842333
.. _suppress_warnings: https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-suppress_warnings
22852334
.. _toctree: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree
22862335
.. _write_doc: https://www.sphinx-doc.org/en/master/extdev/builderapi.html#sphinx.builders.Builder.write_doc

doc/features.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Type Notes
194194

195195
\newpage
196196

197+
.. _extensions_third_party:
198+
197199
Extensions (Third-party)
198200
------------------------
199201

@@ -277,7 +279,9 @@ Type Notes
277279
`sphinxcontrib-kroki`_ Supported
278280
`sphinxcontrib-mermaid`_ Limited support.
279281

280-
Requires a PNG/SVG configuration.
282+
Requires a PNG/SVG configuration. Raw/HTML
283+
renders can be used for environments with
284+
HTML macro support.
281285
`sphinxcontrib-nwdiag`_ Limited support.
282286

283287
PNGs only; cannot configure for SVG at this

sphinxcontrib/confluencebuilder/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ def setup(app):
244244
# Remove a detected title from generated documents.
245245
cm.add_conf_bool('confluence_remove_title', 'confluence')
246246

247+
# (configuration - third-party related)
248+
# Wrap Mermaid nodes into HTML macros.
249+
cm.add_conf_bool('confluence_mermaid_html_macro', 'confluence')
250+
247251
# (configuration - undocumented)
248252
# Enablement for bulk archiving of packages (for premium environments).
249253
cm.add_conf_bool('confluence_adv_bulk_archiving')

sphinxcontrib/confluencebuilder/transmute/ext_sphinxcontrib_mermaid.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from docutils import nodes
55
from sphinxcontrib.confluencebuilder.compat import docutils_findall as findall
66
from sphinxcontrib.confluencebuilder.logger import ConfluenceLogger
7+
from sphinxcontrib.confluencebuilder.nodes import confluence_html
78

89
# ##############################################################################
910
# disable import/except warnings for third-party modules
@@ -50,12 +51,20 @@ def __init__(self, builder):
5051
self.builder = builder
5152
mock_translator = MockTranslator(builder)
5253

54+
# if a user configures to use a mermaid/html-macro hint, we will
55+
# instead leave the raw content as is and wrap things in an HTML macro
56+
format_ = builder.config.mermaid_output_format
57+
if not builder.config.confluence_mermaid_html_macro and format_ == 'raw':
58+
format_ = 'png'
59+
5360
for node in findall(doctree, mermaid):
54-
try:
55-
format_ = builder.config.mermaid_output_format
56-
if format_ == 'raw':
57-
format_ = 'png'
61+
if format_ == 'raw':
62+
raw_html = f'<div class="mermaid">{node["code"]}</div>'
63+
new_node = confluence_html(rawsource=raw_html)
64+
node.replace_self(new_node)
65+
continue
5866

67+
try:
5968
fname, _ = mermaid_render(mock_translator,
6069
node['code'], node['options'], format_, 'mermaid')
6170
if not fname:

0 commit comments

Comments
 (0)