-
Notifications
You must be signed in to change notification settings - Fork 6
/
render.tsx
85 lines (78 loc) · 2.44 KB
/
render.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/** @jsxImportSource npm:preact */
import { HIGHLIGHT_STYLE, HMR_CLIENT } from "./constants.ts";
import { render } from "./deps/preact.ts";
import { inline, twindSetup } from "./deps/twind.ts";
import { twindConfig } from "./twind.config.ts";
import { Crumb, Page, UserConfig } from "./types.d.ts";
import Body from "./components/Body.tsx";
import { sortPages, sortTaggedPages } from "./pages.ts";
const tw = twindSetup(twindConfig);
interface RenderOpts {
page: Page;
crumbs: Crumb[];
dev?: boolean;
childPages?: Page[];
backlinkPages?: Page[];
relatedPages?: Page[];
pagesByTag?: Record<string, Page[]>;
userConfig: UserConfig;
}
export function renderPage({
page,
crumbs,
dev,
childPages,
backlinkPages,
relatedPages,
pagesByTag,
userConfig,
}: RenderOpts): string {
const body = render(
<Body
page={page}
crumbs={crumbs}
childPages={childPages && sortPages(childPages)}
backlinkPages={backlinkPages && sortPages(backlinkPages)}
relatedPages={relatedPages && sortPages(relatedPages)}
pagesByTag={pagesByTag && sortTaggedPages(pagesByTag)}
lang={userConfig.lang}
author={{
name: userConfig.authorName,
email: userConfig.authorEmail,
url: userConfig.authorUrl,
}}
/>
);
const pageTitle =
page.title === userConfig.title
? page.title
: `${page.title} · ${userConfig.title}`;
const pageDescription = `${page.description || userConfig.description}`;
return inline(
`<!doctype html>
<html lang="${userConfig.lang || "en"}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${pageTitle}</title>
<meta name="description" content="${pageDescription}">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="${pageTitle}">
<meta name="twitter:description" content="${pageDescription}">
<meta property="og:type" content="article">
<meta property="og:title" content="${pageTitle}">
<meta property="og:description" content="$${pageDescription}">
<meta property="og:url" content="${page.url}">
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<link rel="alternate" type="application/atom+xml" href="/feed.xml" title="${
userConfig.title
}" />
${userConfig.head || ""}
${userConfig.codeHighlight ? `<style>${HIGHLIGHT_STYLE}</style>` : ""}
</head>
${dev ? `<script>${HMR_CLIENT}</script>` : ""}
${body}
</html>`,
tw
);
}