forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateLearnPages.js
More file actions
41 lines (32 loc) · 1.55 KB
/
createLearnPages.js
File metadata and controls
41 lines (32 loc) · 1.55 KB
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
const { iterateEdges, mapToNavigationData } = require('./createPageUtils');
function getYamlPageIdentifier(relativePath) {
// Include optional possible language code file extension suffixes
// eg.: index.en.md, index.md, index.en.mdx, some-blog-post.md, ...
return relativePath.includes('/index.')
? relativePath.replace(/\/index(\.[a-z]+)?\.(mdx|md)/, '')
: relativePath.replace(/(\.[a-z]+)?\.(mdx|md)/, '');
}
function createLearnPages(learnEdges, yamlNavigationData) {
const learnPages = [];
const navigationData = {};
const getLearnEdgeByPageId = pageId => edge =>
getYamlPageIdentifier(edge.node.parent.relativePath) === pageId;
// Handles the Navigation Data only of Learn pages
yamlNavigationData.forEach(({ section, items }) => {
navigationData[section] = [];
// This adds the items to the navigation section data based on the order defined within the YAML file
// If the page doesn't exist it will be set as null and then removed via Array.filter()
const iteratedPages = iterateEdges(
items
// Iterates the items of the section and retrieve their respective edges
// then we transform them into pages and add to the navigation data
.map(pageId => learnEdges.find(getLearnEdgeByPageId(pageId)))
.filter(edge => edge && edge.node)
);
navigationData[section] = iteratedPages.map(mapToNavigationData);
// Then we push them to the resulting learn pages object
learnPages.push(...iteratedPages);
});
return { learnPages, navigationData };
}
module.exports = createLearnPages;