diff --git a/examples/docs/tests/pagging/index.md b/examples/docs/tests/pagging/index.md index e81d85d..689a2d4 100644 --- a/examples/docs/tests/pagging/index.md +++ b/examples/docs/tests/pagging/index.md @@ -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" @@ -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" } + ``` diff --git a/examples/docs/tests/pagging/test.drawio.svg b/examples/docs/tests/pagging/test.drawio.svg new file mode 100644 index 0000000..14c2887 --- /dev/null +++ b/examples/docs/tests/pagging/test.drawio.svg @@ -0,0 +1,4 @@ + + + +
a
a
c
c
SVG based Drawio Diagram
SVG based Drawio Diagram
Text is not SVG - cannot display
\ No newline at end of file diff --git a/examples/docs/tests/svg/index.md b/examples/docs/tests/svg/index.md index 6c8e5ea..37ac5b2 100644 --- a/examples/docs/tests/svg/index.md +++ b/examples/docs/tests/svg/index.md @@ -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) ``` + diff --git a/mkdocs_drawio/plugin.py b/mkdocs_drawio/plugin.py index 216632c..4ecc831 100644 --- a/mkdocs_drawio/plugin.py +++ b/mkdocs_drawio/plugin.py @@ -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 @@ -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("") + 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""" diff --git a/pyproject.toml b/pyproject.toml index 6e7459a..b950402 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 ",