forked from alephjs/aleph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.ts
65 lines (62 loc) · 1.98 KB
/
html.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 util from './util.ts'
export function createHtml({
lang = 'en',
head = [],
scripts = [],
body,
minify = false
}: {
lang?: string,
head?: string[],
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, nomodule?: boolean, async?: boolean, preload?: boolean })[],
body: string,
minify?: boolean
}) {
const eol = minify ? '' : '\n'
const indent = minify ? '' : ' '.repeat(4)
const headTags = head.map(tag => tag.trim())
.concat(scripts.map(v => {
if (!util.isString(v) && util.isNEString(v.src)) {
if (v.type === 'module') {
return `<link rel="modulepreload" href=${JSON.stringify(v.src)} />`
} else if (v.async === true) {
return `<link rel="preload" href=${JSON.stringify(v.src)} as="script" />`
}
}
return ''
})).filter(Boolean)
const scriptTags = scripts.map(v => {
if (util.isString(v)) {
return `<script>${v}</script>`
} else if (util.isNEString(v.innerText)) {
const { innerText, ...rest } = v
return `<script${attrString(rest)}>${eol}${innerText}${eol}${indent}</script>`
} else if (util.isNEString(v.src) && !v.preload) {
return `<script${attrString(v)}></script>`
} else {
return ''
}
}).filter(Boolean)
return [
'<!DOCTYPE html>',
`<html lang="${lang}">`,
'<head>',
indent + '<meta charSet="utf-8" />',
...headTags.map(tag => indent + tag),
'</head>',
'<body>',
indent + body,
...scriptTags.map(tag => indent + tag),
'</body>',
'</html>'
].join(eol)
}
function attrString(v: any): string {
return Object.keys(v).map(k => {
if (v[k] === true) {
return ` ${k}`
} else {
return ` ${k}=${JSON.stringify(String(v[k]))}`
}
}).join('')
}