-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
page_loader.ts
53 lines (43 loc) 路 1.08 KB
/
page_loader.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
import { Page } from "./filesystem.ts";
import type { Format, Reader } from "../core.ts";
export interface Options {
/** The reader instance used to read the files */
reader: Reader;
}
/**
* Class to load page files that generate assets (css, js, etc).
*/
export default class PageLoader {
/** The filesystem reader */
reader: Reader;
constructor(options: Options) {
this.reader = options.reader;
}
/** Load an asset Page */
async load(
path: string,
format: Format,
): Promise<Page | undefined> {
if (!format.pageLoader) {
return;
}
const info = await this.reader.getInfo(path);
if (!info) {
return;
}
const { ext, asset } = format;
// Create the page
const page = new Page({
path: path.slice(0, -ext.length),
lastModified: info?.mtime || undefined,
created: info?.birthtime || undefined,
remote: info.remote,
ext,
asset,
});
// Load the data
const data = await this.reader.read(path, format.pageLoader);
Object.assign(page.baseData, data);
return page;
}
}