-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
manifest.js
104 lines (95 loc) · 2.75 KB
/
manifest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* External dependencies
*/
const { camelCase, nth, upperFirst } = require( 'lodash' );
const fs = require( 'fs' );
const glob = require( 'glob' ).sync;
const baseRepoUrl = '..';
const componentPaths = glob( 'packages/components/src/*/**/README.md', {
// Don't expose documentation for mobile only and G2 components just yet.
ignore: [ '**/src/mobile/**/README.md', '**/src/ui/**/README.md' ],
} );
const packagePaths = glob( 'packages/*/package.json' ).map(
( fileName ) => fileName.split( '/' )[ 1 ]
);
/**
* Generates the package manifest.
*
* @param {Array} packageFolderNames Package folder names.
*
* @return {Array} Manifest
*/
function getPackageManifest( packageFolderNames ) {
return packageFolderNames.map( ( folderName ) => {
const path = `${ baseRepoUrl }/packages/${ folderName }/README.md`;
return {
title: `@wordpress/${ folderName }`,
slug: `packages-${ folderName }`,
markdown_source: path,
parent: 'packages',
};
} );
}
/**
* Generates the components manifest.
*
* @param {Array} paths Paths for all components
*
* @return {Array} Manifest
*/
function getComponentManifest( paths ) {
return paths.map( ( filePath ) => {
const slug = nth( filePath.split( '/' ), -2 );
return {
title: upperFirst( camelCase( slug ) ),
slug,
markdown_source: `${ baseRepoUrl }/${ filePath }`,
parent: 'components',
};
} );
}
function getRootManifest( tocFileName ) {
return generateRootManifestFromTOCItems( require( tocFileName ) );
}
function generateRootManifestFromTOCItems( items, parent = null ) {
let pageItems = [];
items.forEach( ( obj ) => {
const fileName = Object.keys( obj )[ 0 ];
const children = obj[ fileName ];
let slug = nth( fileName.split( '/' ), -1 ).replace( '.md', '' );
if ( 'readme' === slug.toLowerCase() ) {
slug = nth( fileName.split( '/' ), -2 );
// Special case - the root 'docs' readme needs the 'handbook' slug.
if ( parent === null && 'docs' === slug ) {
slug = 'handbook';
}
}
let title = upperFirst( camelCase( slug ) );
const markdownSource = fs.readFileSync( fileName, 'utf8' );
const titleMarkdown = markdownSource.match( /^#\s(.+)$/m );
if ( titleMarkdown ) {
title = titleMarkdown[ 1 ];
}
pageItems.push( {
title,
slug,
markdown_source: `${ baseRepoUrl }\/${ fileName }`,
parent,
} );
if ( Array.isArray( children ) && children.length ) {
pageItems = pageItems.concat(
generateRootManifestFromTOCItems( children, slug )
);
} else if ( children === '{{components}}' ) {
pageItems = pageItems.concat(
getComponentManifest( componentPaths )
);
} else if ( children === '{{packages}}' ) {
pageItems = pageItems.concat( getPackageManifest( packagePaths ) );
}
} );
return pageItems;
}
module.exports = {
getRootManifest,
};