File tree Expand file tree Collapse file tree 3 files changed +11
-3
lines changed
Expand file tree Collapse file tree 3 files changed +11
-3
lines changed 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 )
You can’t perform that action at this time.
0 commit comments