@@ -12,28 +12,30 @@ function addFrontmatter(filePath, navOrder) {
12
12
// Skip if file already has frontmatter
13
13
if ( content . startsWith ( "---" ) ) return ;
14
14
15
+ const relPath = path . relative ( DOCS_DIR , filePath ) ;
16
+ const parts = relPath . split ( path . sep ) ;
15
17
const baseName = path . basename ( filePath , ".md" ) ;
16
18
17
- // Handle title generation
18
- let title ;
19
- if ( / ^ i n d e x $ / i. test ( baseName ) ) {
20
- // Use parent folder name for index.md files
21
- title = path . basename ( path . dirname ( filePath ) ) ;
19
+ let title , parent ;
20
+ if ( relPath . toLowerCase ( ) === "index.md" ) {
21
+ // Root index.md
22
+ title = "React18 Loaders" ; // or "Home"
23
+ } else if ( / ^ i n d e x $ / i. test ( baseName ) ) {
24
+ // index.md inside a subfolder → use folder name as title
25
+ title = capitalize ( parts [ parts . length - 2 ] ) ;
26
+ if ( parts . length > 2 ) parent = capitalize ( parts [ parts . length - 3 ] || "" ) ; // parent folder’s parent
22
27
} else {
23
- title = baseName ;
28
+ // Regular file
29
+ title = capitalize ( baseName ) ;
30
+ if ( parts . length > 1 ) {
31
+ parent = capitalize ( parts [ parts . length - 2 ] ) ;
32
+ }
24
33
}
25
34
26
- // Capitalize each word
27
- title = title
28
- . split ( / | - / )
29
- . map ( word => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
30
- . join ( " " ) ;
31
-
32
- // Create YAML frontmatter
33
35
const frontmatter = `---
34
36
layout: default
35
37
title: ${ title }
36
- nav_order: ${ navOrder }
38
+ ${ parent ? `parent: ${ parent } \n` : "" } nav_order: ${ navOrder }
37
39
---
38
40
39
41
` ;
@@ -42,6 +44,10 @@ nav_order: ${navOrder}
42
44
console . log ( `✅ Added frontmatter to: ${ filePath } ` ) ;
43
45
}
44
46
47
+ function capitalize ( str ) {
48
+ return str . replace ( / [ - _ ] / g, " " ) . replace ( / \b \w / g, c => c . toUpperCase ( ) ) ;
49
+ }
50
+
45
51
/**
46
52
* Recursively process directory contents, sorting so:
47
53
* 1. Files before directories
0 commit comments