7
7
without duplicating it.
8
8
9
9
It 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 .
11
11
"""
12
12
13
13
import re
@@ -74,16 +74,41 @@ def replace_admonition(match):
74
74
75
75
return re .sub (pattern , replace_admonition , text , flags = re .MULTILINE )
76
76
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" ):
78
78
"""
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.
81
105
82
106
Args:
83
107
content_file (str): Path to the content markdown file
84
108
source_file (str): Path to the source markdown file
85
109
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
87
112
88
113
Returns:
89
114
str: Modified content if no output file is specified
@@ -119,6 +144,9 @@ def replace_non_comment_blocks(match):
119
144
# Convert Github admonition blocks
120
145
modified_content = convert_github_admonition (modified_content )
121
146
147
+ # Convert internal links to full GitHub links
148
+ modified_content = convert_internal_links (modified_content , base_url )
149
+
122
150
# If output file is specified, write to file
123
151
if output_file :
124
152
with open (output_file , 'w' , encoding = 'utf-8' ) as f :
@@ -133,7 +161,8 @@ def main():
133
161
replace_blocks (
134
162
content_file = 'README_npm.md' ,
135
163
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"
137
166
)
138
167
139
168
if __name__ == '__main__' :
0 commit comments