Skip to content

Commit

Permalink
Add the images folder in triggers sync config
Browse files Browse the repository at this point in the history
Pull in SVG images from triggers docs/images into the main docs
folder, so that we can display it correctly.

Handle rewriting links in images with the "src" attribute.
Extend unit tests accordingly.

Partially addresses #340

Signed-off-by: Andrea Frittoli <andrea.frittoli@gmail.com>
  • Loading branch information
afrittoli committed Feb 16, 2022
1 parent 4e5fe00 commit 997aff6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 3 additions & 0 deletions sync/config/triggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,6 @@ tags:
include:
- create-ingress.yaml
- create-webhook.yaml
docs/images:
include: ['*.svg']
target: .
8 changes: 7 additions & 1 deletion sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def transform_links_doc(text, base_path, local_files, rewrite_path, rewrite_url)
# Rewrite map, only use links with an href
rewrite_map = {x.get("href"): transform_link(x.get("href"), base_path, local_files, rewrite_path, rewrite_url)
for x in links if x.get("href")}
rewrite_map.update({x.get("src"): transform_link(x.get("src"), base_path, local_files, rewrite_path, rewrite_url)
for x in links if x.get("src")})
for source, target in rewrite_map.items():
text = text.replace(f'({source})', f'({target})')
return text
Expand All @@ -245,7 +247,9 @@ def get_links(md):
""" return a list of all the links in a string formatted in markdown """
md = markdown.markdown(md)
soup = BeautifulSoup(md, 'html.parser')
return soup.find_all("a")
links = soup.find_all("a")
links.extend(soup.find_all("img"))
return links


def transform_link(link, base_path, local_files, rewrite_path, rewrite_url):
Expand Down Expand Up @@ -306,6 +310,8 @@ def transform_link(link, base_path, local_files, rewrite_path, rewrite_url):
new_path = [rewrite_path, target_file]
return parsed._replace(path="/".join(new_path)).geturl()
# when not found on disk, append to the base_url
if not rewrite_url.endswith("/"):
rewrite_url += "/"
return urljoin(rewrite_url, parsed._replace(path=fq_path).geturl())


Expand Down
30 changes: 17 additions & 13 deletions sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,16 @@ def test_transform_link(self):
def test_transform_links_doc(self):
self.maxDiff = None

# Links are in a page stored undrer base_path
# Links are in a page stored under base_path
base_path = 'test-content'

# The following pages are synced
local_files = {
f'{base_path}/content.md': '_index.md',
f'{base_path}/else.md': 'else.md',
f'{base_path}/test.txt': 'test.txt',
'some_other_folder/with_contend.md': 'with_contend.md'
f'{base_path}/content.md': ['_index.md', ''],
f'{base_path}/else.md': ['else.md', ''],
f'{base_path}/test.txt': ['test.txt', ''],
'some_other_folder/with_content.md': ['with_content.md', 'else'],
f'{base_path}/tekton.png': ['tekton.png', '']
}

cases = [
Expand All @@ -296,29 +297,32 @@ def test_transform_links_doc(self):
("[valid-absolute-link](https://website-random321.net#FRagment) "
"[valid-ref-link](#fooTEr)"),
("Valid link broken on two lines [exists-link-in-list]("
"./test.txt)")
"./test.txt)"),
"[exists-png-image](./tekton.png)"
]
expected_results = [
"[exists-relative-link](/docs/test/test.txt)",
"[exists-relative-link-index](/docs/test/)\nand\n[exists-relative-link-index](/docs/test/#with-fragment)",
"[else](/docs/test/else) and [else](/docs/test/else/)\nand\n[else-frag](/docs/test/else/#with-fragment) and [again](/docs/test/else/#with-another-fragment)",
"[exists-relative-link-other-path](/docs/test/else/)",
"[else](/docs/test/else/) and [else2](/docs/test/else/)\nand\n[else-frag](/docs/test/else/#with-fragment) and [again](/docs/test/else/#with-another-fragment)",
"[exists-relative-link-other-path](/docs/test/else/with_content/)",
"[exists-relative-link-fragment](/docs/test/test.txt#Fragment)",
"[notfound-relative-link](http://test.com/tree/docs/test/this/is/not/found.txt#FraGment)",
"[notfound-relative-link-fragment](http://test.com/tree/docs/test/this/is/not/found.md#fraGmenT)",
"[notfound-relative-link-dotdot](http://test.com/tree/docs/examples/notfound.txt)",
"[invalid-absolute-link](http://test.com/tree/docs/github.com)",
"[notfound-relative-link](http://test.com/tree/docs/test/test-content/this/is/not/found.txt#FraGment)",
"[notfound-relative-link-fragment](http://test.com/tree/docs/test/test-content/this/is/not/found.md#fraGmenT)",
"[notfound-relative-link-dotdot](http://test.com/tree/docs/test/examples/notfound.txt)",
"[invalid-absolute-link](http://test.com/tree/docs/test/test-content/github.com)",
("[valid-absolute-link](https://website-random321.net#FRagment) "
"[valid-ref-link](#footer)"),
("Valid link broken on two lines [exists-link-in-list]("
"/docs/test/test.txt)")
"/docs/test/test.txt)"),
"[exists-png-image](/docs/test/tekton.png)"
]

for case, expected in zip(cases, expected_results):
actual = transform_links_doc(
text=case, base_path=base_path, local_files=local_files,
rewrite_path='/docs/test', rewrite_url='http://test.com/tree/docs/test'
)
self.assertEqual(actual, expected)

def test_read_front_matter(self):
cases = [
Expand Down

0 comments on commit 997aff6

Please # to comment.