-
Notifications
You must be signed in to change notification settings - Fork 6
/
entries.ts
68 lines (57 loc) · 1.64 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
import {
INDEX_FILENAME,
RE_DATE_PREFIX,
RE_HIDDEN_OR_UNDERSCORED,
} from "./constants.ts";
import { path, fs } from "./deps/std.ts";
import { withoutTrailingSlash } from "./deps/ufo.ts";
export const getStaticEntries = async (opts: {
path: string;
exts?: string[];
}): Promise<fs.WalkEntry[]> => {
const entries: fs.WalkEntry[] = [];
for await (const entry of fs.walk(opts.path, {
includeDirs: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
exts: opts.exts,
})) {
entries.push(entry);
}
return entries;
};
export const getContentEntries = async (opts: {
path: string;
}): Promise<fs.WalkEntry[]> => {
const fileEntries: fs.WalkEntry[] = [];
const dirEntries: fs.WalkEntry[] = [];
for await (const entry of fs.walk(opts.path, {
includeDirs: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
exts: ["md"],
})) {
fileEntries.push(entry);
}
const filePaths = fileEntries.map((file) => file.path);
for await (const entry of fs.walk(opts.path, {
includeDirs: true,
includeFiles: false,
skip: [RE_HIDDEN_OR_UNDERSCORED],
})) {
const commonPaths = fileEntries.map((file) =>
withoutTrailingSlash(path.common([entry.path, file.path]))
);
// skip dirs that are already in fileEntries as "[2021-01-01-]index.md"
if (
filePaths.some(
(filepath) =>
filepath.replace(RE_DATE_PREFIX, "") ===
path.join(entry.path, INDEX_FILENAME)
)
)
continue;
// skip dirs that don't have any fileEntries
if (!commonPaths.includes(withoutTrailingSlash(entry.path))) continue;
dirEntries.push(entry);
}
return [...fileEntries, ...dirEntries];
};