Skip to content

Commit 04837aa

Browse files
Add support for the base theme (GH-13)
2 parents f755fa7 + a5efd22 commit 04837aa

File tree

6 files changed

+92
-13
lines changed

6 files changed

+92
-13
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Django Mermaid
22

3-
Django template tag for showing mermaid diagrams.
3+
Django template tag for rendering mermaid diagrams.
44

55
[![PyPI](https://img.shields.io/pypi/v/django-mermaid.svg)](https://pypi.org/project/django-mermaid/)
66
[![License](https://img.shields.io/pypi/l/django-mermaid.svg)](https://github.com/ArtyomVancyan/django-mermaid/blob/master/LICENSE)
@@ -15,7 +15,7 @@ python -m pip install django-mermaid
1515

1616
## Configuration
1717

18-
Add the `django_mermaid.apps.MermaidConfig` to your `INSTALLED_APPS` setting:
18+
Add the `django_mermaid.apps.MermaidConfig` to your `INSTALLED_APPS` in your Django project's **settings.py** file.
1919

2020
```python
2121
INSTALLED_APPS = [
@@ -26,22 +26,58 @@ INSTALLED_APPS = [
2626

2727
## Usage
2828

29-
Once you have installed the app, you can use the `mermaid` template tag in your templates.
29+
After you configure the `INSTALLED_APPS`, you will be able to load the `mermaid` in your template and use the template
30+
tag for rendering mermaid diagrams.
3031

3132
```jinja2
3233
{% load mermaid %}
3334
{% mermaid "graph LR; A-->B;" %}
3435
```
3536

37+
### Mermaid version
38+
3639
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.
40+
mermaid, you can set the `MERMAID_VERSION` variable in your Django project's **settings.py** file.
3841

3942
```python
4043
MERMAID_VERSION = '10.0.3-alpha.1'
4144
```
4245

4346
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.
47+
the **mermaid.min.js** file.
48+
49+
### Mermaid theme
50+
51+
To set a default theme for the whole project, set the `MERMAID_THEME` variable in your Django project's **settings.py**
52+
file. However, Django Mermaid uses
53+
the [default](https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/themes/theme-default.js) theme of
54+
mermaid by default. Also, check out the mermaid [docs](https://mermaid.js.org/config/theming.html?#theme-variables) for
55+
the available themes.
56+
57+
```python
58+
MERMAID_THEME = 'neutral'
59+
```
60+
61+
Also, you can provide the theme right in the template tag which will dynamically override the value of
62+
the `MERMAID_THEME` variable.
63+
64+
```jinja2
65+
{% mermaid "graph LR; A-->B;" "dark" %}
66+
```
67+
68+
### Mermaid theme variables
69+
70+
You can define your custom theme by overriding the `MERMAID_THEME_VARIABLES` variable. You will need to use
71+
the [base](https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/themes/theme-base.js) theme as it is
72+
the only modifiable theme. Check out the mermaid [docs](https://mermaid.js.org/config/theming.html?#theme-variables) for
73+
the complete and up-to-date list of available theme variables.
74+
75+
```python
76+
MERMAID_THEME_VARIABLES = {
77+
'primaryColor': '#BB2528',
78+
'primaryTextColor': '#FFF',
79+
}
80+
```
4581

4682
## Contribute
4783

SECURITY.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Security Policy
2+
3+
## Downloading Mermaid from Trusted CDN
4+
5+
The Django Mermaid downloads a specified version of Mermaid from a trusted https://cdnjs.cloudflare.com/ Content
6+
Delivery Network (CDN) repository.
7+
8+
## Reporting Security Issues
9+
10+
If you discover a security issue in the Django Mermaid project, please do not hesitate to report it by emailing us
11+
at [[email protected]](mailto:[email protected]). We take all security issues seriously and will respond to your
12+
report as soon as possible.
13+
14+
Please provide as much information as possible when reporting a security issue, including steps to reproduce the issue,
15+
the expected behavior, and the actual behavior. This will help us to identify and fix the issue quickly.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = django-mermaid
33
version = attr: django_mermaid.__version__
44
author = Artyom Vancyan
55
author_email = [email protected]
6-
description = Django template tag for showing mermaid diagrams
6+
description = Django template tag for rendering mermaid diagrams
77
long_description = file: README.md
88
long_description_content_type = text/markdown
99
url = https://github.com/ArtyomVancyan/django-mermaid

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.3"
1+
__version__ = "0.0.4"

src/django_mermaid/templatetags/mermaid.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from django import template
24
from django.conf import settings
35
from django.templatetags.static import static
@@ -15,12 +17,14 @@ def mermaid(diagram=None, theme=None):
1517
"""Render a mermaid diagram.
1618
1719
:param diagram: The mermaid diagram definition
18-
:param theme: The mermaid theme to use (default, forest, dark, neutral). See https://mermaid.js.org/config/theming.
20+
:param theme: The mermaid theme to use (default, forest, dark, neutral, base). See https://mermaid.js.org/config/theming.
1921
"""
2022

2123
version = getattr(settings, "MERMAID_VERSION", DEFAULT_VERSION)
2224
theme = theme or getattr(settings, "MERMAID_THEME", DEFAULT_THEME)
25+
theme_variables = getattr(settings, "MERMAID_THEME_VARIABLES", {}) if theme == "base" else {}
2326

2427
mermaid_uri = static("mermaid/%s/mermaid.js" % version)
2528
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram or "", mermaid_uri)
26-
return html + "<script>mermaid.initialize({\"startOnLoad\": true, theme: \"%s\"});</script>" % theme
29+
init_properties = {"startOnLoad": True, "theme": theme, "themeVariables": theme_variables}
30+
return html + "<script>mermaid.initialize(%s);</script>" % json.dumps(init_properties)

tests/test_tag.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.template import Context
77
from django.template import Template
88
from django.test import override_settings
9-
109
from django_mermaid.templatetags import DEFAULT_THEME
1110

1211

@@ -16,7 +15,8 @@ def test_tag_use_in_template(version):
1615
template = template.render(Context({"content": "graph LR; A-->B;"}))
1716
assert template == (
1817
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
19-
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"%s\"});</script>" % (version, theme)
18+
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"%s\", \"themeVariables\": {}"
19+
"});</script>" % (version, theme)
2020
)
2121

2222

@@ -26,7 +26,8 @@ def test_tag_use_settings_theme(version):
2626
template = template.render(Context({"content": "graph LR; A-->B;"}))
2727
assert template == (
2828
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
29-
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"forest\"});</script>" % version
29+
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"forest\", \"themeVariables\""
30+
": {}});</script>" % version
3031
)
3132

3233

@@ -35,7 +36,30 @@ def test_tag_use_custom_theme(version):
3536
template = template.render(Context({"content": "graph LR; A-->B;"}))
3637
assert template == (
3738
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
38-
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"dark\"});</script>" % version
39+
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"dark\", \"themeVariables\": "
40+
"{}});</script>" % version
41+
)
42+
43+
44+
@override_settings(MERMAID_THEME_VARIABLES={"primaryColor": "red"})
45+
def test_tag_use_custom_theme_variables(version):
46+
template = Template("{% load mermaid %}{% mermaid content \"dark\" %}")
47+
template = template.render(Context({"content": "graph LR; A-->B;"}))
48+
assert template == (
49+
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
50+
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"dark\", \"themeVariables\": "
51+
"{}});</script>" % version
52+
)
53+
54+
55+
@override_settings(MERMAID_THEME="base", MERMAID_THEME_VARIABLES={"primaryColor": "#efefef"})
56+
def test_tag_use_custom_theme_variables_with_base_theme(version):
57+
template = Template("{% load mermaid %}{% mermaid content %}")
58+
template = template.render(Context({"content": "graph LR; A-->B;"}))
59+
assert template == (
60+
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
61+
"<script>mermaid.initialize({\"startOnLoad\": true, \"theme\": \"base\", \"themeVariables\": "
62+
"{\"primaryColor\": \"#efefef\"}});</script>" % version
3963
)
4064

4165

0 commit comments

Comments
 (0)