Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ the `MERMAID_THEME` variable.
{% startmermaid "dark" %}graph LR; A--B;{% endmermaid %}
```

### Mermaid use CDN

By default, Django Mermaid uses the local copy of mermaid from staticfiles. However, if you want to use the CDN version
directly, you can set the `MERMAID_USE_CDN` variable in your Django project's **settings.py** file.

```python
MERMAID_USE_CDN = True
```

### Mermaid theme variables

You can define your custom theme by overriding the `MERMAID_THEME_VARIABLES` variable. You will need to use
Expand Down
2 changes: 1 addition & 1 deletion src/django_mermaid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.8"
__version__ = "0.0.9"
4 changes: 2 additions & 2 deletions src/django_mermaid/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings

from .templatetags import DEFAULT_VERSION
from .templatetags import MERMAID_CDN


class MermaidConfig(AppConfig):
Expand All @@ -13,12 +14,11 @@ class MermaidConfig(AppConfig):
def ready(self):
"""Download mermaid.js from CDN if not already present"""
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % version
current_dir = pathlib.Path(__file__).parent
static_dir = current_dir / "static"
mermaid_dir = static_dir / "mermaid" / version
mermaid_js = mermaid_dir / "mermaid.js"
if not mermaid_js.exists() or \
mermaid_js.stat().st_size == 0:
mermaid_dir.mkdir(parents=True, exist_ok=True)
urlretrieve(cdn, str(mermaid_js))
urlretrieve(MERMAID_CDN % version, str(mermaid_js))
5 changes: 5 additions & 0 deletions src/django_mermaid/templatetags/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
MERMAID_CDN = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js"

DEFAULT_VERSION = "9.4.3" # default to latest stable version
DEFAULT_THEME = "default" # use the mermaid 'default' theme
DEFAULT_USE_CDN = False # use local mermaid.js by default

__all__ = [
"DEFAULT_USE_CDN",
"DEFAULT_VERSION",
"DEFAULT_THEME",
"MERMAID_CDN",
]
5 changes: 4 additions & 1 deletion src/django_mermaid/templatetags/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django.utils.safestring import mark_safe

from . import DEFAULT_THEME
from . import DEFAULT_USE_CDN
from . import DEFAULT_VERSION
from . import MERMAID_CDN

register = template.Library()

Expand All @@ -24,10 +26,11 @@ def mermaid(diagram=None, theme=None):
"""

version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
use_cdn = getattr(settings, "MERMAID_USE_CDN", DEFAULT_USE_CDN)
theme = theme or getattr(settings, "MERMAID_THEME", DEFAULT_THEME)
theme_variables = getattr(settings, "MERMAID_THEME_VARIABLES", {}) if theme == "base" else {}

mermaid_uri = static("mermaid/%s/mermaid.js" % version)
mermaid_uri = MERMAID_CDN % version if use_cdn else static("mermaid/%s/mermaid.js" % version)
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram or "", mermaid_uri)
init_properties = {"startOnLoad": True, "theme": theme, "themeVariables": theme_variables}
return html + "<script>mermaid.initialize(%s);</script>" % json.dumps(init_properties)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ def test_tag_use_in_template(version, template_code):
)


@override_settings(MERMAID_USE_CDN=True)
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content %}",
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_mermaid_cdn(version, template_code):
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js\"></script>"
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"default\", \"themeVariables\""
": {}});</script>" % version
)


@override_settings(MERMAID_THEME="forest")
@pytest.mark.parametrize(
"template_code",
Expand Down