Skip to content

Commit

Permalink
Fix broken links in lists and indented text
Browse files Browse the repository at this point in the history
When replacing links, we parse the markdown document line by line. When
the markdown parsers encounters an indented line, without the context of
the surrounding lines, it assumes a code block and does not render the
links to html, which means we do not re-write those links.

This patch fixes the issue by stripping left spaces in the text
fragments sent to "get_links", so that we can extract links successfully
and preserve the spaces in the rendered markdown.

Fixes: #229

Signed-off-by: Andrea Frittoli <andrea.frittoli@gmail.com>
  • Loading branch information
afrittoli committed Feb 19, 2021
1 parent 1a2e500 commit 3f46cc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ def decode(s, encodings=('utf8', 'latin1', 'ascii')):
def transform_line(line, base_path, local_files, rewrite_path, rewrite_url):
""" transform all the links in one line """
line = line.rstrip()
links = get_links(line)
# If there are links in this line we may need to fix them
# We need to left strip lines before paring for links because
# reading the markdown line by line, the context is lost and line
# indented because of a list is otherwise considered as a code block
# Links are ignored in code blocks
links = get_links(line.lstrip()) # If there are links in this line we may need to fix them
for link in links:
# link contains the text and href
href =link.get("href")
Expand Down
8 changes: 6 additions & 2 deletions sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ def test_transform_line(self):
"[notfound-relative-link-dotdot](../examples/notfound.txt)",
"[invalid-absolute-link](www.github.com)",
("[valid-absolute-link](https://website-random321.net#FRagment) "
"[valid-ref-link](#fooTEr)")
"[valid-ref-link](#fooTEr)"),
" - [exists-link-in-list](./test.txt)",
" [exists-link-indented](./test.txt) "
]
expected_results = [
"[exists-relative-link](/docs/test/test.txt)",
Expand All @@ -295,7 +297,9 @@ def test_transform_line(self):
"[notfound-relative-link-dotdot](http://test.com/tree/docs/examples/notfound.txt)",
"[invalid-absolute-link](http://test.com/tree/docs/www.github.com)",
("[valid-absolute-link](https://website-random321.net#FRagment) "
"[valid-ref-link](#footer)")
"[valid-ref-link](#footer)"),
" - [exists-link-in-list](/docs/test/test.txt)",
" [exists-link-indented](/docs/test/test.txt) "
]

for case, expected in zip(cases, expected_results):
Expand Down

0 comments on commit 3f46cc1

Please sign in to comment.