diff --git a/examples/mkdocs/docs/images/image1.jpg b/examples/mkdocs/docs/images/image1.jpg new file mode 100644 index 0000000..202ffb3 Binary files /dev/null and b/examples/mkdocs/docs/images/image1.jpg differ diff --git a/examples/mkdocs/docs/index.md b/examples/mkdocs/docs/index.md new file mode 100644 index 0000000..9f5b291 --- /dev/null +++ b/examples/mkdocs/docs/index.md @@ -0,0 +1,8 @@ +# Title + +This is a simple example of using MKdocs with mkdocs-include. + +{!template/template.md!} + + +{!template/template_2.md!} \ No newline at end of file diff --git a/examples/mkdocs/docs/template/some_image.jpg b/examples/mkdocs/docs/template/some_image.jpg new file mode 100644 index 0000000..66e6f9f Binary files /dev/null and b/examples/mkdocs/docs/template/some_image.jpg differ diff --git a/examples/mkdocs/docs/template/template.md b/examples/mkdocs/docs/template/template.md new file mode 100644 index 0000000..245c536 --- /dev/null +++ b/examples/mkdocs/docs/template/template.md @@ -0,0 +1 @@ +![some_image.jpg](some_image.jpg) \ No newline at end of file diff --git a/examples/mkdocs/docs/template/template_2.md b/examples/mkdocs/docs/template/template_2.md new file mode 100644 index 0000000..3bfd5e9 --- /dev/null +++ b/examples/mkdocs/docs/template/template_2.md @@ -0,0 +1 @@ +![some_image.jpg](../images/image1.jpg) \ No newline at end of file diff --git a/examples/mkdocs/mkdocs.yml b/examples/mkdocs/mkdocs.yml new file mode 100644 index 0000000..bf63a93 --- /dev/null +++ b/examples/mkdocs/mkdocs.yml @@ -0,0 +1,5 @@ +site_name: Example markdown-include + +markdown_extensions: + - markdown_include.include: + base_path: docs diff --git a/markdown_include/include.py b/markdown_include/include.py index e4b18bf..7de08c7 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -30,6 +30,7 @@ INC_SYNTAX = re.compile(r"{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}") HEADING_SYNTAX = re.compile("^#+") +LINK_SYNTAX = re.compile("(!|)\[[^]]+]\(([^)]+)\)") class MarkdownInclude(Extension): @@ -99,8 +100,8 @@ def run(self, lines): m = INC_SYNTAX.search(line) while m: - filename = m.group(1) - filename = os.path.expanduser(filename) + relative_filename = m.group(1) + filename = os.path.expanduser(relative_filename) if not os.path.isabs(filename): filename = os.path.normpath( os.path.join(self.base_path, filename) @@ -170,6 +171,12 @@ def run(self, lines): text[i] = bonusHeading + text[i] if self.headingOffset: text[i] = "#" * self.headingOffset + text[i] + link = LINK_SYNTAX.search(text[i]) + if link: + raw_path = link.group(2) + if not raw_path.startswith("http"): + path_ = f"{os.path.dirname(relative_filename)}{os.path.sep}{raw_path}" + text[i] = text[i][:link.start(2)] + path_ + text[i][link.end(2):] text[i] = text[i].rstrip("\r\n") text_to_insert = "\r\n".join(text) diff --git a/tests/resources/docs/images/image1.jpg b/tests/resources/docs/images/image1.jpg new file mode 100644 index 0000000..202ffb3 Binary files /dev/null and b/tests/resources/docs/images/image1.jpg differ diff --git a/tests/resources/docs/template/some_image.jpg b/tests/resources/docs/template/some_image.jpg new file mode 100644 index 0000000..66e6f9f Binary files /dev/null and b/tests/resources/docs/template/some_image.jpg differ diff --git a/tests/resources/docs/template/template.md b/tests/resources/docs/template/template.md new file mode 100644 index 0000000..245c536 --- /dev/null +++ b/tests/resources/docs/template/template.md @@ -0,0 +1 @@ +![some_image.jpg](some_image.jpg) \ No newline at end of file diff --git a/tests/resources/docs/template/template_2.md b/tests/resources/docs/template/template_2.md new file mode 100644 index 0000000..3bfd5e9 --- /dev/null +++ b/tests/resources/docs/template/template_2.md @@ -0,0 +1 @@ +![some_image.jpg](../images/image1.jpg) \ No newline at end of file diff --git a/tests/resources/with_img_link.md b/tests/resources/with_img_link.md new file mode 100644 index 0000000..7448962 --- /dev/null +++ b/tests/resources/with_img_link.md @@ -0,0 +1 @@ +![some link](https://google.com) \ No newline at end of file diff --git a/tests/resources/with_link.md b/tests/resources/with_link.md new file mode 100644 index 0000000..e13c0c5 --- /dev/null +++ b/tests/resources/with_link.md @@ -0,0 +1 @@ +[some link](https://google.com) \ No newline at end of file diff --git a/tests/test_include.py b/tests/test_include.py index eb2da12..6f5dfe7 100644 --- a/tests/test_include.py +++ b/tests/test_include.py @@ -6,7 +6,6 @@ import pytest - RESOURCE_DIR = pathlib.Path(__file__).parent.absolute() / "resources" @@ -22,6 +21,33 @@ def markdown_include_inherit_heading_depth(): ) +def test_relative_path(markdown_include): + source = "{!docs/template/template.md!}" + html = markdown.markdown(source, extensions=[markdown_include]) + + assert html == "
" + + +def test_relative_path_parent(markdown_include): + source = "{!docs/template/template_2.md!}" + html = markdown.markdown(source, extensions=[markdown_include]) + + assert html == "" + + +def test_relative_path_url_link(markdown_include): + source = "{!with_link.md!}" + html = markdown.markdown(source, extensions=[markdown_include]) + + assert html == "" + +def test_relative_path_img_url_link(markdown_include): + source = "{!with_img_link.md!}" + html = markdown.markdown(source, extensions=[markdown_include]) + + assert html == "" + + def test_single_include(markdown_include): source = "{!simple.md!}" html = markdown.markdown(source, extensions=[markdown_include]) @@ -34,7 +60,7 @@ def test_double_include(markdown_include): html = markdown.markdown(source, extensions=[markdown_include]) assert ( - html == "This is a simple template and This is another simple template
" + html == "This is a simple template and This is another simple template
" ) @@ -68,8 +94,8 @@ def test_embedded_template(markdown_include): html = markdown.markdown(source, extensions=[markdown_include]) assert ( - html - == "This is a simple template
\nThis is a template with a template.
" + html + == "This is a simple template
\nThis is a template with a template.
" ) @@ -89,7 +115,7 @@ def test_double_include_inherit_heading_depth(markdown_include_inherit_heading_d ) assert ( - html == "This is a simple template and This is another simple template
" + html == "This is a simple template and This is another simple template
" )