@@ -86,17 +86,15 @@ def reset_knowledge_base():
86
86
with open (kb_file_path , 'w' ) as output_file :
87
87
json .dump ([], output_file )
88
88
89
- def parse_markdown_file_to_json (file_path ):
89
+ def parse_markdown_file_to_json (current_id , file_path ):
90
90
""" Parses individual markdown file and adds its content to JSON """
91
91
try :
92
92
# Load existing content if the file exists
93
93
with open (kb_file_path , 'r' ) as existing_file :
94
94
json_output = json .load (existing_file )
95
- current_id = len (json_output ) + 1 # Start ID from the next available number
96
95
except (FileNotFoundError , json .JSONDecodeError ):
97
96
# If the file doesn't exist or is empty, start fresh
98
97
json_output = []
99
- current_id = 1
100
98
101
99
with open (file_path , 'r' , encoding = 'utf-8' ) as file :
102
100
lines = file .readlines ()
@@ -148,7 +146,6 @@ def parse_markdown_file_to_json(file_path):
148
146
"text" : text ,
149
147
"path" : adjust_knowledge_base_entry_path (file_path ) # Adjust path format
150
148
})
151
- current_id += 1
152
149
153
150
# Write the augmented JSON output to ./data/knowledge_base.json
154
151
with open (kb_file_path , 'w' , encoding = 'utf-8' ) as output_file :
@@ -158,17 +155,15 @@ def adjust_knowledge_base_entry_path(file_path):
158
155
""" Adjusts the file path format for storage. """
159
156
return re .sub (r'\/(\d{4})-(\d{2})-(\d{2})-' , r'/\1/\2/\3/' , file_path .replace ("./.tmp/defang-docs" , "" ).replace (".mdx" , "" ).replace (".md" , "" ))
160
157
161
- def parse_cli_markdown (file_path ):
158
+ def parse_cli_markdown (current_id , file_path ):
162
159
""" Parses CLI-specific markdown files """
163
160
try :
164
161
# Load existing content if the file exists
165
162
with open (kb_file_path , 'r' ) as existing_file :
166
163
json_output = json .load (existing_file )
167
- current_id = len (json_output ) + 1 # Start ID from the next available number
168
164
except (FileNotFoundError , json .JSONDecodeError ):
169
165
# If the file doesn't exist or is empty, start fresh
170
166
json_output = []
171
- current_id = 1
172
167
173
168
with open (file_path , 'r' , encoding = 'utf-8' ) as file :
174
169
lines = file .readlines ()
@@ -192,7 +187,6 @@ def parse_cli_markdown(file_path):
192
187
"text" : text ,
193
188
"path" : file_path .replace ("./.tmp/defang-docs" , "" ).replace (".mdx" , "" ).replace (".md" , "" )
194
189
})
195
- current_id += 1
196
190
197
191
# Write the augmented JSON output to data/knowledge_base.json
198
192
with open (kb_file_path , 'w' , encoding = 'utf-8' ) as output_file :
@@ -206,11 +200,11 @@ def recursive_parse_directory(root_dir):
206
200
lower_filename = filename .lower ()
207
201
if lower_filename .endswith ('.md' ) or lower_filename .endswith ('.mdx' ):
208
202
paths .append (os .path .join (dirpath , filename ))
209
- for file_path in paths :
203
+ for id , file_path in enumerate ( paths , start = 1 ) :
210
204
if 'cli' in dirpath .lower () or 'cli' in filename .lower ():
211
- parse_cli_markdown (file_path )
205
+ parse_cli_markdown (id , file_path )
212
206
else :
213
- parse_markdown_file_to_json (file_path )
207
+ parse_markdown_file_to_json (id , file_path )
214
208
215
209
if __name__ == "__main__" :
216
210
setup_repositories ()
0 commit comments