@@ -19,6 +19,9 @@ def extract_inline_code(file_path, languages):
19
19
if child .type != "fence" :
20
20
continue
21
21
22
+ if not child .info :
23
+ continue
24
+
22
25
info_string = child .info .split ()
23
26
language = info_string [0 ]
24
27
flags = info_string [1 :]
@@ -91,14 +94,18 @@ def replace(path, i, language, first_line, last_line):
91
94
return
92
95
93
96
with open (path , "r" ) as f :
94
- markdown_content = f .read ()
95
- lines = markdown_content .split ("\n " )
97
+ lines = f .read ().split ("\n " )
98
+ pretty_lines = pretty_content .split ("\n " )
99
+
100
+ offset = len (pretty_lines ) - len (lines [first_line + 1 : last_line - 1 ])
96
101
97
- lines [first_line + 1 : last_line - 1 ] = pretty_content . split ( " \n " )
102
+ lines [first_line + 1 : last_line - 1 ] = pretty_lines
98
103
99
104
with open (path , "w" ) as f :
100
105
f .write ("\n " .join (lines ))
101
106
107
+ return offset
108
+
102
109
103
110
def autoformat (path , i , language , first_line , last_line ):
104
111
file_name = f"{ path } .snippet-{ i } .{ language } "
@@ -184,8 +191,14 @@ def parse_args():
184
191
parsed_markdowns = get_markdown_files (args .path , args .languages )
185
192
186
193
for path in parsed_markdowns ["files" ].keys ():
194
+ offset = 0
187
195
for i , code_block in enumerate (parsed_markdowns ["files" ][path ]["code-blocks" ]):
188
196
197
+ # adjust line numbers after replace
198
+ for cb in parsed_markdowns ["files" ][path ]["code-blocks" ][i :]:
199
+ cb ["first_line" ] += offset
200
+ cb ["last_line" ] += offset
201
+
189
202
if args .extract and "noextract" not in code_block ["flags" ]:
190
203
extract (
191
204
path ,
@@ -211,7 +224,7 @@ def parse_args():
211
224
check_output ()
212
225
213
226
if args .replace and "noreplace" not in code_block ["flags" ]:
214
- replace (
227
+ offset = replace (
215
228
path ,
216
229
i + 1 ,
217
230
supported_languages [code_block ["language" ]],
0 commit comments