generated from jackyzha0/quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.tsx
65 lines (60 loc) · 1.53 KB
/
resources.tsx
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 { randomUUID } from "crypto"
import { JSX } from "preact/jsx-runtime"
export type JSResource = {
loadTime: "beforeDOMReady" | "afterDOMReady"
moduleType?: "module"
spaPreserve?: boolean
} & (
| {
src: string
contentType: "external"
}
| {
script: string
contentType: "inline"
}
)
export type CSSResource = {
content: string
inline?: boolean
spaPreserve?: boolean
}
export function JSResourceToScriptElement(resource: JSResource, preserve?: boolean): JSX.Element {
const scriptType = resource.moduleType ?? "application/javascript"
const spaPreserve = preserve ?? resource.spaPreserve
if (resource.contentType === "external") {
return (
<script key={resource.src} src={resource.src} type={scriptType} spa-preserve={spaPreserve} />
)
} else {
const content = resource.script
return (
<script
key={randomUUID()}
type={scriptType}
spa-preserve={spaPreserve}
dangerouslySetInnerHTML={{ __html: content }}
></script>
)
}
}
export function CSSResourceToStyleElement(resource: CSSResource, preserve?: boolean): JSX.Element {
const spaPreserve = preserve ?? resource.spaPreserve
if (resource.inline ?? false) {
return <style>{resource.content}</style>
} else {
return (
<link
key={resource.content}
href={resource.content}
rel="stylesheet"
type="text/css"
spa-preserve={spaPreserve}
/>
)
}
}
export interface StaticResources {
css: CSSResource[]
js: JSResource[]
}