Skip to content

Commit

Permalink
Add support for custom versions of mermaid (GH-9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan authored Mar 13, 2023
2 parents c32cfe2 + 00963a4 commit 4c64a6c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 17 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ INSTALLED_APPS = [

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

```html
```jinja2
{% load mermaid %}

{% mermaid "graph LR; A-->B;" %}
```

By default, Django Mermaid uses the **9.4.3** version of mermaid. However, if you want to use a specific version of
mermaid, you can set the `MERMAID_VERSION` variable in your Django project's settings.py file.

```python
MERMAID_VERSION = '10.0.3-alpha.1'
```

Make sure the version you specify is available on the [mermaid CDN](https://cdnjs.com/libraries/mermaid), and has
the `mermaid.min.js` file.

## Contribute

Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And
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.2"
__version__ = "0.0.3"
19 changes: 13 additions & 6 deletions src/django_mermaid/apps.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from distutils import dir_util
from os.path import dirname, join, exists
from os import stat
from os.path import dirname
from os.path import exists
from os.path import join
from urllib.request import urlretrieve

from django.apps import AppConfig

from .templatetags import MERMAID_VERSION


class MermaidConfig(AppConfig):
name = "django_mermaid"

def ready(self):
"""Download mermaid.js from CDN if not already present"""
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.4.3/mermaid.js"
static = join(dirname(__file__), "static")
if not exists(join(static, "mermaid.js")):
dir_util.create_tree(static, ["mermaid.js"])
urlretrieve(cdn, join(static, "mermaid.js"))
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % MERMAID_VERSION
static_dir = join(dirname(__file__), "static")
mermaid_dir = join(static_dir, "mermaid", MERMAID_VERSION)
if not exists(join(mermaid_dir, "mermaid.js")) or \
stat(join(mermaid_dir, "mermaid.js")).st_size == 0:
dir_util.create_tree(mermaid_dir, ["mermaid.js"])
urlretrieve(cdn, join(mermaid_dir, "mermaid.js"))
Empty file.
9 changes: 9 additions & 0 deletions src/django_mermaid/templatetags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings

MERMAID_VERSION = getattr(
settings,
"MERMAID_VERSION",
"9.4.3", # default to latest stable version
)

__all__ = ["MERMAID_VERSION"]
5 changes: 4 additions & 1 deletion src/django_mermaid/templatetags/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.templatetags.static import static
from django.utils.safestring import mark_safe

from . import MERMAID_VERSION

register = template.Library()


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

html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram, static("mermaid.js"))
mermaid_uri = static("mermaid/%s/mermaid.js" % MERMAID_VERSION)
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram, mermaid_uri)
return html + "<script>mermaid.initialize({\"startOnLoad\": true, theme: \"%s\"});</script>" % theme
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ def pytest_configure():
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
},
]
],
MERMAID_VERSION="8.6.3", # Use a specific version of mermaid
)
22 changes: 16 additions & 6 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
from os.path import dirname
from os.path import exists
from os.path import join

from django.template import Context
from django.template import Template

from django_mermaid.templatetags import MERMAID_VERSION
from django_mermaid.templatetags.mermaid import mermaid


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


def test_tag_use_in_template():
template = Template("{% load mermaid %}{% mermaid content %}")
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>"""
"""<script>mermaid.initialize({"startOnLoad": true, theme: "default"});</script>"""
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"default\"});</script>" % MERMAID_VERSION
)


def test_tag_use_in_template_with_arguments():
template = Template("{% load mermaid %}{% mermaid content \"forest\" %}")
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>"""
"""<script>mermaid.initialize({"startOnLoad": true, theme: "forest"});</script>"""
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"forest\"});</script>" % MERMAID_VERSION
)


def test_tag_use_custom_version():
static_dir = join(dirname(__file__), "..", "src", "django_mermaid", "static")
assert exists(join(static_dir, "mermaid", MERMAID_VERSION, "mermaid.js"))

0 comments on commit 4c64a6c

Please sign in to comment.