Skip to content

Commit 661598f

Browse files
yves-chevalliertuunit
authored andcommitted
feat: add support for page attribute
Signed-off-by: Jan Larwig <jan@larwig.com>
1 parent 57325f4 commit 661598f

File tree

4 files changed

+94
-56
lines changed

4 files changed

+94
-56
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# MkDocs Plugin for embedding Drawio files
2+
23
[![Publish Badge](https://github.com/tuunit/mkdocs-drawio/workflows/Publish/badge.svg)](https://github.com/tuunit/mkdocs-drawio/actions)
34
[![PyPI](https://img.shields.io/pypi/v/mkdocs-drawio)](https://pypi.org/project/mkdocs-drawio/)
45

56
Sergey ([onixpro](https://github.com/onixpro)) is the original creator of this plugin but since his repository isn't maintained anymore we forked it on the 19th December of 2023 and have been keeping it up-to-date and expanding on the features since then.
67
[Buy Sergey a ☕](https://www.buymeacoffee.com/SergeyLukin)
78

89
## Features
10+
911
This plugin enables you to embed interactive drawio diagrams in your documentation. Simply add your diagrams like you would any other image:
1012

1113
```markdown
@@ -25,18 +27,23 @@ Or you can use external urls:
2527
![](https://example.com/diagram.drawio)
2628
```
2729

28-
Additionally this plugin supports multi page diagrams by using the `alt` text to select the pages by name:
30+
Additionally this plugin supports multi page diagrams by using either the `page` or `alt` property. To use the `page` property, you need to use the markdown extension `attr_list`.
2931

3032
```markdown
33+
Either use the alt text:
3134
![Page-2](my-diagram.drawio)
3235
![my-custom-page-name](my-diagram.drawio)
36+
37+
Or use the page attribute:
38+
![Foo Diagram](my-diagram.drawio){ page="Page-2" }
39+
![Bar Diagram](my-diagram.drawio){ page="my-custom-page-name" }
3340
```
3441

3542
## Setup
3643

3744
Install plugin using pip:
3845

39-
```
46+
```bash
4047
pip install mkdocs-drawio
4148
```
4249

@@ -67,7 +74,7 @@ plugins:
6774
toolbar: true # control if hovering on a diagram shows a toolbar for zooming or not (default: true)
6875
tooltips: true # control if tooltips will be shown (default: true)
6976
edit: true # control if edit button will be shown in the lightbox view (default: true)
70-
border: 10 # increase or decrease the border / margin around your diagrams (default: 5)
77+
border: 10 # increase or decrease the border / margin around your diagrams (default: 5)
7178
```
7279
7380
## Material Integration
@@ -106,7 +113,6 @@ document$.subscribe(({ body }) => {
106113
4. Searches through the generated html for all `img` tags that have a source of type `.drawio`
107114
5. Replaces the found `img` tags with `mxgraph` html blocks (actual drawio diagram content). For more details, please have a look at the [official drawio.com documentation](https://www.drawio.com/doc/faq/embed-html).
108115

109-
110116
## Contribution guide
111117

112118
1. Setup a virtual environment: `python3 -m venv venv && source venv/bin/activate`
Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,61 @@
11
# Pagging
22

3-
## Examples
4-
5-
Below you should see diagram Page-1:
6-
![Page-1](test.drawio)
7-
8-
Below you should see diagram Page-2:
9-
![Page-2](test.drawio)
10-
11-
Below you should see Page-1 (default) because the specified Page-3 has not been found:
12-
![Page-3](test.drawio)
3+
You can either use the `alt` text of the image for pagging or use an attribute
4+
page for pagging if you have the `attr_list` markdown extension installed in
5+
your `mkdocs.yaml`.
136

14-
Furthoremore, you should see a warning in your mkdocs server similar to:
15-
16-
```bash
17-
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
7+
```yaml
8+
markdown_extensions:
9+
- attr_list
1810
```
1911
20-
## Markdown
12+
## Examples
2113
22-
```markdown
23-
![Page-1](test.drawio)
24-
![Page-2](test.drawio)
25-
![Page-3](test.drawio)
26-
```
14+
=== "Diagram"
15+
Below you should see diagram Page-1:
16+
![Page-1](test.drawio)
17+
18+
=== "Markdown"
19+
```markdown
20+
![Page-1](test.drawio)
21+
```
22+
23+
---
24+
25+
=== "Diagram"
26+
Below you should see diagram Page-2:
27+
![Page-2](test.drawio)
28+
29+
=== "Markdown"
30+
```markdown
31+
![Page-2](test.drawio)
32+
```
33+
34+
---
35+
36+
=== "Diagram"
37+
Below you should see diagram Page-2 using the attribute page:
38+
![Some alt text](test.drawio){ page="Page-2" }
39+
40+
=== "Markdown"
41+
```markdown
42+
![Some alt text](test.drawio){ page="Page-2" }
43+
```
44+
45+
---
46+
47+
=== "Diagram"
48+
Below you should see Page-1 (default) because the specified Page-3 has not been found:
49+
![Page-3](test.drawio)
50+
51+
Furthoremore, you should see a warning in your mkdocs server similar to:
52+
53+
```bash
54+
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
55+
```
56+
57+
=== "Markdown"
58+
```markdown
59+
If page doesn't exist it will fallback to the first page.
60+
![Page-3](test.drawio)
61+
```

mkdocs_drawio/plugin.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22
import json
3-
import mkdocs
43
import string
54
import logging
65
from lxml import etree
@@ -9,55 +8,47 @@
98
from pathlib import Path
109
from bs4 import BeautifulSoup
1110
from mkdocs.plugins import BasePlugin
11+
from mkdocs.config import base, config_options as c
1212

13-
# ------------------------
14-
# Constants and utilities
15-
# ------------------------
1613
SUB_TEMPLATE = string.Template(
1714
'<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="$config"></div>'
1815
)
1916

2017
LOGGER = logging.getLogger("mkdocs.plugins.diagrams")
2118

2219

23-
# ------------------------
24-
# Plugin
25-
# ------------------------
26-
class DrawioPlugin(BasePlugin):
20+
class DrawioConfig(base.Config):
21+
"""Configuration options for the Drawio Plugin"""
22+
23+
viewer_js = c.Type(
24+
str, default="https://viewer.diagrams.net/js/viewer-static.min.js"
25+
)
26+
toolbar = c.Type(bool, default=True)
27+
tooltips = c.Type(bool, default=True)
28+
border = c.Type(int, default=0)
29+
edit = c.Type(bool, default=True)
30+
31+
32+
class DrawioPlugin(BasePlugin[DrawioConfig]):
2733
"""
2834
Plugin for embedding Drawio Diagrams into your MkDocs
2935
"""
3036

31-
config_scheme = (
32-
(
33-
"viewer_js",
34-
mkdocs.config.config_options.Type(
35-
str, default="https://viewer.diagrams.net/js/viewer-static.min.js"
36-
),
37-
),
38-
("toolbar", mkdocs.config.config_options.Type(bool, default=True)),
39-
("tooltips", mkdocs.config.config_options.Type(bool, default=True)),
40-
("border", mkdocs.config.config_options.Type(int, default=0)),
41-
("edit", mkdocs.config.config_options.Type(bool, default=True)),
42-
)
43-
4437
def on_post_page(self, output_content, config, page, **kwargs):
4538
return self.render_drawio_diagrams(output_content, page)
4639

4740
def render_drawio_diagrams(self, output_content, page):
4841
if ".drawio" not in output_content.lower():
4942
return output_content
5043

51-
plugin_config = self.config.copy()
52-
5344
soup = BeautifulSoup(output_content, "html.parser")
5445

5546
diagram_config = {
56-
"toolbar": "zoom" if plugin_config["toolbar"] else None,
57-
"tooltips": "1" if plugin_config["tooltips"] else "0",
58-
"border": plugin_config["border"] + 5,
47+
"toolbar": "zoom" if self.config.toolbar else None,
48+
"tooltips": "1" if self.config.tooltips else "0",
49+
"border": self.config.border + 5,
5950
"resize": "1",
60-
"edit": "_blank" if plugin_config["edit"] else None,
51+
"edit": "_blank" if self.config.edit else None,
6152
}
6253

6354
# search for images using drawio extension
@@ -66,7 +57,7 @@ def render_drawio_diagrams(self, output_content, page):
6657
return output_content
6758

6859
# add drawio library to body
69-
lib = soup.new_tag("script", src=plugin_config["viewer_js"])
60+
lib = soup.new_tag("script", src=self.config.viewer_js)
7061
soup.body.append(lib)
7162

7263
# substitute images with embedded drawio diagram
@@ -79,9 +70,13 @@ def render_drawio_diagrams(self, output_content, page):
7970
"html.parser",
8071
)
8172
else:
73+
diagram_page = diagram["alt"]
74+
# Use page attribute instead of alt if it is set
75+
if "page" in diagram:
76+
diagram_page = diagram["page"]
8277
mxgraph = BeautifulSoup(
8378
DrawioPlugin.substitute_with_file(
84-
diagram_config, path, diagram["src"], diagram["alt"]
79+
diagram_config, path, diagram["src"], diagram_page
8580
),
8681
"html.parser",
8782
)

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[tool.poetry]
22
name = "mkdocs-drawio"
3-
version = "1.8.2"
3+
version = "1.9.0"
44
description = "MkDocs plugin for embedding Drawio files"
55
authors = [
66
"Jan Larwig <jan@larwig.com>",
7-
"Sergey Lukin <onixpro@gmail.com>"
7+
"Sergey Lukin <onixpro@gmail.com>",
88
]
99
license = "MIT"
1010
readme = "README.md"
@@ -19,9 +19,11 @@ packages = [
1919

2020
[tool.poetry.dependencies]
2121
python = ">=3.8.0,<4.0"
22+
requests = ">=2.0"
23+
Jinja2 = ">=3.0"
2224
beautifulsoup4 = ">=4.0"
2325
lxml = ">=4.0"
24-
mkdocs = ">=1.3"
26+
mkdocs = ">=1.4"
2527

2628
[tool.poetry.group.dev.dependencies]
2729
python = ">=3.8.0,<4.0"

0 commit comments

Comments
 (0)