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 pathcreatePageUtils.js
More file actions
76 lines (63 loc) · 1.73 KB
/
createPageUtils.js
File metadata and controls
76 lines (63 loc) · 1.73 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
function createPreviousNextNodeData(edges, index, categoryName) {
let previousNodeData = null;
const previousNode = index === 0 ? undefined : edges[index - 1].node;
if (previousNode && previousNode.fields.categoryName === categoryName) {
previousNodeData = {
slug: previousNode.fields.slug,
title:
previousNode.frontmatter.displayTitle || previousNode.frontmatter.title,
};
}
let nextNodeData = null;
const nextNode =
index === edges.length - 1 ? undefined : edges[index + 1].node;
if (nextNode && nextNode.fields.categoryName === categoryName) {
nextNodeData = {
slug: nextNode.fields.slug,
title: nextNode.frontmatter.displayTitle || nextNode.frontmatter.title,
};
}
return {
previousNodeData,
nextNodeData,
};
}
function mapToNavigationData(page) {
return {
title: page.displayTitle || page.title,
slug: page.slug,
};
}
function iterateEdges(edges, extraProperties = () => ({})) {
function updateMarkdownPage({ node }, index) {
const {
fields: { slug, categoryName },
parent: { relativePath },
frontmatter: { title, displayTitle },
fileAbsolutePath,
} = node;
const { nextNodeData, previousNodeData } = createPreviousNextNodeData(
edges,
index,
categoryName
);
const result = {
slug,
title,
categoryName,
relativePath,
next: nextNodeData,
previous: previousNodeData,
displayTitle: displayTitle || '',
realPath: fileAbsolutePath || '',
id: node.id,
};
return { ...result, ...extraProperties(node) };
}
return edges.map(updateMarkdownPage);
}
module.exports = {
createPreviousNextNodeData,
iterateEdges,
mapToNavigationData,
};