-
Notifications
You must be signed in to change notification settings - Fork 307
/
utils.ts
47 lines (39 loc) · 1.24 KB
/
utils.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
import type { FileContent } from "static-browser-server";
export const insertHtmlAfterRegex = (
regex: RegExp,
content: string,
insertable: string
): string | void => {
const match = regex.exec(content);
if (match && match.length >= 1) {
const offset = match.index + match[0].length;
const prefix = content.substring(0, offset);
const suffix = content.substring(offset);
return prefix + insertable + suffix;
}
};
export const writeBuffer = (content: string | Uint8Array): Uint8Array => {
if (typeof content === "string") {
return new TextEncoder().encode(content);
} else {
return content;
}
};
export const readBuffer = (content: string | Uint8Array): string => {
if (typeof content === "string") {
return content;
} else {
return new TextDecoder().decode(content);
}
};
export const validateHtml = (content: FileContent): FileContent => {
// Make it a string
const contentString = readBuffer(content);
const domParser = new DOMParser();
const doc = domParser.parseFromString(contentString, "text/html");
if (!doc.documentElement.getAttribute("lang")) {
doc.documentElement.setAttribute("lang", "en");
}
const html = doc.documentElement.outerHTML;
return `<!DOCTYPE html>\n${html}`;
};