Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions examples/docs/tests/pagging/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ markdown_extensions:
---

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

=== "Markdown"
Expand All @@ -45,17 +45,47 @@ markdown_extensions:
---

=== "Diagram"
Below you should see Page-1 (default) because the specified Page-3 has not been found:
If the alt text doesn't exist as a page it will fallback to the first page of your diagram.
Below you should therefore see Page-1 (default):
![Page-3](test.drawio)

Furthoremore, you should see a warning in your mkdocs server similar to:

```bash
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
```

=== "Markdown"
```markdown
If page doesn't exist it will fallback to the first page.
![Page-3](test.drawio)
```

---

=== "Diagram"
If the alt text and page attribute don't exist as a page, it will fallback to the first page of your diagram.
Below you should therefore see Page-1 (default):
![Some alt text](test.drawio){ page="Page-3" }

Furthoremore, you should see a warning in your mkdocs server similar to:

```bash
WARNING - Warning: Found 0 results for page name 'Page-3' for diagram 'test.drawio' on path '/tmp/mkdocs_ce1qjhyn/test2'
```

=== "Markdown"
```markdown
![Some alt text](test.drawio){ page="Page-3" }
```

---

=== "Diagram"
Pagging logic for SVG diagrams is skip as only a single page can be exported as an SVG drawio diagram.

![Some alt text](test.drawio.svg){ page="Page-3" }

=== "Markdown"
```markdown
![Some alt text](test.drawio.svg){ page="Page-3" }
```
4 changes: 4 additions & 0 deletions examples/docs/tests/pagging/test.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion examples/docs/tests/svg/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

## Example

=== "Diagram"

The following is a SVG based drawio diagram:

![](test.drawio.svg)

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

## Markdown
=== "Markdown"

```markdown
![](test.drawio.svg)
```

64 changes: 33 additions & 31 deletions mkdocs_drawio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def render_drawio_diagrams(self, output_content, page):
"html.parser",
)
else:
diagram_page = diagram["alt"]
diagram_page = ""

# Use page attribute instead of alt if it is set
if "page" in diagram:
diagram_page = diagram["page"]
if diagram.has_attr("page"):
diagram_page = diagram.get("page")
else:
diagram_page = diagram.get("alt")

mxgraph = BeautifulSoup(
DrawioPlugin.substitute_with_file(
diagram_config, path, diagram["src"], diagram_page
Expand All @@ -96,48 +100,46 @@ def substitute_with_url(config: Dict, url: str) -> str:
return SUB_TEMPLATE.substitute(config=escape(json.dumps(config)))

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

diagram = DrawioPlugin.parse_diagram(diagram_xml, alt)
diagram = DrawioPlugin.parse_diagram(diagram_xml, page)
config["xml"] = diagram

return SUB_TEMPLATE.substitute(config=escape(json.dumps(config)))

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

try:
mxfile = data.xpath("//mxfile")[0]
if not mxfile_nodes:
if data.xpath("//*[local-name()='svg']") is not None:
return etree.tostring(data, encoding=str)
return ""

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

if len(page) == 1:
parser = etree.XMLParser()
result = parser.makeelement(mxfile.tag, mxfile.attrib)
if not page_name:
return etree.tostring(mxfile, encoding=str)

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

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

def on_config(self, config: base.Config):
"""Load embedded files"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mkdocs-drawio"
version = "1.11.2"
version = "1.11.3"
description = "MkDocs plugin for embedding Drawio files"
authors = [
"Jan Larwig <jan@larwig.com>",
Expand Down