Skip to content

Commit

Permalink
feat(relative): support fragment links
Browse files Browse the repository at this point in the history
  • Loading branch information
Umaaz committed May 10, 2024
1 parent ce6c2c6 commit d122c2f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
15 changes: 7 additions & 8 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def run(self, lines):
)

wanted_lines.extend(
original_text[current_start - 1 : current_end]
original_text[current_start - 1: current_end]
)
else:
wanted_line = int(block.strip())
Expand All @@ -174,19 +174,18 @@ def run(self, lines):
link = LINK_SYNTAX.search(text[i])
if link:
raw_path = link.group(2)
if not raw_path.startswith(
"http"
) and not raw_path.startswith("/"):
if not raw_path.startswith("http") and not raw_path.startswith(
"/") and not raw_path.startswith("#"):
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][: link.start(2)]
+ path_
+ text[i][link.end(2):]
)

text[i] = text[i].rstrip("\r\n")
text_to_insert = "\r\n".join(text)
line = line[: m.start()] + text_to_insert.strip() + line[m.end() :]
line = line[: m.start()] + text_to_insert.strip() + line[m.end():]
del lines[loc]
lines[loc:loc] = line.splitlines()
m = INC_SYNTAX.search(line)
Expand Down
1 change: 1 addition & 0 deletions tests/resources/docs/template/template_frag_link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[some link](#template)
8 changes: 8 additions & 0 deletions tests/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def test_abs_url_link(markdown_include):

assert html == "<p><a href=\"/template\">some link</a></p>"


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

assert html == "<p><a href=\"#template\">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])
Expand Down

0 comments on commit d122c2f

Please sign in to comment.