-
Notifications
You must be signed in to change notification settings - Fork 6
/
entries.ts
65 lines (55 loc) · 1.61 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
import { common, join, walk, WalkEntry, withoutTrailingSlash } from "./deps.ts";
import { INDEX_FILENAME, RE_HIDDEN_OR_UNDERSCORED } from "./constants.ts";
export async function getStaticEntries(opts: {
path: string;
exts?: Array<string>;
}): Promise<Array<WalkEntry>> {
const entries: Array<WalkEntry> = [];
for await (
const entry of walk(opts.path, {
includeDirs: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
exts: opts.exts,
})
) {
entries.push(entry);
}
return entries;
}
export async function getContentEntries(opts: {
path: string;
}): Promise<Array<WalkEntry>> {
const fileEntries: Array<WalkEntry> = [];
let dirEntries: Array<WalkEntry> = [];
for await (
const entry of walk(opts.path, {
includeDirs: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
exts: ["md"],
})
) {
fileEntries.push(entry);
}
for await (
const entry of walk(opts.path, {
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));
});
return [...fileEntries, ...dirEntries];
}