@@ -89,27 +89,68 @@ export function updateContentFiles() {
89
89
90
90
if ( appliesToNoSupportedGhesReleases ) {
91
91
if ( Object . keys ( data . versions ) . length === 1 ) {
92
- console . log ( 'Removing file: ' , file )
93
- fs . unlinkSync ( file )
94
- const indexFile = file . replace ( path . basename ( file ) , 'index.md' )
95
- const indexFileContent = fs . readFileSync ( indexFile , 'utf8' )
96
- const { content, data } = frontmatter ( indexFileContent ) as {
97
- content : string | undefined
98
- data : { children : string [ ] } | undefined
99
- }
100
- if ( ! data ) continue
101
- data . children = data . children . filter ( ( child ) => child !== '/' + path . basename ( file , '.md' ) )
102
- console . log ( '..Updating children in: ' , indexFile )
103
- fs . writeFileSync (
104
- indexFile ,
105
- frontmatter . stringify ( content || '' , data , { lineWidth : - 1 } as any ) ,
106
- )
107
- continue
92
+ removeFileUpdateParent ( file )
93
+ } else {
94
+ // Remove the ghes property from versions Fm and return
95
+ delete data . versions . ghes
96
+ console . log ( 'Removing GHES version from: ' , file )
97
+ fs . writeFileSync ( file , frontmatter . stringify ( content , data , { lineWidth : - 1 } as any ) )
108
98
}
109
- // Remove the ghes property from versions Fm and return
110
- delete data . versions . ghes
111
- console . log ( 'Removing GHES version from: ' , file )
112
- fs . writeFileSync ( file , frontmatter . stringify ( content , data , { lineWidth : - 1 } as any ) )
113
99
}
114
100
}
115
101
}
102
+
103
+ function removeFileUpdateParent ( filePath : string ) {
104
+ console . log ( 'Removing file: ' , filePath )
105
+ fs . unlinkSync ( filePath )
106
+ const filePathDirectory = path . dirname ( filePath )
107
+ if ( fs . readdirSync ( filePathDirectory ) . length === 0 ) {
108
+ fs . rmdirSync ( filePathDirectory )
109
+ }
110
+ const parentFilePath = getParentFilePath ( filePath )
111
+ if ( ! parentFilePath ) return
112
+ const indexFileContent = fs . readFileSync ( parentFilePath , 'utf8' )
113
+ const { content, data } = frontmatter ( indexFileContent ) as {
114
+ content : string | undefined
115
+ data : { children : string [ ] } | undefined
116
+ }
117
+ if ( ! data ) return
118
+ // Children paths are relative to the index.md file's directory
119
+ const childPath = filePath . endsWith ( 'index.md' )
120
+ ? '/' + path . basename ( path . dirname ( filePath ) )
121
+ : '/' + path . basename ( filePath , '.md' )
122
+
123
+ // Remove the childPath from the parent index.md file's children frontmatter
124
+ data . children = data . children . filter ( ( child ) => child !== childPath )
125
+
126
+ // If removing the childPath leaves the parent index.md file empty, remove it
127
+ if ( data . children . length === 0 ) {
128
+ removeFileUpdateParent ( parentFilePath )
129
+ } else {
130
+ console . log ( '..Updating children in: ' , parentFilePath )
131
+ fs . writeFileSync (
132
+ parentFilePath ,
133
+ frontmatter . stringify ( content || '' , data , { lineWidth : - 1 } as any ) ,
134
+ )
135
+ }
136
+ }
137
+
138
+ // Gets the next parent file path.
139
+ // If the filePath is an article (e.g., doesn't end with index.md),
140
+ // then the parent file is the index.md file in the same directory.
141
+ // If the filePath is a category or subcategory (e.g., ends with index.md),
142
+ // the parent is the index.md file in the next directory up.
143
+ function getParentFilePath ( filePath : string ) {
144
+ // This is the root index.md file, it has no parent
145
+ if ( ! filePath || filePath === 'content/index.md' ) return null
146
+ // Handle index.md files with index.md parent in directory above
147
+ if ( filePath . endsWith ( 'index.md' ) ) {
148
+ const pathParts = filePath . split ( '/' )
149
+ pathParts . pop ( )
150
+ pathParts . pop ( )
151
+ pathParts . push ( 'index.md' )
152
+ return pathParts . join ( '/' )
153
+ }
154
+ // Handle articles with a parent index.md file
155
+ return filePath . replace ( path . basename ( filePath ) , 'index.md' )
156
+ }
0 commit comments