generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
render_html.ts
171 lines (155 loc) · 5.08 KB
/
render_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
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
163
164
165
166
167
168
169
170
171
import { AMP, GT, LT, mapValues, QUOT, tag as h } from "./deps.ts";
import { getDenoteCss } from "./css_functions.ts";
import { ConfigObject, ListGroup, ListItem } from "./types.ts";
export const DENOTE_LOGO =
"https://raw.githubusercontent.com/kawarimidoll/denote/main/logo.svg";
export function sanitize(str?: string): string {
return (str || "")
.replaceAll("&", AMP)
.replaceAll("<", LT)
.replaceAll(">", GT)
.replaceAll(`"`, QUOT);
}
export function loadConfig(config: Partial<ConfigObject>): ConfigObject {
config.name ||= "Your name will be here";
if (!config.list || Object.keys(config.list).length === 0) {
throw new Error("list is empty");
}
const name = sanitize(config.name);
const description = sanitize(config.description);
const image = encodeURI(config.image || "");
const favicon = encodeURI(config.favicon || DENOTE_LOGO);
const twitter = config.twitter?.replace(/^([^@])/, "@$1");
const list = mapValues(
config.list,
(group: ListGroup) => ({
icon: group.icon,
items: group.items.map((item) => ({
icon: item.icon,
text: sanitize(item.text),
link: item.link ? encodeURI(item.link) : "",
})),
}),
);
return {
name,
description,
image,
favicon,
twitter,
disable: (config.disable || []),
list,
} as ConfigObject;
}
export function icongram(name: string, size = 20, attrs = {}) {
const regex =
/^(clarity|devicon|entypo|feather|fontawesome|jam|material|octicons|simple)\/[\w-]+$/;
if (!regex.test(name)) {
throw new Error(
`invalid icon name: ${name}, icon name should be matched with ${regex}`,
);
}
return h("img", {
src: `https://icongr.am/${name}.svg?size=${size}&color=f0ffff`,
alt: name,
...attrs,
});
}
const exLink = icongram("feather/external-link", 12, { class: "ex-link" });
export function renderListItem(listItem: ListItem) {
const { icon, text, link: href } = listItem;
const iconText = (icon = "", text = "") =>
h(
"div",
{ class: "list-item" },
icon ? icongram(icon) : "",
text ? h("div", text) : "",
);
return href
? h("a", { href }, iconText(icon, (text || href) + exLink))
: iconText(icon, text);
}
const rainCount = 30;
export function renderHtmlHead(config: ConfigObject) {
const { name, description, disable, image, favicon, twitter } = config;
const url = `https://denote.deno.dev/${name}`;
const viewport = "width=device-width,initial-scale=1.0";
const title = `${name} | Denote`;
const noRound = disable.includes("rounded-image");
return h(
"head",
{ prefix: "og:http://ogp.me/ns#" },
h("meta", { charset: "utf-8" }),
h("meta", { name: "viewport", content: viewport }),
h("meta", { name: "description", content: description }),
h("meta", { property: "og:title", content: title }),
h("meta", { property: "og:description", content: description }),
h("meta", { property: "og:url", content: url }),
h("meta", { property: "og:type", content: "profile" }),
h("meta", { property: "og:site_name", content: "Denote" }),
image ? h("meta", { property: "og:image", content: image }) : "",
h("meta", { name: "twitter:card", content: "summary" }),
twitter ? h("meta", { name: "twitter:site", content: twitter }) : "",
h("title", title),
h("style", getDenoteCss(rainCount, noRound)),
h("link", { rel: "icon", href: favicon }),
h("link", { rel: "canonical", href: url }),
);
}
export function renderHtmlBody(config: ConfigObject) {
const { name, description, disable, image } = config;
const list = Object.entries(config.list);
const alt = `${name} main image`;
const noNav = disable.includes("nav");
const noRain = disable.includes("rain");
return h(
"body",
noRain ? "" : h(
"div",
{ class: "rain" },
h("div", { class: "drop" }).repeat(rainCount),
),
h(
"div",
{ id: "main" },
image ? h("img", { alt, class: "main-image", src: image }) : "",
h("h1", name),
description ? h("div", { class: "description" }, description) : "",
noNav ? "" : h("div", "Click to jump...") + h(
"div",
{ class: "nav-box" },
h(
"div",
{ class: "nav" },
...list.map(([id, { icon }]) =>
h("a", { href: `#${id}` }, icongram(icon, 26))
),
),
),
h(
"div",
{ class: "list-group" },
...list.map(([id, { icon, items }]) =>
h("h2", { id }, icongram(icon, 40)) +
items.map((listItem) => renderListItem(listItem)).join("")
),
),
h(
"footer",
h("a", { href: "https://github.com/kawarimidoll/denote" }, "Denote"),
exLink,
icongram("jam/heart-f", 18, { class: "inline" }),
h("a", { href: "https://deno.com/deploy" }, "Deno Deploy"),
exLink,
),
),
);
}
export function renderHtml(rawConfig: ConfigObject, debug = false) {
const config = loadConfig(rawConfig);
if (debug) {
console.log(config);
}
return "<!DOCTYPE html>" +
h("html", renderHtmlHead(config), renderHtmlBody(config));
}