Skip to content

Commit

Permalink
feat(relative): add support for rewriting links in included files
Browse files Browse the repository at this point in the history
  • Loading branch information
Umaaz committed Feb 19, 2024
1 parent 8da79f0 commit f9847e2
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 7 deletions.
Binary file added examples/mkdocs/docs/images/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/mkdocs/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Title

This is a simple example of using MKdocs with mkdocs-include.

{!template/template.md!}


{!template/template_2.md!}
Binary file added examples/mkdocs/docs/template/some_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/mkdocs/docs/template/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![some_image.jpg](some_image.jpg)
1 change: 1 addition & 0 deletions examples/mkdocs/docs/template/template_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![some_image.jpg](../images/image1.jpg)
5 changes: 5 additions & 0 deletions examples/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
site_name: Example markdown-include

markdown_extensions:
- markdown_include.include:
base_path: docs
11 changes: 9 additions & 2 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Binary file added tests/resources/docs/images/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/docs/template/some_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/resources/docs/template/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![some_image.jpg](some_image.jpg)
1 change: 1 addition & 0 deletions tests/resources/docs/template/template_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![some_image.jpg](../images/image1.jpg)
1 change: 1 addition & 0 deletions tests/resources/with_img_link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![some link](https://google.com)
1 change: 1 addition & 0 deletions tests/resources/with_link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[some link](https://google.com)
36 changes: 31 additions & 5 deletions tests/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pytest


RESOURCE_DIR = pathlib.Path(__file__).parent.absolute() / "resources"


Expand All @@ -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 == "<p><img alt=\"some_image.jpg\" src=\"docs/template/some_image.jpg\" /></p>"


def test_relative_path_parent(markdown_include):
source = "{!docs/template/template_2.md!}"
html = markdown.markdown(source, extensions=[markdown_include])

assert html == "<p><img alt=\"some_image.jpg\" src=\"docs/template/../images/image1.jpg\" /></p>"


def test_relative_path_url_link(markdown_include):
source = "{!with_link.md!}"
html = markdown.markdown(source, extensions=[markdown_include])

assert html == "<p><a href=\"https://google.com\">some link</a></p>"

def test_relative_path_img_url_link(markdown_include):
source = "{!with_img_link.md!}"
html = markdown.markdown(source, extensions=[markdown_include])

assert html == "<p><img alt=\"some link\" src=\"https://google.com\" /></p>"


def test_single_include(markdown_include):
source = "{!simple.md!}"
html = markdown.markdown(source, extensions=[markdown_include])
Expand All @@ -34,7 +60,7 @@ def test_double_include(markdown_include):
html = markdown.markdown(source, extensions=[markdown_include])

assert (
html == "<p>This is a simple template and This is another simple template</p>"
html == "<p>This is a simple template and This is another simple template</p>"
)


Expand Down Expand Up @@ -68,8 +94,8 @@ def test_embedded_template(markdown_include):
html = markdown.markdown(source, extensions=[markdown_include])

assert (
html
== "<p>This is a simple template</p>\n<p>This is a template with a template.</p>"
html
== "<p>This is a simple template</p>\n<p>This is a template with a template.</p>"
)


Expand All @@ -89,7 +115,7 @@ def test_double_include_inherit_heading_depth(markdown_include_inherit_heading_d
)

assert (
html == "<p>This is a simple template and This is another simple template</p>"
html == "<p>This is a simple template and This is another simple template</p>"
)


Expand Down

0 comments on commit f9847e2

Please sign in to comment.