forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateMarkdownPages.js
More file actions
52 lines (42 loc) · 1.79 KB
/
Copy pathcreateMarkdownPages.js
File metadata and controls
52 lines (42 loc) · 1.79 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
42
43
44
45
46
47
48
49
50
51
52
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 createMarkdownPages(pagesEdges, learnEdges, yamlNavigationData) {
const learnPages = [];
const navigationData = {};
// Iterates the non-Learn edges and transforms them in Pages
const markdownPages = iterateEdges(pagesEdges);
const getLearnEdgeByPageId = pageId => edge =>
getYamlPageIdentifier(edge.node.parent.relativePath) === pageId;
// Handles the Navigation Data only of Learn pages
yamlNavigationData.forEach(({ section, items }) => {
navigationData[section] = {
category: 'learn',
data: [],
};
// 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].data = iteratedPages.map(mapToNavigationData);
// Then we push them to the resulting learn pages object
learnPages.push(...iteratedPages);
});
return {
markdownPages,
learnPages,
navigationData,
firstLearnPage: learnPages[0],
};
}
module.exports = createMarkdownPages;