generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.ts
220 lines (201 loc) Β· 6.01 KB
/
render.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { tag as h } from "./deps.ts";
import { Marked } from "./marked.ts";
import { Config, NavLink, PageMeta } from "./mod.ts";
import { aTag, getH1, toTitle } from "./utils.ts";
import { PageLink } from "./mod.ts";
export function genNavbar(links: Array<NavLink>): string {
return h(
"ul",
...links.map(({ path, title, items }) => {
path ||= "#";
title ||= toTitle(path);
return items
? h("li", h("span", title), genNavbar(items))
: h("li", aTag({ href: path }, title));
}),
);
}
export function prismJs(path: string, integrity: string) {
let tagName = "link";
const cdnHost = "https://cdn.jsdelivr.net/npm/[email protected]/";
const attr: Record<string, string | boolean> = {
crossorigin: "anonymous",
integrity,
};
if (path.endsWith("css")) {
attr.rel = "stylesheet";
attr.href = `${cdnHost}${path}`;
} else {
tagName = "script";
attr.src = `${cdnHost}${path}`;
attr.defer = true;
}
return h(tagName, attr);
}
export function getPageType(pageUrl: string) {
return /^https?:\/\/[^\/]+\/?$/.test(pageUrl) ? "website" : "article";
}
export function genToc(tocLevels: Array<number>, content: string) {
const levels = tocLevels.join("").replace(/[^1-6]/g, "");
if (!levels) {
return "";
}
const regex = new RegExp(
`<h([${levels}]) [^>]*id="([^"]+)"[^>]*>(.*?)<\/h[${levels}]>`,
"g",
);
let minLevel = 6;
const tocMd = (content.match(regex) || []).map((matched) => {
const [levelStr, id] = (matched.replace(regex, "$1 $2") || "").split(" ");
const level = Number(levelStr);
if (level < minLevel) {
minLevel = level;
}
const text = matched.replace(/<[^>]*>/g, "");
return `${" ".repeat(level)}- [${text}](#${id})`;
}).map((str) => str.slice(minLevel * 2)).join("\n");
const tocHtml = Marked.parse(tocMd).content;
return tocHtml
? h(
"details",
{ id: "table-of-contents" },
h("summary", "Table of contents"),
tocHtml.replace(/\n/g, ""),
)
: "";
}
export function genPageLink(
page?: PageLink,
attributes?: Record<string, string | number | boolean>,
) {
return page ? aTag({ ...attributes, href: page.path }, page.title) : "";
}
export function processTitle(title: string, siteName: string) {
return title && title != siteName ? title + " | " + siteName : siteName;
}
export function genHead({
content,
pageUrl,
siteName,
description,
favicon,
image,
twitter,
removeDefaultStyles = false,
bottomHead = "",
title = "",
}: {
content: string;
pageUrl: string;
siteName: string;
description: string;
favicon: string;
image: string;
twitter: string;
removeDefaultStyles: boolean;
bottomHead: string;
title?: string;
}): string {
const viewport = "width=device-width,initial-scale=1.0,minimum-scale=1.0";
const pageTitle = processTitle(title || getH1(content), siteName);
const style = "#table-of-contents{margin:2rem;margin-bottom:0;}" +
"#neighbors{display:flex;margin-bottom:1rem}" +
"#neighbors>#prev,#neighbors>#next{display:block;width:50%}" +
"#neighbors>#next{margin-left:auto}" +
"#neighbors>#prev::before{content:'Β« '}#neighbors>#next::after{content:' Β»'}" +
".feather{width:.8rem;height:.8rem;stroke:var(--text-color);stroke-width:2;" +
"stroke-linecap:round;stroke-linejoin:round;fill:none;" +
"display:inline-block;margin:0 .05rem 0 .15rem;vertical-align:-.1em;}";
return h(
"head",
h("meta", { charset: "UTF-8" }),
h("title", pageTitle),
h("meta", { name: "viewport", content: viewport }),
h("meta", { name: "description", content: description }),
h("link", { rel: "icon", href: favicon }),
h("meta", { property: "og:url", content: pageUrl }),
h("meta", { property: "og:type", content: getPageType(pageUrl) }),
h("meta", { property: "og:title", content: pageTitle }),
h("meta", { property: "og:description", content: description }),
h("meta", { property: "og:site_name", content: siteName }),
h("meta", { property: "og:image", content: image }),
h("meta", { name: "twitter:card", content: "summary" }),
h("meta", { name: "twitter:site", content: twitter }),
removeDefaultStyles ? "" : h("link", {
rel: "stylesheet",
href: "https://cdn.jsdelivr.net/npm/[email protected]",
}) + prismJs(
"themes/prism-tomorrow.css",
"sha256-0dkohC9ZEupqWbq0hS5cVR4QQXJ+mp6N2oJyuks6gt0=",
) + h("style", style),
bottomHead,
);
}
export function genNeighbors(
{ prev, next }: { prev?: PageLink; next?: PageLink },
) {
return prev || next
? h(
"div",
{ id: "neighbors" },
genPageLink(prev, { id: "prev" }),
genPageLink(next, { id: "next" }),
)
: "";
}
export function genBody({
content,
siteName,
navLinks = [],
removeDefaultStyles = false,
bottomBody = "",
tocLevels = [],
prev,
next,
}: {
content: string;
siteName: string;
navLinks: Array<NavLink>;
removeDefaultStyles: boolean;
bottomBody: string;
tocLevels: Array<number>;
prev?: PageLink;
next?: PageLink;
}): string {
const href = "https://github.com/kawarimidoll/deno-diplodocus";
return h(
"body",
h("header", h("h1", aTag({ href: "/" }, siteName))),
h("nav", { id: "header-nav" }, genNavbar(navLinks)),
genToc(tocLevels, content),
h("main", content),
h(
"footer",
genNeighbors({ prev, next }),
h("div", "Powered by ", aTag({ href }, "Diplodocus")),
),
removeDefaultStyles ? "" : prismJs(
"components/prism-core.min.js",
"sha256-dz05jjFU9qYuMvQQlE6iWDtNAnEsmu6uMb1vWhKdkEM=",
) + prismJs(
"plugins/autoloader/prism-autoloader.min.js",
"sha256-sttoa+EIAvFFfeeIkmPn8ypyOOb6no2sZ2NbxtBXgqU=",
),
bottomBody,
);
}
export function renderPage({
content,
pageMeta,
siteMeta,
pageUrl,
}: {
content: string;
pageMeta: PageMeta;
siteMeta: Config;
pageUrl: string;
}): string {
const config = { content, pageUrl, ...siteMeta, ...pageMeta };
return "<!DOCTYPE html>" +
h("html", { lang: config.lang }, genHead(config), genBody(config));
}