diff --git a/mkdocs_drawio_exporter/exporter.py b/mkdocs_drawio_exporter/exporter.py index 3fdd0b2..8407ba1 100644 --- a/mkdocs_drawio_exporter/exporter.py +++ b/mkdocs_drawio_exporter/exporter.py @@ -126,14 +126,14 @@ def __hash__(self): def __repr__(self): return f"Source({self.source_embed}, {self.page_index}, {self.source_rel})" - def resolve_rel_path(self, page_dest_path): + def resolve_rel_path(self, page_src_path): """Resolve the path of the source, relative to the documentation directory. - :param str page_dest_path: The destination path of the parent page. + :param str page_src_path: The source path of the parent page. """ unescaped_source_embed = urllib.parse.unquote(self.source_embed) self.source_rel = os.path.normpath(os.path.join( - os.path.dirname(page_dest_path), + os.path.dirname(page_src_path), unescaped_source_embed)) @@ -248,7 +248,7 @@ def validate_config(self, config: Configuration): 'embed_format', config['embed_format'], 'cannot inline content of non-SVG format') - def rewrite_image_embeds(self, page_dest_path, output_content, config: Configuration): + def rewrite_image_embeds(self, page_src_path, output_content, config: Configuration): """Rewrite image embeds. :param str page_dest_path: Destination path. @@ -270,7 +270,7 @@ def replace(match): if fnmatch.fnmatch(filename, config['sources']): source = Source(filename, page_index) - source.resolve_rel_path(page_dest_path) + source.resolve_rel_path(page_src_path) content_sources.append(source) img_src = f"{filename}-{page_index}.{config['format']}" diff --git a/mkdocs_drawio_exporter/plugin.py b/mkdocs_drawio_exporter/plugin.py index e1bfa67..1387d8c 100644 --- a/mkdocs_drawio_exporter/plugin.py +++ b/mkdocs_drawio_exporter/plugin.py @@ -55,7 +55,7 @@ def on_config(self, config, **kwargs): def on_page_markdown(self, markdown, page, **kwargs): output_content, content_sources = self.exporter.rewrite_image_embeds( - page.file.dest_path, markdown, self.config) + page.file.src_path, markdown, self.config) self.sources += content_sources diff --git a/mkdocs_drawio_exporter/tests/exporter.py b/mkdocs_drawio_exporter/tests/exporter.py index a6f9458..ec6dfdc 100644 --- a/mkdocs_drawio_exporter/tests/exporter.py +++ b/mkdocs_drawio_exporter/tests/exporter.py @@ -5,7 +5,7 @@ import os from os.path import join, sep -from ..exporter import Configuration, ConfigurationError, DrawIoExporter +from ..exporter import Configuration, ConfigurationError, DrawIoExporter, Source class FileMock: @@ -17,12 +17,25 @@ def __init__(self, **kwargs): setattr(self, attr, value) +class SourceTests(unittest.TestCase): + def test_resolve_rel_path(self): + cases = [ + ("dir/diagram.drawio", ("diagram.drawio", 0), "dir/page.md"), + ("dir1/dir2/diagram.drawio", ("diagram.drawio", 0), "dir1/dir2/index.md"), + ("dir1/dir2/dir3/diagram.drawio", ("diagram.drawio", 0), "dir1/dir2/dir3/index.md"), + ] + for expect, source_args, page_src_path in cases: + with self.subTest(expect, source_args, page_src_path): + source = Source(*source_args) + result = source.resolve_rel_path(page_src_path) + self.assertEqual(expect, result) + + class ExporterTests(unittest.TestCase): log = None def setUp(self): self.log = logging.getLogger(__name__) - def make_exporter(self, docs_dir=None): if not docs_dir: docs_dir = sep + 'docs'