-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgraph.js
More file actions
114 lines (95 loc) · 2.44 KB
/
Copy pathgraph.js
File metadata and controls
114 lines (95 loc) · 2.44 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { gql } from "graphql-tag";
import {
filterContentByCollection,
filterContentByRoute,
} from "@greenwood/cli/src/lib/content-utils.js";
const getCollection = async (root, { name, orderBy }, context) => {
const { graph } = context;
const content = filterContentByCollection(graph, name);
let items = [];
content.forEach((page) => {
const { data, label, title } = page;
const { tableOfContents, tocHeading } = data;
const toc = getParsedHeadingsFromPage(tableOfContents, tocHeading);
items.push({ ...page, title: title || label, tableOfContents: toc });
});
if (orderBy) {
return sortCollection(items, orderBy);
}
return items;
};
const sortCollection = (collection, orderBy) => {
const compare = (a, b) => {
if (orderBy === "title_asc" || orderBy === "title_desc") {
((a = a?.title), (b = b?.title));
}
if (orderBy === "order_asc" || orderBy === "order_desc") {
((a = a?.data.order), (b = b?.data?.order));
}
if (orderBy === "title_asc" || orderBy === "order_asc") {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
} else if (orderBy === "title_desc" || orderBy === "order_desc") {
if (a > b) {
return -1;
}
if (a < b) {
return 1;
}
}
return 0;
};
return collection.sort(compare);
};
const getParsedHeadingsFromPage = (tableOfContents = []) => {
let children = [];
tableOfContents.forEach(({ content, slug }) => {
children.push({ label: content, route: "#" + slug });
});
return children;
};
const getPagesFromGraph = async (root, query, context) => {
return context.graph;
};
const getChildrenFromParentRoute = async (root, query, context) => {
const { parent } = query;
return filterContentByRoute(context.graph, parent);
};
const graphTypeDefs = gql`
type TocItem {
label: String
route: String
}
type Page {
id: String
label: String
title: String
route: String
layout: String
data: Data
tableOfContents: [TocItem]
}
enum CollectionOrderBy {
title_asc
title_desc
order_asc
order_desc
}
type Query {
graph: [Page]
collection(name: String!, orderBy: CollectionOrderBy): [Page]
children(parent: String!): [Page]
}
`;
const graphResolvers = {
Query: {
graph: getPagesFromGraph,
collection: getCollection,
children: getChildrenFromParentRoute,
},
};
export { graphTypeDefs, graphResolvers };