This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcreateLearnPages.js
More file actions
86 lines (71 loc) · 2.91 KB
/
createLearnPages.js
File metadata and controls
86 lines (71 loc) · 2.91 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
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
const { defaultLanguage } = require('../locales');
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 getEdgesToLocaleMap(edges) {
const mapEdgesToLocale = new Map();
edges.forEach(edge => {
const { locale } = edge.node.fields;
if (!mapEdgesToLocale.has(locale)) {
mapEdgesToLocale.set(locale, []);
}
const localeEdges = mapEdgesToLocale.get(locale);
localeEdges.push(edge);
mapEdgesToLocale.set(locale, localeEdges);
});
return mapEdgesToLocale;
}
const getLearnEdgeByPageId = pageId => edge =>
getYamlPageIdentifier(edge.node.parent.relativePath) === pageId;
const getIteratedPagesForYaml = (yamlNavigation, edges) => {
const iteratedPagesForSection = {};
yamlNavigation.forEach(({ section, items }) => {
iteratedPagesForSection[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
// since learnPages are language independent we will use default edges
.map(pageId => edges.find(getLearnEdgeByPageId(pageId)))
.filter(edge => edge && edge.node)
);
iteratedPagesForSection[section] = iteratedPages;
});
return iteratedPagesForSection;
};
function getNavigationData(yamlNavigation, edges) {
const navigationData = {};
const iteratedPageForYaml = getIteratedPagesForYaml(yamlNavigation, edges);
Object.entries(iteratedPageForYaml).forEach(([section, iteratedPages]) => {
navigationData[section] = iteratedPages.map(mapToNavigationData);
});
return navigationData;
}
function getLearnPages(yamlNavigation, edges) {
const learnPages = [];
const iteratedPageForYaml = getIteratedPagesForYaml(yamlNavigation, edges);
Object.values(iteratedPageForYaml).forEach(iteratedPages => {
learnPages.push(...iteratedPages);
});
return learnPages;
}
function createLearnPages(learnEdges, yamlNavigationData) {
const mapEdgesToLocale = getEdgesToLocaleMap(learnEdges);
const learnPages = getLearnPages(
yamlNavigationData,
mapEdgesToLocale.get(defaultLanguage)
);
const navigationData = {};
mapEdgesToLocale.forEach((edges, locale) => {
navigationData[locale] = getNavigationData(yamlNavigationData, edges);
});
return { learnPages, navigationData };
}
module.exports = createLearnPages;