7
7
from pathlib import Path
8
8
from subprocess import run
9
9
from typing import List
10
+ import frontmatter
10
11
11
12
BASE_DIR = Path (__file__ ).resolve ().parent
12
13
@@ -121,8 +122,8 @@ def main(args):
121
122
122
123
def setup (args ):
123
124
doc_path = args .doc_path
124
- product = doc_path .parts [2 ]
125
- version = doc_path .parts [3 ]
125
+ product = doc_path .parts [- 2 ] if len ( doc_path . parts ) >= 4 else doc_path . parts [ - 1 ]
126
+ version = doc_path .parts [- 1 ] if len ( doc_path . parts ) >= 4 else ''
126
127
127
128
_file_prefix = f"{ product } _v{ version } _documentation"
128
129
@@ -140,8 +141,9 @@ def setup(args):
140
141
def list_files (doc_path , chapter = None , is_root_dir = True ):
141
142
chapter = [1 ] if chapter is None else chapter
142
143
all_files = []
144
+ nav_order = load_nav_index (doc_path )
143
145
directory_contents = sorted (
144
- filter (filter_path , doc_path .iterdir ()), key = put_index_first
146
+ filter (filter_path , doc_path .iterdir ()), key = lambda file : put_index_first ( file , nav_order )
145
147
)
146
148
147
149
for i , entry in enumerate (directory_contents ):
@@ -226,10 +228,20 @@ def parse_mdx(mdx_file):
226
228
front_matter , _ , content = mdx_file .read_text ().partition ("---" )[2 ].partition ("---" )
227
229
return front_matter .strip (), content .strip ()
228
230
229
-
230
- def put_index_first (path ):
231
- filename = str (path )
232
- return filename .replace ("index.mdx" , "00_index.mdx" )
231
+ def load_nav_index (path ):
232
+ try :
233
+ index = frontmatter .load (path / "index.mdx" )["navigation" ]
234
+ return index
235
+ except (FileNotFoundError , KeyError ):
236
+ return None
237
+
238
+ def put_index_first (path , nav_order ):
239
+ filename = path .name if path .suffix != ".mdx" else path .stem
240
+ nav_order = nav_order or [filename ]
241
+ indice = 0
242
+ if path .name != "index.mdx" :
243
+ indice = nav_order .index (filename )+ 1 if filename in nav_order else len (nav_order )
244
+ return f'{ indice :03d} _{ filename } '
233
245
234
246
235
247
def get_title (doc_path ):
0 commit comments