File tree Expand file tree Collapse file tree 6 files changed +39
-4
lines changed Expand file tree Collapse file tree 6 files changed +39
-4
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,15 @@ the `MERMAID_THEME` variable.
7676{% startmermaid "dark" %}graph LR; A--B;{% endmermaid %}
7777```
7878
79+ ### Mermaid use CDN
80+
81+ By default, Django Mermaid uses the local copy of mermaid from staticfiles. However, if you want to use the CDN version
82+ directly, you can set the ` MERMAID_USE_CDN ` variable in your Django project's ** settings.py** file.
83+
84+ ``` python
85+ MERMAID_USE_CDN = True
86+ ```
87+
7988### Mermaid theme variables
8089
8190You can define your custom theme by overriding the ` MERMAID_THEME_VARIABLES ` variable. You will need to use
Original file line number Diff line number Diff line change 1- __version__ = "0.0.8 "
1+ __version__ = "0.0.9 "
Original file line number Diff line number Diff line change 55from django .conf import settings
66
77from .templatetags import DEFAULT_VERSION
8+ from .templatetags import MERMAID_CDN
89
910
1011class MermaidConfig (AppConfig ):
@@ -13,12 +14,11 @@ class MermaidConfig(AppConfig):
1314 def ready (self ):
1415 """Download mermaid.js from CDN if not already present"""
1516 version = getattr (settings , "MERMAID_VERSION" , DEFAULT_VERSION )
16- cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
1717 current_dir = pathlib .Path (__file__ ).parent
1818 static_dir = current_dir / "static"
1919 mermaid_dir = static_dir / "mermaid" / version
2020 mermaid_js = mermaid_dir / "mermaid.js"
2121 if not mermaid_js .exists () or \
2222 mermaid_js .stat ().st_size == 0 :
2323 mermaid_dir .mkdir (parents = True , exist_ok = True )
24- urlretrieve (cdn , str (mermaid_js ))
24+ urlretrieve (MERMAID_CDN % version , str (mermaid_js ))
Original file line number Diff line number Diff line change 1+ MERMAID_CDN = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js"
2+
13DEFAULT_VERSION = "9.4.3" # default to latest stable version
24DEFAULT_THEME = "default" # use the mermaid 'default' theme
5+ DEFAULT_USE_CDN = False # use local mermaid.js by default
36
47__all__ = [
8+ "DEFAULT_USE_CDN" ,
59 "DEFAULT_VERSION" ,
610 "DEFAULT_THEME" ,
11+ "MERMAID_CDN" ,
712]
Original file line number Diff line number Diff line change 77from django .utils .safestring import mark_safe
88
99from . import DEFAULT_THEME
10+ from . import DEFAULT_USE_CDN
1011from . import DEFAULT_VERSION
12+ from . import MERMAID_CDN
1113
1214register = template .Library ()
1315
@@ -24,10 +26,11 @@ def mermaid(diagram=None, theme=None):
2426 """
2527
2628 version = getattr (settings , "MERMAID_VERSION" , DEFAULT_VERSION )
29+ use_cdn = getattr (settings , "MERMAID_USE_CDN" , DEFAULT_USE_CDN )
2730 theme = theme or getattr (settings , "MERMAID_THEME" , DEFAULT_THEME )
2831 theme_variables = getattr (settings , "MERMAID_THEME_VARIABLES" , {}) if theme == "base" else {}
2932
30- mermaid_uri = static ("mermaid/%s/mermaid.js" % version )
33+ mermaid_uri = MERMAID_CDN % version if use_cdn else static ("mermaid/%s/mermaid.js" % version )
3134 html = "<div class=\" mermaid\" >%s</div><script src=\" %s\" ></script>" % (diagram or "" , mermaid_uri )
3235 init_properties = {"startOnLoad" : True , "theme" : theme , "themeVariables" : theme_variables }
3336 return html + "<script>mermaid.initialize(%s);</script>" % json .dumps (init_properties )
Original file line number Diff line number Diff line change @@ -36,6 +36,24 @@ def test_tag_use_in_template(version, template_code):
3636 )
3737
3838
39+ @override_settings (MERMAID_USE_CDN = True )
40+ @pytest .mark .parametrize (
41+ "template_code" ,
42+ [
43+ "{% load mermaid %}{% mermaid content %}" ,
44+ "{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
45+ ]
46+ )
47+ def test_tag_use_mermaid_cdn (version , template_code ):
48+ template = Template (template_code )
49+ template = template .render (Context ({"content" : "graph LR; A-->B;" }))
50+ assert template == (
51+ "<div class=\" mermaid\" >graph LR; A-->B;</div><script src=\" https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js\" ></script>"
52+ "<script>mermaid.initialize({\" startOnLoad\" : true, \" theme\" : \" default\" , \" themeVariables\" "
53+ ": {}});</script>" % version
54+ )
55+
56+
3957@override_settings (MERMAID_THEME = "forest" )
4058@pytest .mark .parametrize (
4159 "template_code" ,
You can’t perform that action at this time.
0 commit comments