1
1
import { GatsbyNode } from "gatsby"
2
2
import * as path from "path"
3
- import { promisify } from "util"
4
- import { readFile } from "fs/promises"
5
3
import * as globby from "globby"
6
- import * as frontmatterParser from "parser-front-matter"
7
- import { sortLibs } from "./scripts/sort-libraries/sort-libraries"
8
-
9
- const parse$ = promisify ( frontmatterParser . parse )
4
+ import { updateCodeData } from "./scripts/update-code-data/update-code-data"
5
+ import { organizeCodeData } from "./scripts/update-code-data/organize-code-data"
6
+ import { sortCodeData } from "./scripts/update-code-data/sort-code-data"
10
7
11
8
export const createSchemaCustomization : GatsbyNode [ "createSchemaCustomization" ] =
12
9
async ( { actions } ) => {
@@ -85,15 +82,10 @@ export const onCreatePage: GatsbyNode["onCreatePage"] = async ({
85
82
page,
86
83
actions,
87
84
} ) => {
88
- // trying to refactor code to be "the Gatsby way".
89
- // from the paths on ready, ignores a bunch of existing custom logic below.
90
- if ( page . path . startsWith ( "/blog" ) ) {
91
- return
92
- }
93
- if ( page . path . startsWith ( "/tags" ) ) {
85
+ // This way is not "the Gatsby way", we create the pages, delete the pages, and create "code" paths page again.
86
+ if ( page . path . startsWith ( "/blog" ) || page . path . startsWith ( "/tags" ) ) {
94
87
return
95
88
}
96
-
97
89
const { createPage, deletePage } = actions
98
90
deletePage ( page )
99
91
let context = {
@@ -102,144 +94,14 @@ export const onCreatePage: GatsbyNode["onCreatePage"] = async ({
102
94
}
103
95
if ( page . path === "/code" || page . path === "/code/" ) {
104
96
const markdownFilePaths = await globby ( "src/content/code/**/*.md" )
105
- const codeData : any = { }
106
97
const slugMap = require ( "./src/content/code/slug-map.json" )
107
- await Promise . all (
108
- markdownFilePaths . map ( async markdownFilePath => {
109
- const markdownFileContent = await readFile ( markdownFilePath , "utf-8" )
110
- let {
111
- data : { name, description, url, github, npm, gem } ,
112
- content : howto ,
113
- } = await parse$ ( markdownFileContent )
114
- howto = howto . trim ( )
115
- const pathArr = markdownFilePath . split ( "/" )
116
- if ( markdownFilePath . includes ( "language-support" ) ) {
117
- const languageSupportDirIndex = pathArr . indexOf ( "language-support" )
118
- const languageNameSlugIndex = languageSupportDirIndex + 1
119
- const languageNameSlug = pathArr [ languageNameSlugIndex ]
120
- const languageName = slugMap [ languageNameSlug ]
121
- codeData . Languages ||= { }
122
- codeData . Languages [ languageName ] ||= { }
123
-
124
- const categoryNameSlugIndex = languageSupportDirIndex + 2
125
- const categoryNameSlug = pathArr [ categoryNameSlugIndex ]
126
- const categoryName = slugMap [ categoryNameSlug ]
127
- codeData . Languages [ languageName ] [ categoryName ] ||= [ ]
128
- codeData . Languages [ languageName ] [ categoryName ] . push ( {
129
- name,
130
- description,
131
- howto,
132
- url,
133
- github,
134
- npm,
135
- gem,
136
- sourcePath : markdownFilePath ,
137
- } )
138
- } else if ( markdownFilePath . includes ( "tools" ) ) {
139
- const toolSupportDirIndex = pathArr . indexOf ( "tools" )
140
- const toolNameSlugIndex = toolSupportDirIndex + 1
141
- const toolNameSlug = pathArr [ toolNameSlugIndex ]
142
- const toolName = slugMap [ toolNameSlug ]
143
- codeData . ToolsNew ||= { }
144
- codeData . ToolsNew [ toolName ] ||= { }
145
- const categoryToolsNameSlugIndex = toolSupportDirIndex + 2
146
- const categoryToolsNameSlug = pathArr [ categoryToolsNameSlugIndex ]
147
- const categoryToolsName = slugMap [ categoryToolsNameSlug ]
148
- codeData . ToolsNew [ toolName ] [ categoryToolsName ] ||= [ ]
149
-
150
- codeData . ToolsNew [ toolName ] [ categoryToolsName ] . push ( {
151
- name,
152
- description,
153
- howto,
154
- url,
155
- github,
156
- npm,
157
- gem,
158
- sourcePath : markdownFilePath ,
159
- } )
160
- } else {
161
- const codeDirIndex = pathArr . indexOf ( "code" )
162
- const categoryNameSlugIndex = codeDirIndex + 1
163
- const categoryNameSlug = pathArr [ categoryNameSlugIndex ]
164
- const categoryName = slugMap [ categoryNameSlug ]
165
- codeData [ categoryName ] ||= [ ]
166
- codeData [ categoryName ] . push ( {
167
- name,
168
- description,
169
- howto,
170
- url,
171
- github,
172
- npm,
173
- gem,
174
- sourcePath : markdownFilePath ,
175
- } )
176
- }
177
- } )
178
- )
179
- const languageList : any = [ ]
180
- const toolList : any = [ ]
181
- await Promise . all ( [
182
- ...Object . keys ( codeData . Languages ) . map ( async languageName => {
183
- const libraryCategoryMap = codeData . Languages [ languageName ]
184
- let languageTotalStars = 0
185
- await Promise . all (
186
- Object . keys ( libraryCategoryMap ) . map ( async libraryCategoryName => {
187
- const libraries = libraryCategoryMap [ libraryCategoryName ]
188
- const { sortedLibs, totalStars } = await sortLibs ( libraries )
189
-
190
- libraryCategoryMap [ libraryCategoryName ] = sortedLibs
191
- languageTotalStars += totalStars || 0
192
- } )
193
- )
194
- languageList . push ( {
195
- name : languageName ,
196
- totalStars : languageTotalStars ,
197
- categoryMap : libraryCategoryMap ,
198
- } )
199
- } ) ,
200
- ...Object . keys ( codeData . ToolsNew ) . map ( async toolName => {
201
- const toolCategoryMap = codeData . ToolsNew [ toolName ]
202
- let toolTotalStars = 0
203
- await Promise . all (
204
- Object . keys ( toolCategoryMap ) . map ( async toolCategoryName => {
205
- const tools = toolCategoryMap [ toolCategoryName ]
206
- const { sortedLibs, totalStars } = await sortLibs ( tools )
207
- toolCategoryMap [ toolCategoryName ] = sortedLibs
208
- toolTotalStars += totalStars || 0
209
- } )
210
- )
211
- toolList . push ( {
212
- name : toolName ,
213
- totalStars : toolTotalStars ,
214
- categoryMap : toolCategoryMap ,
215
- } )
216
- } ) ,
217
- ] )
98
+ const codeData = await updateCodeData ( markdownFilePaths , slugMap )
99
+ const organizeData = await organizeCodeData ( codeData )
100
+ const sortedOrganizeData = await sortCodeData ( organizeData )
218
101
219
102
context = {
220
- ...context ,
221
- otherLibraries : {
222
- Services : codeData . Services ,
223
- "More Stuff" : codeData [ "More Stuff" ] ,
224
- } ,
225
- languageList : languageList . sort ( ( a , b ) => {
226
- if ( a . totalStars > b . totalStars ) {
227
- return - 1
228
- }
229
- if ( a . totalStars < b . totalStars ) {
230
- return 1
231
- }
232
- return 0
233
- } ) ,
234
- toolList : toolList . sort ( ( a , b ) => {
235
- if ( a . totalStars > b . totalStars ) {
236
- return - 1
237
- }
238
- if ( a . totalStars < b . totalStars ) {
239
- return 1
240
- }
241
- return 0
242
- } ) ,
103
+ sourcePath : path . relative ( __dirname , page . path ) ,
104
+ ...sortedOrganizeData ,
243
105
}
244
106
}
245
107
createPage ( {
0 commit comments