File tree 6 files changed +39
-4
lines changed
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.
76
76
{% startmermaid "dark" %}graph LR; A--B;{% endmermaid %}
77
77
```
78
78
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
+
79
88
### Mermaid theme variables
80
89
81
90
You 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 5
5
from django .conf import settings
6
6
7
7
from .templatetags import DEFAULT_VERSION
8
+ from .templatetags import MERMAID_CDN
8
9
9
10
10
11
class MermaidConfig (AppConfig ):
@@ -13,12 +14,11 @@ class MermaidConfig(AppConfig):
13
14
def ready (self ):
14
15
"""Download mermaid.js from CDN if not already present"""
15
16
version = getattr (settings , "MERMAID_VERSION" , DEFAULT_VERSION )
16
- cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
17
17
current_dir = pathlib .Path (__file__ ).parent
18
18
static_dir = current_dir / "static"
19
19
mermaid_dir = static_dir / "mermaid" / version
20
20
mermaid_js = mermaid_dir / "mermaid.js"
21
21
if not mermaid_js .exists () or \
22
22
mermaid_js .stat ().st_size == 0 :
23
23
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
+
1
3
DEFAULT_VERSION = "9.4.3" # default to latest stable version
2
4
DEFAULT_THEME = "default" # use the mermaid 'default' theme
5
+ DEFAULT_USE_CDN = False # use local mermaid.js by default
3
6
4
7
__all__ = [
8
+ "DEFAULT_USE_CDN" ,
5
9
"DEFAULT_VERSION" ,
6
10
"DEFAULT_THEME" ,
11
+ "MERMAID_CDN" ,
7
12
]
Original file line number Diff line number Diff line change 7
7
from django .utils .safestring import mark_safe
8
8
9
9
from . import DEFAULT_THEME
10
+ from . import DEFAULT_USE_CDN
10
11
from . import DEFAULT_VERSION
12
+ from . import MERMAID_CDN
11
13
12
14
register = template .Library ()
13
15
@@ -24,10 +26,11 @@ def mermaid(diagram=None, theme=None):
24
26
"""
25
27
26
28
version = getattr (settings , "MERMAID_VERSION" , DEFAULT_VERSION )
29
+ use_cdn = getattr (settings , "MERMAID_USE_CDN" , DEFAULT_USE_CDN )
27
30
theme = theme or getattr (settings , "MERMAID_THEME" , DEFAULT_THEME )
28
31
theme_variables = getattr (settings , "MERMAID_THEME_VARIABLES" , {}) if theme == "base" else {}
29
32
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 )
31
34
html = "<div class=\" mermaid\" >%s</div><script src=\" %s\" ></script>" % (diagram or "" , mermaid_uri )
32
35
init_properties = {"startOnLoad" : True , "theme" : theme , "themeVariables" : theme_variables }
33
36
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):
36
36
)
37
37
38
38
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
+
39
57
@override_settings (MERMAID_THEME = "forest" )
40
58
@pytest .mark .parametrize (
41
59
"template_code" ,
You can’t perform that action at this time.
0 commit comments