-
Notifications
You must be signed in to change notification settings - Fork 6
/
entries.ts
109 lines (92 loc) · 2.49 KB
/
entries.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
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
import { common, join } from "./deps.ts";
import { expandGlob, walk, WalkEntry } from "./deps.ts";
import { withoutTrailingSlash } from "./deps.ts";
export const INDEX_FILENAME = "index.md";
export const RE_HIDDEN_OR_UNDERSCORED = /^\.|^_|\/\.|\/\_/;
const hasIgnoredPrefix = (path: string): boolean => {
const pathChunks = path.split("/");
for (const chunk of pathChunks) {
if (/^\./.test(chunk) || /^\_/.test(chunk)) {
return true;
}
}
return false;
};
export async function getStaticEntries(
staticPath: string,
outputPath: string,
extensions?: Array<string>,
): Promise<Array<WalkEntry>> {
const entries: Array<WalkEntry> = [];
let glob = "**/*";
if (extensions && extensions.length > 0) {
glob = `**/*.{${extensions.join(",")}}`;
}
for await (
const entry of expandGlob(glob, {
root: staticPath,
includeDirs: false,
caseInsensitive: true,
exclude: [outputPath],
})
) {
if (hasIgnoredPrefix(entry.path)) {
continue;
}
entries.push(entry);
}
return entries;
}
export async function getAssetEntries(
assetPath: string,
): Promise<Array<WalkEntry>> {
const entries: Array<WalkEntry> = [];
// const glob = "**/*";
for await (
const entry of walk(assetPath, {
includeDirs: false,
followSymlinks: true,
})
) {
entries.push(entry);
}
return entries;
}
export async function getContentEntries(
contentPath: string,
): Promise<Array<WalkEntry>> {
const fileEntries: Array<WalkEntry> = [];
let dirEntries: Array<WalkEntry> = [];
for await (
const entry of walk(contentPath, {
includeDirs: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
exts: ["md"],
})
) {
fileEntries.push(entry);
}
for await (
const entry of walk(contentPath, {
includeDirs: true,
includeFiles: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
})
) {
dirEntries.push(entry);
}
const filePaths = fileEntries.map((file) => file.path);
// filter out dirs that are already in fileEntries as "index.md"
dirEntries = dirEntries.filter((dir) => {
return !filePaths.includes(join(dir.path, INDEX_FILENAME));
});
// filter out dirs that don't have any fileEntries
dirEntries = dirEntries.filter((dir) => {
const commonPaths = fileEntries.map((file) =>
withoutTrailingSlash(common([dir.path, file.path]))
);
return commonPaths.includes(withoutTrailingSlash(dir.path));
});
const entries = [...fileEntries, ...dirEntries];
return entries;
}