Skip to content

Commit f3cb068

Browse files
authored
fix: pagging handling in case of missing page and skip for SVG based diagrams (#40)
Signed-off-by: Jan Larwig <jan@larwig.com>
1 parent 4a10c4b commit f3cb068

File tree

5 files changed

+77
-38
lines changed

5 files changed

+77
-38
lines changed

examples/docs/tests/pagging/index.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ markdown_extensions:
3434
---
3535

3636
=== "Diagram"
37-
Below you should see diagram Page-2 using the attribute page:
37+
Below you should see the diagram Page-2 using the page attribute:
3838
![Some alt text](test.drawio){ page="Page-2" }
3939

4040
=== "Markdown"
@@ -45,17 +45,47 @@ markdown_extensions:
4545
---
4646

4747
=== "Diagram"
48-
Below you should see Page-1 (default) because the specified Page-3 has not been found:
48+
If the alt text doesn't exist as a page it will fallback to the first page of your diagram.
49+
Below you should therefore see Page-1 (default):
4950
![Page-3](test.drawio)
50-
51+
5152
Furthoremore, you should see a warning in your mkdocs server similar to:
52-
53+
5354
```bash
5455
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
5556
```
5657

5758
=== "Markdown"
5859
```markdown
59-
If page doesn't exist it will fallback to the first page.
6060
![Page-3](test.drawio)
6161
```
62+
63+
---
64+
65+
=== "Diagram"
66+
If the alt text and page attribute don't exist as a page, it will fallback to the first page of your diagram.
67+
Below you should therefore see Page-1 (default):
68+
![Some alt text](test.drawio){ page="Page-3" }
69+
70+
Furthoremore, you should see a warning in your mkdocs server similar to:
71+
72+
```bash
73+
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
74+
```
75+
76+
=== "Markdown"
77+
```markdown
78+
![Some alt text](test.drawio){ page="Page-3" }
79+
```
80+
81+
---
82+
83+
=== "Diagram"
84+
Pagging logic for SVG diagrams is skip as only a single page can be exported as an SVG drawio diagram.
85+
86+
![Some alt text](test.drawio.svg){ page="Page-3" }
87+
88+
=== "Markdown"
89+
```markdown
90+
![Some alt text](test.drawio.svg){ page="Page-3" }
91+
```
Lines changed: 4 additions & 0 deletions
Loading

examples/docs/tests/svg/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
## Example
44

5+
=== "Diagram"
6+
57
The following is a SVG based drawio diagram:
68

79
![](test.drawio.svg)
810

911
You can open the diagram as an SVG in your browser. [Click here.](test.drawio.svg)
1012

11-
## Markdown
13+
=== "Markdown"
1214

1315
```markdown
1416
![](test.drawio.svg)
1517
```
18+

mkdocs_drawio/plugin.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ def render_drawio_diagrams(self, output_content, page):
7474
"html.parser",
7575
)
7676
else:
77-
diagram_page = diagram["alt"]
77+
diagram_page = ""
78+
7879
# Use page attribute instead of alt if it is set
79-
if "page" in diagram:
80-
diagram_page = diagram["page"]
80+
if diagram.has_attr("page"):
81+
diagram_page = diagram.get("page")
82+
else:
83+
diagram_page = diagram.get("alt")
84+
8185
mxgraph = BeautifulSoup(
8286
DrawioPlugin.substitute_with_file(
8387
diagram_config, path, diagram["src"], diagram_page
@@ -96,48 +100,46 @@ def substitute_with_url(config: Dict, url: str) -> str:
96100
return SUB_TEMPLATE.substitute(config=escape(json.dumps(config)))
97101

98102
@staticmethod
99-
def substitute_with_file(config: Dict, path: Path, src: str, alt: str) -> str:
103+
def substitute_with_file(config: Dict, path: Path, src: str, page: str) -> str:
100104
try:
101105
diagram_xml = etree.parse(path.joinpath(src).resolve())
102-
except Exception:
106+
except Exception as e:
103107
LOGGER.error(
104-
f"Error: Provided diagram file '{src}' on path '{path}' is not a valid diagram"
108+
f"Error: Could not parse diagram file '{src}' on path '{path}': {e}"
105109
)
106-
diagram_xml = etree.fromstring("<invalid/>")
110+
config["xml"] = ""
111+
return SUB_TEMPLATE.substitute(config=escape(json.dumps(config)))
107112

108-
diagram = DrawioPlugin.parse_diagram(diagram_xml, alt)
113+
diagram = DrawioPlugin.parse_diagram(diagram_xml, page)
109114
config["xml"] = diagram
110-
111115
return SUB_TEMPLATE.substitute(config=escape(json.dumps(config)))
112116

113117
@staticmethod
114-
def parse_diagram(data, alt, src="", path=None) -> str:
115-
if alt is None or len(alt) == 0:
116-
return etree.tostring(data, encoding=str)
118+
def parse_diagram(data, page_name, src="", path=None) -> str:
119+
mxfile_nodes = data.xpath("//mxfile")
117120

118-
try:
119-
mxfile = data.xpath("//mxfile")[0]
121+
if not mxfile_nodes:
122+
if data.xpath("//*[local-name()='svg']") is not None:
123+
return etree.tostring(data, encoding=str)
124+
return ""
120125

121-
# try to parse for a specific page by using the alt attribute
122-
page = mxfile.xpath(f"//diagram[@name='{alt}']")
126+
mxfile = mxfile_nodes[0]
123127

124-
if len(page) == 1:
125-
parser = etree.XMLParser()
126-
result = parser.makeelement(mxfile.tag, mxfile.attrib)
128+
if not page_name:
129+
return etree.tostring(mxfile, encoding=str)
127130

128-
result.append(page[0])
129-
return etree.tostring(result, encoding=str)
130-
else:
131-
LOGGER.warning(
132-
f"Warning: Found {len(page)} results for page name '{alt}' for diagram '{src}' on path '{path}'"
133-
)
131+
page = mxfile.xpath(f"//diagram[@name='{page_name}']")
132+
if len(page) == 1:
133+
parser = etree.XMLParser()
134+
result = parser.makeelement(mxfile.tag, mxfile.attrib)
135+
result.append(page[0])
136+
return etree.tostring(result, encoding=str)
134137

135-
return etree.tostring(mxfile, encoding=str)
136-
except Exception:
137-
LOGGER.error(
138-
f"Error: Could not properly parse page name '{alt}' for diagram '{src}' on path '{path}'"
139-
)
140-
return ""
138+
LOGGER.warning(
139+
f"Warning: Found {len(page)} results for page name '{page_name}' "
140+
f"falling back to first page."
141+
)
142+
return etree.tostring(mxfile, encoding=str)
141143

142144
def on_config(self, config: base.Config):
143145
"""Load embedded files"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mkdocs-drawio"
3-
version = "1.11.2"
3+
version = "1.11.3"
44
description = "MkDocs plugin for embedding Drawio files"
55
authors = [
66
"Jan Larwig <jan@larwig.com>",

0 commit comments

Comments
 (0)