55SIDEBAR_LABEL_REGEX = re .compile ('sidebar_label: .*' , re .IGNORECASE )
66CUSTOM_EDIT_URL_REGEX = re .compile ('custom_edit_url: .*' , re .IGNORECASE )
77NAME_REGEX = re .compile ('name: .*' , re .IGNORECASE )
8+ RELATIVE_LINK_PATTERN = r"\]\((?!http[s]?://)([^)\s]+)\)"
89
910
1011def fix_self_non_closing_br_tags (content ):
@@ -61,11 +62,6 @@ def rename_name(repo, content):
6162 return NAME_REGEX .sub (f'name: { repo .name } ' , content )
6263
6364
64- def fix_links_to_examples (repo , content ):
65- return re .sub (r"(\[examples/.*])\((examples/.*)\)" ,
66- rf"\1(https://github.com/{ repo .full_name } /tree/{ repo .default_branch } /\2)" , content )
67-
68-
6965def parse_terraform_repo_name (name ):
7066 name_items = name .split ('-' )
7167 provider = name_items [1 ]
@@ -79,3 +75,27 @@ def parse_github_action_repo_name(name):
7975 action_name = '-' .join (name_items [2 :])
8076
8177 return action_name
78+
79+
80+ def replace_relative_links_with_github_links (repo , content , relative_path = None ):
81+ links = re .findall (RELATIVE_LINK_PATTERN , content )
82+
83+ for link in links :
84+ # ignore links to images, anchors and emails
85+ if link .startswith ('images/' ) or link .startswith ('#' ) or link .startswith ('mailto:' ):
86+ continue
87+
88+ updated_link = link
89+
90+ # remove leading './' or '/'
91+ if link .startswith ('./' ):
92+ updated_link = updated_link .replace ('./' , '' , 1 )
93+ elif link .startswith ('/' ):
94+ updated_link = updated_link .replace ('/' , '' , 1 )
95+
96+ if relative_path :
97+ updated_link = f"{ relative_path } /{ updated_link } "
98+
99+ content = content .replace (f"]({ link } )" , f"](https://github.com/{ repo .full_name } /tree/{ repo .default_branch } /{ updated_link } )" )
100+
101+ return content
0 commit comments