77without duplicating it.
88
99It also converts Github-style admonition blocks to HTML that mimics Github
10- styling.
10+ styling and converts internal anchor links to full GitHub repository URLs .
1111"""
1212
1313import re
@@ -74,16 +74,41 @@ def replace_admonition(match):
7474
7575 return re .sub (pattern , replace_admonition , text , flags = re .MULTILINE )
7676
77- def replace_blocks ( content_file , source_file , output_file = None ):
77+ def convert_internal_links ( text , base_url = "https://github.com/CarlosNZ/json-edit-react" ):
7878 """
79- Replace blocks in the content file with corresponding blocks from the source file.
80- Ignores block markers inside Markdown comments.
79+ Convert internal Markdown anchor links to full GitHub documentation links.
80+
81+ Args:
82+ text (str): The input text containing internal links
83+ base_url (str): The base URL for the GitHub repository
84+
85+ Returns:
86+ str: Text with converted links
87+ """
88+ # Regex to match internal Markdown links: [text](#anchor)
89+ # But avoid matching links that already have a full URL or are not anchors
90+ pattern = r'\[([^\]]+)\]\(#([^)]+)\)'
91+
92+ def replace_link (match ):
93+ link_text = match .group (1 )
94+ anchor = match .group (2 )
95+
96+ # Create the full GitHub URL with the anchor
97+ return f'[{ link_text } ]({ base_url } #{ anchor } )'
98+
99+ return re .sub (pattern , replace_link , text )
100+
101+ def replace_blocks (content_file , source_file , output_file = None , base_url = "https://github.com/CarlosNZ/json-edit-react" ):
102+ """
103+ Replace blocks in the content file with corresponding blocks from the source file,
104+ and convert internal links to full GitHub links.
81105
82106 Args:
83107 content_file (str): Path to the content markdown file
84108 source_file (str): Path to the source markdown file
85109 output_file (str, optional): Path to save the modified content.
86- If None, returns the modified content.
110+ If None, returns the modified content.
111+ base_url (str): The base URL for the GitHub repository
87112
88113 Returns:
89114 str: Modified content if no output file is specified
@@ -119,6 +144,9 @@ def replace_non_comment_blocks(match):
119144 # Convert Github admonition blocks
120145 modified_content = convert_github_admonition (modified_content )
121146
147+ # Convert internal links to full GitHub links
148+ modified_content = convert_internal_links (modified_content , base_url )
149+
122150 # If output file is specified, write to file
123151 if output_file :
124152 with open (output_file , 'w' , encoding = 'utf-8' ) as f :
@@ -133,7 +161,8 @@ def main():
133161 replace_blocks (
134162 content_file = 'README_npm.md' ,
135163 source_file = 'README.md' ,
136- output_file = 'README_npm_output.md'
164+ output_file = 'README_npm_output.md' ,
165+ base_url = "https://github.com/CarlosNZ/json-edit-react"
137166 )
138167
139168if __name__ == '__main__' :
0 commit comments