forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDocPages.js
More file actions
55 lines (48 loc) · 1.36 KB
/
createDocPages.js
File metadata and controls
55 lines (48 loc) · 1.36 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
function createDocPages(edges) {
const navigationData = {};
const docPages = [];
edges.forEach(({ node }, index) => {
const {
fields: { slug },
frontmatter: { title, section, category },
parent: { relativePath },
} = node;
let previousNodeData = null;
const previousNode = index === 0 ? undefined : edges[index - 1].node;
if (previousNode) {
previousNodeData = {
slug: previousNode.fields.slug,
title: previousNode.frontmatter.title,
};
}
let nextNodeData = null;
const nextNode =
index === edges.length - 1 ? undefined : edges[index + 1].node;
if (nextNode) {
nextNodeData = {
slug: nextNode.fields.slug,
title: nextNode.frontmatter.title,
};
}
let data;
if (!navigationData[section]) {
data = { title, slug, section, category };
navigationData[section] = { data: [data], category };
} else {
data = { title, slug, section, category };
navigationData[section] = {
data: [...navigationData[section].data, data],
category: navigationData[section].category,
};
}
docPages.push({
slug,
next: nextNodeData,
previous: previousNodeData,
relativePath,
category,
});
});
return docPages.map(page => ({ ...page, navigationData }));
}
module.exports = createDocPages;