From 3f46cc1ef00a0d72aea4fa56e9939ce174325034 Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Fri, 19 Feb 2021 11:12:58 +0000 Subject: [PATCH] Fix broken links in lists and indented text 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 --- sync/sync.py | 7 +++++-- sync/test_sync.py | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sync/sync.py b/sync/sync.py index 99e8e7c2b..cb8770b4d 100755 --- a/sync/sync.py +++ b/sync/sync.py @@ -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") diff --git a/sync/test_sync.py b/sync/test_sync.py index 83e75a573..3acdd4749 100644 --- a/sync/test_sync.py +++ b/sync/test_sync.py @@ -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)", @@ -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):