-
Notifications
You must be signed in to change notification settings - Fork 81
/
processDocInfos.ts
69 lines (67 loc) · 1.93 KB
/
processDocInfos.ts
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
import path from "path";
import {
DocInfoWithFilePath,
DocInfoWithRoute,
ProcessedPluginOptions,
PostBuildData,
} from "../../shared/interfaces";
export function processDocInfos(
{ routesPaths, outDir, baseUrl }: PostBuildData,
{
indexDocs,
indexBlog,
indexPages,
docsRouteBasePath,
blogRouteBasePath,
}: ProcessedPluginOptions
): DocInfoWithFilePath[] {
return routesPaths
.map<DocInfoWithRoute | undefined>((url: string) => {
// istanbul ignore next
if (!url.startsWith(baseUrl)) {
throw new Error(
`The route must start with the baseUrl "${baseUrl}", but was "${url}". This is a bug, please report it.`
);
}
const route = url.substr(baseUrl.length);
if (route === "404.html" || route === "search/index.html") {
// Do not index error page and search page.
return;
}
if (
indexBlog &&
blogRouteBasePath.some((basePath) => urlMatchesPrefix(route, basePath))
) {
if (
blogRouteBasePath.some(
(basePath) =>
route === basePath || urlMatchesPrefix(route, `${basePath}/tags`)
)
) {
// Do not index list of blog posts and tags filter pages
return;
}
return { route, url, type: "blog" };
}
if (
indexDocs &&
docsRouteBasePath.some((basePath) => urlMatchesPrefix(route, basePath))
) {
return { route, url, type: "docs" };
}
if (indexPages) {
return { route, url, type: "page" };
}
return;
})
.filter<DocInfoWithRoute>(Boolean as any)
.map(({ route, url, type }) => ({
filePath: path.join(outDir, route, "index.html"),
url,
type,
}));
}
function urlMatchesPrefix(url: string, prefix: string) {
const rightTrimmedPrefix = prefix.replace(/\/$/, "");
return url === rightTrimmedPrefix || url.startsWith(`${rightTrimmedPrefix}/`);
}