Skip to content

Commit 4c64a6c

Browse files
Add support for custom versions of mermaid (GH-9)
2 parents c32cfe2 + 00963a4 commit 4c64a6c

File tree

8 files changed

+56
-17
lines changed

8 files changed

+56
-17
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ INSTALLED_APPS = [
2828

2929
Once you have installed the app, you can use the `mermaid` template tag in your templates.
3030

31-
```html
31+
```jinja2
3232
{% load mermaid %}
33-
3433
{% mermaid "graph LR; A-->B;" %}
3534
```
3635

36+
By default, Django Mermaid uses the **9.4.3** version of mermaid. However, if you want to use a specific version of
37+
mermaid, you can set the `MERMAID_VERSION` variable in your Django project's settings.py file.
38+
39+
```python
40+
MERMAID_VERSION = '10.0.3-alpha.1'
41+
```
42+
43+
Make sure the version you specify is available on the [mermaid CDN](https://cdnjs.com/libraries/mermaid), and has
44+
the `mermaid.min.js` file.
45+
3746
## Contribute
3847

3948
Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And

src/django_mermaid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.2"
1+
__version__ = "0.0.3"

src/django_mermaid/apps.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
from distutils import dir_util
2-
from os.path import dirname, join, exists
2+
from os import stat
3+
from os.path import dirname
4+
from os.path import exists
5+
from os.path import join
36
from urllib.request import urlretrieve
47

58
from django.apps import AppConfig
69

10+
from .templatetags import MERMAID_VERSION
11+
712

813
class MermaidConfig(AppConfig):
914
name = "django_mermaid"
1015

1116
def ready(self):
1217
"""Download mermaid.js from CDN if not already present"""
13-
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.4.3/mermaid.js"
14-
static = join(dirname(__file__), "static")
15-
if not exists(join(static, "mermaid.js")):
16-
dir_util.create_tree(static, ["mermaid.js"])
17-
urlretrieve(cdn, join(static, "mermaid.js"))
18+
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % MERMAID_VERSION
19+
static_dir = join(dirname(__file__), "static")
20+
mermaid_dir = join(static_dir, "mermaid", MERMAID_VERSION)
21+
if not exists(join(mermaid_dir, "mermaid.js")) or \
22+
stat(join(mermaid_dir, "mermaid.js")).st_size == 0:
23+
dir_util.create_tree(mermaid_dir, ["mermaid.js"])
24+
urlretrieve(cdn, join(mermaid_dir, "mermaid.js"))

src/django_mermaid/static/mermaid/9.4.3/mermaid.js

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.conf import settings
2+
3+
MERMAID_VERSION = getattr(
4+
settings,
5+
"MERMAID_VERSION",
6+
"9.4.3", # default to latest stable version
7+
)
8+
9+
__all__ = ["MERMAID_VERSION"]

src/django_mermaid/templatetags/mermaid.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from django.templatetags.static import static
33
from django.utils.safestring import mark_safe
44

5+
from . import MERMAID_VERSION
6+
57
register = template.Library()
68

79

@@ -14,5 +16,6 @@ def mermaid(diagram="", theme="default"):
1416
:param theme: The mermaid theme to use (default, forest, dark, neutral). See https://mermaid.js.org/config/theming.
1517
"""
1618

17-
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram, static("mermaid.js"))
19+
mermaid_uri = static("mermaid/%s/mermaid.js" % MERMAID_VERSION)
20+
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram, mermaid_uri)
1821
return html + "<script>mermaid.initialize({\"startOnLoad\": true, theme: \"%s\"});</script>" % theme

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ def pytest_configure():
1010
{
1111
"BACKEND": "django.template.backends.django.DjangoTemplates",
1212
},
13-
]
13+
],
14+
MERMAID_VERSION="8.6.3", # Use a specific version of mermaid
1415
)

tests/test_tag.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1+
from os.path import dirname
2+
from os.path import exists
3+
from os.path import join
4+
15
from django.template import Context
26
from django.template import Template
37

8+
from django_mermaid.templatetags import MERMAID_VERSION
49
from django_mermaid.templatetags.mermaid import mermaid
510

611

712
def test_tag_renders():
813
assert mermaid("graph LR; A-->B;") == (
9-
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>"""
10-
"""<script>mermaid.initialize({"startOnLoad": true, theme: "default"});</script>"""
14+
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
15+
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"default\"});</script>" % MERMAID_VERSION
1116
)
1217

1318

1419
def test_tag_use_in_template():
1520
template = Template("{% load mermaid %}{% mermaid content %}")
1621
template = template.render(Context({"content": "graph LR; A-->B;"}))
1722
assert template == (
18-
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>"""
19-
"""<script>mermaid.initialize({"startOnLoad": true, theme: "default"});</script>"""
23+
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
24+
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"default\"});</script>" % MERMAID_VERSION
2025
)
2126

2227

2328
def test_tag_use_in_template_with_arguments():
2429
template = Template("{% load mermaid %}{% mermaid content \"forest\" %}")
2530
template = template.render(Context({"content": "graph LR; A-->B;"}))
2631
assert template == (
27-
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>"""
28-
"""<script>mermaid.initialize({"startOnLoad": true, theme: "forest"});</script>"""
32+
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
33+
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"forest\"});</script>" % MERMAID_VERSION
2934
)
35+
36+
37+
def test_tag_use_custom_version():
38+
static_dir = join(dirname(__file__), "..", "src", "django_mermaid", "static")
39+
assert exists(join(static_dir, "mermaid", MERMAID_VERSION, "mermaid.js"))

0 commit comments

Comments
 (0)