-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
162 lines (145 loc) · 4.56 KB
/
mod.ts
File metadata and controls
162 lines (145 loc) · 4.56 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { merge } from "lume/core/utils/object.ts";
import Site from "lume/core/site.ts";
import { Page } from "lume/core/file.ts";
import { concurrent } from "lume/core/utils/concurrent.ts";
import { sha1 } from "https://deno.land/x/sha1@v1.0.3/mod.ts";
interface Options {
/**
* The extensions of the files to process.
* @default [".html"]
*/
extensions?: string[];
/**
* A function that returns true if the URL should be cached.
* @default (url: string) => url.startsWith("https://") && [".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp"].some((ext) => url.endsWith(ext)),
*/
shouldCache?: (url: string) => boolean;
/**
* Transforms the URL into a hashed version for local storage use.
* @param url The URL to transform.
* @returns A file-system-compatible string.
* @default (url: string) => sha1(url)
*/
transform?: (url: string) => string;
/**
* The folder where the images will be cached.
* @default "cache"
*/
folder?: string;
/**
* Whether to log the output or not.
* @default true
*/
logOutput?: boolean;
}
export const defaults: Options = {
extensions: [".html"],
shouldCache: (url: string) =>
url.startsWith("https://") &&
[".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp"].some((ext) =>
url.endsWith(ext)
),
folder: "cache",
transform: (url: string) => sha1(url, "utf-8", "hex").toString(),
logOutput: true,
};
/**
* Plugin that allows you to cache remote content locally.
*
* Most of this function is repurposed from https://github.com/lumeland/lume/blob/2e739f07806bfdf3a3c4c49ea28e8129ee3f61a9/plugins/modify_urls.ts
*/
export default function cacheContent(userOptions?: Options) {
const options = merge(defaults, userOptions);
const generated = new Set<string>();
const encoder = new TextEncoder();
async function replace(
site: Site,
url: string | null,
): Promise<string> {
if (!url) {
return "";
}
if (!options.shouldCache!(url)) {
return url;
}
const hash = options.transform!(url);
const folder = options.folder!.replace(/\/$/, "");
const extension = url.split(".").pop()!;
const path = `/${folder}/${hash}.${extension}`;
// Don't download the same file twice
if (!generated.has(path)) {
generated.add(path);
Deno.writeSync(Deno.stdout.rid, encoder.encode(`Caching ${url} in ${path}\r`));
const res = await fetch(url);
const page = Page.create({
url: path,
content: new Uint8Array(await res.arrayBuffer()),
});
site.pages.push(page);
}
return path;
}
async function replaceSrcset(
site: Site,
attr: string | null,
): Promise<string> {
const srcset = attr ? attr.trim().split(",") : [];
const replaced: string[] = [];
for (const src of srcset) {
const [, url, rest] = src.trim().match(/^(\S+)(.*)/)!;
replaced.push(await replace(site, url) + rest);
}
return replaced.join(", ");
}
return (site: Site) => {
site.process(
options.extensions,
async (pages) => {
await concurrent(pages, async (page: Page) => {
const { document } = page;
if (!document) {
return;
}
for (const element of document.querySelectorAll("[href]")) {
element.setAttribute(
"href",
await replace(site, element.getAttribute("href")),
);
}
for (const element of document.querySelectorAll("[src]")) {
element.setAttribute(
"src",
await replace(site, element.getAttribute("src")),
);
}
for (const element of document.querySelectorAll("video[poster]")) {
element.setAttribute(
"poster",
await replace(site, element.getAttribute("poster")),
);
}
for (const element of document.querySelectorAll("[srcset]")) {
element.setAttribute(
"srcset",
await replaceSrcset(
site,
element.getAttribute("srcset"),
),
);
}
for (const element of document.querySelectorAll("[imagesrcset]")) {
element.setAttribute(
"imagesrcset",
await replaceSrcset(
site,
element.getAttribute("imagesrcset"),
),
);
}
});
const { columns } = Deno.consoleSize();
Deno.writeSync(Deno.stdout.rid, encoder.encode(" ".repeat(columns) + "\r"));
},
);
};
}