@@ -4,17 +4,7 @@ import { JSDOM } from 'jsdom';
4
4
import { Marked } from 'marked' ;
5
5
import { gfmHeadingId , getHeadingList } from 'marked-gfm-heading-id' ;
6
6
import markedAlert from 'marked-alert' ;
7
-
8
- const DOCS_DIR = 'docs/web/' ;
9
- const BLOG_DIR = 'docs/web/blog/' ;
10
- const RESULT_DIR = 'website/dist/' ;
11
- const TEMPLATES_DIR = 'website/templates/' ;
12
- const SRC_DIR = 'website/src/' ;
13
- const VERSIONS_FILE = 'website/config/versions.json' ;
14
- const TEMPLATE_PATH = `${ TEMPLATES_DIR } index.html` ;
15
- const BUNDLES_PATH = './bundles' ;
16
- const BUNDLE_NAME = 'core-js-bundle.js' ;
17
- const BUNDLE_NAME_ESMODULES = 'core-js-bundle-esmodules.js' ;
7
+ import config from './config/config.mjs' ;
18
8
19
9
const args = process . argv ;
20
10
const lastArg = args . at ( - 1 ) ;
@@ -25,7 +15,7 @@ const BASE = BRANCH ? `/branches/${ BRANCH }/` : '/';
25
15
async function getDefaultVersion ( ) {
26
16
if ( BRANCH ) return BRANCH ;
27
17
28
- const versions = await readJSON ( VERSIONS_FILE ) ;
18
+ const versions = await readJSON ( config . versionsFile ) ;
29
19
return versions . find ( v => v . default ) ?. label ;
30
20
}
31
21
@@ -53,9 +43,8 @@ async function buildDocsMenu(item) {
53
43
result += '</ul></li>' ;
54
44
return result ;
55
45
}
56
- const defaultVersion = BRANCH || DEFAULT_VERSION ;
57
46
58
- return `<li><a href="${ item . url } " class="with-docs-version" data-default-version="${ defaultVersion } ">${ item . title } </a></li>` ;
47
+ return `<li><a href="${ item . url } " class="with-docs-version" data-default-version="${ DEFAULT_VERSION } ">${ item . title } </a></li>` ;
59
48
}
60
49
61
50
async function isExists ( target ) {
@@ -75,7 +64,7 @@ async function getDocsMenuItems(version) {
75
64
if ( docsMenuItems [ version ] ) return docsMenuItems [ version ] ;
76
65
77
66
echo ( chalk . green ( `Getting menu items from file for version: ${ version } ` ) ) ;
78
- const jsonPath = BRANCH ? `${ DOCS_DIR } docs/menu.json` : `${ DOCS_DIR } ${ version } /docs/menu.json` ;
67
+ const jsonPath = BRANCH ? `${ config . docsDir } docs/menu.json` : `${ config . docsDir } ${ version } /docs/menu.json` ;
79
68
const exists = await isExists ( jsonPath ) ;
80
69
if ( ! exists ) {
81
70
echo ( chalk . yellow ( `Menu JSON file not found: ${ jsonPath } ` ) ) ;
@@ -104,10 +93,10 @@ async function buildDocsMenuForVersion(version) {
104
93
async function buildVersionsMenuList ( versions , currentVersion , section ) {
105
94
let versionsMenuHtml = '<div class="dropdown-block">' ;
106
95
for ( const v of versions ) {
107
- const activityClass = v . label === currentVersion ? ' class="active"' : '' ;
96
+ const activityClass = v . label === currentVersion && ! v . default ? ' class="active"' : '' ;
108
97
const defaultBadge = v . default ? ' (default)' : '' ;
109
98
const versionPath = v . default ? '' : `${ v . label } /` ;
110
- versionsMenuHtml += `<a href="./${ versionPath } ${ section } / "${ activityClass } >${ v . label } ${ defaultBadge } </a>` ;
99
+ versionsMenuHtml += `<a href="./${ versionPath } ${ section } "${ activityClass } >${ v . label } ${ defaultBadge } </a>` ;
111
100
}
112
101
versionsMenuHtml += '</div>' ;
113
102
@@ -146,7 +135,6 @@ const linkRenderer = {
146
135
} ;
147
136
148
137
function buildMenus ( html ) {
149
- const defaultVersion = BRANCH || DEFAULT_VERSION ;
150
138
const headings = getHeadingList ( ) . filter ( ( { level } ) => level > 1 ) ;
151
139
let result = '<div class="wrapper">' ;
152
140
if ( isBlog ) {
@@ -159,7 +147,7 @@ function buildMenus(html) {
159
147
result += `<div class="table-of-contents sticky"><div class="mobile-trigger"></div>
160
148
${ headings . map ( ( { id, raw, level } ) => `<div class="toc-link"><a href="${
161
149
htmlFileName . replace ( '.html' , '' ) } #${ id } " class="h${
162
- level } with-docs-version" data-default-version="${ defaultVersion } ">${
150
+ level } with-docs-version" data-default-version="${ DEFAULT_VERSION } ">${
163
151
raw } </a></div>`) . join ( '\n' ) }
164
152
</div>` ;
165
153
}
@@ -187,7 +175,7 @@ let blogMenuCache = '';
187
175
async function buildBlogMenu ( ) {
188
176
if ( blogMenuCache !== '' ) return blogMenuCache ;
189
177
190
- const mdFiles = await getAllMdFiles ( BLOG_DIR ) ;
178
+ const mdFiles = await getAllMdFiles ( config . blogDir ) ;
191
179
mdFiles . reverse ( ) ;
192
180
let index = '---\ndisableContentMenu: true\n---\n# Blog\n\n' ;
193
181
let menu = '<ul>' ;
@@ -205,14 +193,14 @@ async function buildBlogMenu() {
205
193
206
194
const match = mdPath . match ( / (?< date > \d { 4 } - \d { 2 } - \d { 2 } ) - / ) ;
207
195
const date = match && match . groups ? match . groups . date : null ;
208
- const htmlFileName = mdPath . replace ( BLOG_DIR , '' ) . replace ( / \. m d $ / i, '' ) ;
196
+ const htmlFileName = mdPath . replace ( config . blogDir , '' ) . replace ( / \. m d $ / i, '' ) ;
209
197
menu += `<li><a href="./blog/${ htmlFileName } ">${ date } : ${ firstH1 . text } </a></li>` ;
210
198
index += `## [${ firstH1 . text } ](./blog/${
211
199
htmlFileName } )\n\n*${ date } *\n\n${ fileMetadata . preview ?? '' } \n\n`;
212
200
}
213
201
menu += '</ul>' ;
214
202
blogMenuCache = menu ;
215
- const blogIndexPath = path . join ( BLOG_DIR , 'index.md' ) ;
203
+ const blogIndexPath = path . join ( config . blogDir , 'index.md' ) ;
216
204
await fs . writeFile ( blogIndexPath , index , 'utf8' ) ;
217
205
echo ( chalk . green ( `File created: ${ blogIndexPath } ` ) ) ;
218
206
@@ -228,7 +216,7 @@ async function getVersionTags() {
228
216
async function getVersionFromMdFile ( mdPath ) {
229
217
const match = mdPath . match ( / \/ w e b \/ (?< version > [ ^ / ] + ) \/ d o c s \/ / ) ;
230
218
if ( match && match . groups && match . groups . version ) {
231
- return match . groups . version
219
+ return match . groups . version ;
232
220
}
233
221
return DEFAULT_VERSION ;
234
222
}
@@ -245,23 +233,23 @@ async function buildPlaygrounds(template, versions) {
245
233
}
246
234
247
235
async function buildPlayground ( template , version , versions ) {
248
- const bundleScript = `<script nomodule src="${ BUNDLES_PATH } /${ version . label } /${ BUNDLE_NAME } "></script>` ;
249
- const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH } /${ version . label } /${ BUNDLE_NAME_ESMODULES } "></script>` ;
250
- const playgroundContent = await readFile ( `${ SRC_DIR } playground.html` ) ;
251
- const versionsMenu = await buildVersionsMenu ( versions , version , 'playground' ) ;
236
+ const bundleScript = `<script nomodule src="${ config . bundlesPath } /${ version . label } /${ config . bundleName } "></script>` ;
237
+ const bundleESModulesScript = `<script type="module" src="${ config . bundlesPath } /${ version . label } /${ config . bundleNameESModules } "></script>` ;
238
+ const playgroundContent = await readFile ( `${ config . srcDir } playground.html` ) ;
239
+ const versionsMenu = await buildVersionsMenu ( versions , version . label , 'playground' ) ;
252
240
let playground = template . replace ( '{content}' , playgroundContent ) ;
253
241
playground = playground . replace ( '{base}' , BASE ) ;
254
242
playground = playground . replace ( '{title}' , 'Playground - ' ) ;
255
243
playground = playground . replace ( '{base}' , BASE ) ;
256
244
playground = playground . replace ( '{core-js-bundle}' , bundleScript ) ;
257
245
playground = playground . replace ( '{core-js-bundle-esmodules}' , bundleESModulesScript ) ;
258
246
const playgroundWithVersion = playground . replace ( '{versions-menu}' , versionsMenu ) ;
259
- const playgroundFilePath = path . join ( RESULT_DIR , version . label , 'playground.html' ) ;
247
+ const playgroundFilePath = path . join ( config . resultDir , version . label , 'playground.html' ) ;
260
248
261
249
if ( version . default ) {
262
- const defaultVersionsMenu = await buildVersionsMenu ( versions , version , 'playground' ) ;
250
+ const defaultVersionsMenu = await buildVersionsMenu ( versions , version . label , 'playground' ) ;
263
251
const defaultVersionPlayground = playground . replace ( '{versions-menu}' , defaultVersionsMenu ) ;
264
- const defaultPlaygroundPath = path . join ( RESULT_DIR , 'playground.html' ) ;
252
+ const defaultPlaygroundPath = path . join ( config . resultDir , 'playground.html' ) ;
265
253
await fs . writeFile ( defaultPlaygroundPath , defaultVersionPlayground , 'utf8' ) ;
266
254
echo ( chalk . green ( `File created: ${ defaultPlaygroundPath } ` ) ) ;
267
255
} else {
@@ -274,17 +262,17 @@ async function buildPlayground(template, version, versions) {
274
262
async function createDocsIndexes ( versions ) {
275
263
if ( BRANCH ) {
276
264
const menuItems = await getDocsMenuItems ( BRANCH ) ;
277
- const firstDocPath = path . join ( RESULT_DIR , `${ menuItems [ 0 ] . url } .html` . replace ( '{docs-version}/' , '' ) ) ;
278
- const indexFilePath = path . join ( RESULT_DIR , 'docs/' , 'index.html' ) ;
265
+ const firstDocPath = path . join ( config . resultDir , `${ menuItems [ 0 ] . url } .html` . replace ( '{docs-version}/' , '' ) ) ;
266
+ const indexFilePath = path . join ( config . resultDir , 'docs/' , 'index.html' ) ;
279
267
await fs . copy ( firstDocPath , indexFilePath ) ;
280
268
echo ( chalk . green ( `File created: ${ indexFilePath } ` ) ) ;
281
269
return ;
282
270
}
283
271
284
272
for ( const version of versions ) {
285
273
const menuItems = await getDocsMenuItems ( version . label ) ;
286
- const firstDocPath = path . join ( RESULT_DIR , `${ menuItems [ 0 ] . url } .html` . replace ( '{docs-version}' , version . label ) ) ;
287
- const indexFilePath = path . join ( RESULT_DIR , `${ version . label } /docs/` , 'index.html' ) ;
274
+ const firstDocPath = path . join ( config . resultDir , `${ menuItems [ 0 ] . url } .html` . replace ( '{docs-version}' , version . label ) ) ;
275
+ const indexFilePath = path . join ( config . resultDir , `${ version . label } /docs/` , 'index.html' ) ;
288
276
await fs . copy ( firstDocPath , indexFilePath ) ;
289
277
echo ( chalk . green ( `File created: ${ indexFilePath } ` ) ) ;
290
278
}
@@ -306,7 +294,7 @@ async function getVersions() {
306
294
default : true ,
307
295
} ] ;
308
296
}
309
- const versions = await readJSON ( VERSIONS_FILE ) ;
297
+ const versions = await readJSON ( config . versionsFile ) ;
310
298
echo ( chalk . green ( 'Got versions from file' ) ) ;
311
299
312
300
return versions ;
@@ -324,13 +312,12 @@ let isDocs = false;
324
312
let isChangelog ;
325
313
326
314
async function build ( ) {
327
- const template = await readFile ( TEMPLATE_PATH ) ;
315
+ const template = await readFile ( config . templatePath ) ;
328
316
await buildBlogMenu ( ) ;
329
- const mdFiles = await getAllMdFiles ( DOCS_DIR ) ;
317
+ const mdFiles = await getAllMdFiles ( config . docsDir ) ;
330
318
const versions = await getVersions ( ) ;
331
- const defaultVersion = BRANCH || DEFAULT_VERSION ;
332
- const bundleScript = `<script nomodule src="${ BUNDLES_PATH } /${ defaultVersion } /${ BUNDLE_NAME } "></script>` ;
333
- const bundleESModulesScript = `<script type="module" src="${ BUNDLES_PATH } /${ defaultVersion } /${ BUNDLE_NAME_ESMODULES } "></script>` ;
319
+ const bundleScript = `<script nomodule src="${ config . bundlesPath } /${ DEFAULT_VERSION } /${ config . bundleName } "></script>` ;
320
+ const bundleESModulesScript = `<script type="module" src="${ config . bundlesPath } /${ DEFAULT_VERSION } /${ config . bundleNameESModules } "></script>` ;
334
321
335
322
let currentVersion = '' ;
336
323
let versionsMenu = '' ;
@@ -347,11 +334,11 @@ async function build() {
347
334
if ( currentVersion !== versionFromMdFile ) {
348
335
currentVersion = versionFromMdFile ;
349
336
docsMenu = await buildDocsMenuForVersion ( currentVersion ) ;
350
- versionsMenu = await buildVersionsMenu ( versions , currentVersion , 'docs' ) ;
337
+ versionsMenu = await buildVersionsMenu ( versions , currentVersion , 'docs/ ' ) ;
351
338
}
352
339
353
- htmlFileName = mdPath . replace ( DOCS_DIR , '' ) . replace ( / \. m d $ / i, '.html' ) ;
354
- const htmlFilePath = path . join ( RESULT_DIR , htmlFileName ) ;
340
+ htmlFileName = mdPath . replace ( config . docsDir , '' ) . replace ( / \. m d $ / i, '.html' ) ;
341
+ const htmlFilePath = path . join ( config . resultDir , htmlFileName ) ;
355
342
const htmlContent = isDocs || isBlog || isChangelog ? markedWithContents . parse ( content ) : marked . parse ( content ) ;
356
343
357
344
let resultHtml = template . replace ( '{content}' , htmlContent . replaceAll ( '$' , '$' ) ) ;
0 commit comments