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