This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathcreateDocsPages.js
119 lines (111 loc) · 3 KB
/
createDocsPages.js
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
115
116
117
118
119
const path = require("path");
const { defaultLocale } = require("./i18n");
const {
buildRoute,
buildPathFromFile,
normalizeRoute,
REFERENCE_ROOT,
DOCS_ROOT,
} = require("./routes");
const isReference = (doc) => {
const pathSegments = doc.relativeDirectory.split("/");
const parentDirectory = pathSegments[0];
return parentDirectory.includes(REFERENCE_ROOT);
};
const createDocsPages = ({ actions, docs }) => {
// TODO: These pages don't support internationalization. This should be
// refactored so that it can use createI18nPages.
const docTemplate = path.resolve(".", "src", "templates", "Documentation.js");
const apiTemplate = path.resolve(".", "src", "templates", "ApiReference.js");
const apiSeoTemplate = path.resolve(
".",
"src",
"templates",
"SingleApiReference.js",
);
const allDocs = docs.map(({ node }) => node);
const apiReference = allDocs.filter((doc) => isReference(doc));
const documentation = allDocs.filter((doc) => !isReference(doc));
documentation.forEach((doc) => {
const docPath = buildRoute(
defaultLocale,
buildPathFromFile(doc.relativePath),
);
actions.createPage({
path: docPath,
component: docTemplate,
context: {
mdxId: doc.childMdx.id,
urlPath: docPath,
locale: defaultLocale,
name: doc.name,
lastModified: doc.modifiedTime,
relativeDirectory: doc.relativeDirectory,
rootDir: DOCS_ROOT,
// None of these have translations set up. If we translate them in
// the future, we'll have to revisit this.
},
});
});
const apiRefRoute = buildRoute(defaultLocale, "api");
const apiRefDocIds = apiReference.map(({ childMdx }) => childMdx.id);
actions.createPage({
path: apiRefRoute,
matchPath: normalizeRoute(`${apiRefRoute}/*`),
component: apiTemplate,
context: {
urlPath: apiRefRoute,
ids: apiRefDocIds,
locale: defaultLocale,
},
});
apiReference.forEach((ref) => {
const refPath = buildRoute(
defaultLocale,
"no-js",
ref.relativeDirectory === DOCS_ROOT
? "/docs/"
: buildPathFromFile(ref.relativePath),
);
actions.createPage({
path: refPath,
component: apiSeoTemplate,
context: {
urlPath: refPath,
docId: ref.childMdx.id,
ids: apiRefDocIds,
lastModified: ref.modifiedTime,
locale: defaultLocale,
},
});
});
};
const queryFragment = `
docs: allFile(filter: {
sourceInstanceName: { eq: "docs" },
extension: { eq: "mdx" }
}) {
edges {
node {
childMdx {
id
}
modifiedTime(formatString: "YYYY-MM-DD")
name
relativePath
relativeDirectory
fields {
metadata {
data {
order
title
}
}
}
}
}
}
`;
exports.createDocsPages = createDocsPages;
exports.queryFragment = queryFragment;
exports.buildPathFromFile = buildPathFromFile;