@risinghq/folio 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +45 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +123 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +147 -0
- package/dist/highlight.d.ts +2 -0
- package/dist/highlight.js +82 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/layout.d.ts +39 -0
- package/dist/layout.js +203 -0
- package/dist/manual.d.ts +11 -0
- package/dist/manual.js +23 -0
- package/dist/markdown.d.ts +43 -0
- package/dist/markdown.js +144 -0
- package/dist/og.d.ts +19 -0
- package/dist/og.js +94 -0
- package/dist/options.d.ts +67 -0
- package/dist/options.js +50 -0
- package/dist/plugin.d.ts +20 -0
- package/dist/plugin.js +255 -0
- package/dist/site.d.ts +14 -0
- package/dist/site.js +53 -0
- package/package.json +74 -4
- package/theme/base.css +66 -0
- package/theme/components/code-block.css +53 -0
- package/theme/components/doc.css +61 -0
- package/theme/components/footer.css +18 -0
- package/theme/components/header.css +89 -0
- package/theme/components/home.css +14 -0
- package/theme/components/not-found.css +13 -0
- package/theme/components/pager.css +39 -0
- package/theme/components/prose.css +104 -0
- package/theme/components/sidebar.css +173 -0
- package/theme/components/toc.css +102 -0
- package/theme/controls.css +110 -0
- package/theme/fonts.css +24 -0
- package/theme/index.css +14 -0
- package/theme/tokens.css +83 -0
package/dist/layout.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { escapeHtml } from "./markdown.js";
|
|
2
|
+
export function routesFor(base, url) {
|
|
3
|
+
const root = base.endsWith("/") ? base : `${base}/`;
|
|
4
|
+
return {
|
|
5
|
+
home: root,
|
|
6
|
+
page: (slug) => `${root}docs/${slug}`,
|
|
7
|
+
file: (path) => `${root}docs/${path}`,
|
|
8
|
+
card: (slug) => slug ? `${root}og/docs/${slug}.png` : `${root}og/index.png`,
|
|
9
|
+
absolute: (path) => (url ? new URL(path, url).href : undefined),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** The image a page shares, as a full URL, or none. */
|
|
13
|
+
function shareImage(options, routes, slug) {
|
|
14
|
+
const image = options.ogImage;
|
|
15
|
+
if (image === false)
|
|
16
|
+
return undefined;
|
|
17
|
+
if (image === undefined)
|
|
18
|
+
return routes.absolute(routes.card(slug));
|
|
19
|
+
if (/^https?:\/\//.test(image))
|
|
20
|
+
return image;
|
|
21
|
+
return routes.absolute(`${routes.home}${image.replace(/^\//, "")}`);
|
|
22
|
+
}
|
|
23
|
+
function socialTags(shell) {
|
|
24
|
+
const social = shell.social;
|
|
25
|
+
if (!social)
|
|
26
|
+
return `<meta name="robots" content="noindex">`;
|
|
27
|
+
const property = (name, content) => content ? `<meta property="${name}" content="${escapeHtml(content)}">` : "";
|
|
28
|
+
return [
|
|
29
|
+
social.url ? `<link rel="canonical" href="${escapeHtml(social.url)}">` : "",
|
|
30
|
+
property("og:type", "website"),
|
|
31
|
+
property("og:site_name", social.site),
|
|
32
|
+
property("og:title", social.title),
|
|
33
|
+
property("og:description", shell.description),
|
|
34
|
+
property("og:url", social.url),
|
|
35
|
+
property("og:image", social.image),
|
|
36
|
+
social.image
|
|
37
|
+
? `<meta name="twitter:card" content="summary_large_image">`
|
|
38
|
+
: "",
|
|
39
|
+
]
|
|
40
|
+
.filter(Boolean)
|
|
41
|
+
.join("\n");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs before first paint, so the saved theme never flashes. It is inline
|
|
45
|
+
* because a module script runs after the page has painted.
|
|
46
|
+
*/
|
|
47
|
+
const themeScript = `<script>try{var t=localStorage.getItem("folio-theme");document.documentElement.dataset.theme=t==="light"||t==="dark"?t:matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"}catch(e){document.documentElement.dataset.theme="light"}</script>`;
|
|
48
|
+
function document(shell, assets) {
|
|
49
|
+
return `<!doctype html>
|
|
50
|
+
<html lang="en">
|
|
51
|
+
<head>
|
|
52
|
+
<meta charset="utf-8">
|
|
53
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
54
|
+
<title>${escapeHtml(shell.title)}</title>
|
|
55
|
+
${shell.description ? `<meta name="description" content="${escapeHtml(shell.description)}">` : ""}
|
|
56
|
+
${socialTags(shell)}
|
|
57
|
+
${themeScript}
|
|
58
|
+
${assets.head}
|
|
59
|
+
</head>
|
|
60
|
+
<body>
|
|
61
|
+
${shell.body}
|
|
62
|
+
</body>
|
|
63
|
+
</html>
|
|
64
|
+
`;
|
|
65
|
+
}
|
|
66
|
+
const pageNumber = (index) => String(index + 1).padStart(2, "0");
|
|
67
|
+
function header(options, routes, pages, inManual) {
|
|
68
|
+
const manual = `<a href="${routes.page(pages[0]?.slug ?? "")}"${inManual ? ' aria-current="true"' : ""}>Manual</a>`;
|
|
69
|
+
const links = [
|
|
70
|
+
...options.nav,
|
|
71
|
+
...(options.repository
|
|
72
|
+
? [{ text: "GitHub", href: options.repository }]
|
|
73
|
+
: []),
|
|
74
|
+
]
|
|
75
|
+
.map((link) => {
|
|
76
|
+
const external = /^https?:/.test(link.href) ? ' rel="noreferrer"' : "";
|
|
77
|
+
return `<a href="${escapeHtml(link.href)}"${external}>${escapeHtml(link.text)}</a>`;
|
|
78
|
+
})
|
|
79
|
+
.join("");
|
|
80
|
+
const version = options.version
|
|
81
|
+
? `<span class="version">${escapeHtml(options.version)}</span>`
|
|
82
|
+
: "";
|
|
83
|
+
return `<a class="skip" href="#content">Skip to content</a>
|
|
84
|
+
<header class="site-header">
|
|
85
|
+
<a class="brand" href="${routes.home}"><span class="brand-mark" aria-hidden="true"></span>${escapeHtml(options.title)}</a>${version}
|
|
86
|
+
<nav class="site-nav" aria-label="Site">${manual}${links}</nav>
|
|
87
|
+
<button class="theme-toggle" type="button" aria-label="Switch between light and dark"></button>
|
|
88
|
+
</header>`;
|
|
89
|
+
}
|
|
90
|
+
function sidebar(pages, current, routes) {
|
|
91
|
+
const items = pages
|
|
92
|
+
.map((page, index) => {
|
|
93
|
+
const currentAttr = page.slug === current ? ' aria-current="page"' : "";
|
|
94
|
+
return `<li><a href="${routes.page(page.slug)}"${currentAttr}><span class="num">${pageNumber(index)}</span><span>${escapeHtml(page.title)}</span></a></li>`;
|
|
95
|
+
})
|
|
96
|
+
.join("");
|
|
97
|
+
return `<nav class="sidebar" id="sidebar" aria-label="Manual">
|
|
98
|
+
<div class="sidebar-head"><span>Manual</span><span>${pages.length}</span><button class="menu-close" type="button" aria-label="Close the manual's pages"></button></div>
|
|
99
|
+
<ol>${items}</ol>
|
|
100
|
+
</nav>
|
|
101
|
+
<div class="scrim" aria-hidden="true"></div>`;
|
|
102
|
+
}
|
|
103
|
+
function headingList(page) {
|
|
104
|
+
return page.headings
|
|
105
|
+
.map((heading) => `<li data-depth="${heading.depth}"><a href="#${heading.id}">${escapeHtml(heading.title)}</a></li>`)
|
|
106
|
+
.join("");
|
|
107
|
+
}
|
|
108
|
+
function rail(page) {
|
|
109
|
+
const toc = page.headings.length
|
|
110
|
+
? `<nav class="toc" aria-label="On this page"><p>On this page</p><ul>${headingList(page)}</ul></nav>`
|
|
111
|
+
: "";
|
|
112
|
+
return `<aside class="rail">${toc}<a class="to-top" href="#content">Back to top</a></aside>`;
|
|
113
|
+
}
|
|
114
|
+
function inlineToc(page) {
|
|
115
|
+
return page.headings.length
|
|
116
|
+
? `<details class="toc-inline"><summary>On this page</summary><ul>${headingList(page)}</ul></details>`
|
|
117
|
+
: "";
|
|
118
|
+
}
|
|
119
|
+
function pager(pages, index, routes) {
|
|
120
|
+
const link = (offset, rel, label) => {
|
|
121
|
+
const page = pages[index + offset];
|
|
122
|
+
if (!page)
|
|
123
|
+
return "";
|
|
124
|
+
const number = pageNumber(index + offset);
|
|
125
|
+
const caption = rel === "prev" ? `← ${label} · ${number}` : `${number} · ${label} →`;
|
|
126
|
+
return `<a href="${routes.page(page.slug)}" rel="${rel}"><span>${caption}</span>${escapeHtml(page.title)}</a>`;
|
|
127
|
+
};
|
|
128
|
+
return `<nav class="pager" aria-label="Pages">${link(-1, "prev", "Previous")}${link(1, "next", "Next")}</nav>`;
|
|
129
|
+
}
|
|
130
|
+
function footer(options) {
|
|
131
|
+
return options.footer
|
|
132
|
+
? `<footer class="site-footer">${options.footer}</footer>`
|
|
133
|
+
: "";
|
|
134
|
+
}
|
|
135
|
+
export function manualPage(input) {
|
|
136
|
+
const { options, routes, pages, slug, page } = input;
|
|
137
|
+
const edit = options.repository
|
|
138
|
+
? `<a href="${escapeHtml(`${options.repository}/edit/${options.branch}/${options.docsPath}/${slug}.md`)}" rel="noreferrer">Edit this page on GitHub</a>`
|
|
139
|
+
: "";
|
|
140
|
+
const index = pages.findIndex((entry) => entry.slug === slug);
|
|
141
|
+
const number = pageNumber(index);
|
|
142
|
+
const first = routes.page(pages[0]?.slug ?? "");
|
|
143
|
+
const body = `${header(options, routes, pages, true)}
|
|
144
|
+
<div class="menu-bar"><button class="menu-button" type="button" aria-controls="sidebar" aria-expanded="false"><span class="menu-icon" aria-hidden="true"></span>Manual</button><span class="menu-crumb">/ ${number} ${escapeHtml(page.title)}</span></div>
|
|
145
|
+
<div class="doc-layout">
|
|
146
|
+
${sidebar(pages, slug, routes)}
|
|
147
|
+
<main id="content" class="doc">
|
|
148
|
+
<p class="crumb"><a href="${first}">Manual</a> / ${number}</p>
|
|
149
|
+
${inlineToc(page)}
|
|
150
|
+
<h1>${escapeHtml(page.title)}</h1>
|
|
151
|
+
<article class="prose">
|
|
152
|
+
${page.html}
|
|
153
|
+
</article>
|
|
154
|
+
<footer class="doc-footer">${pager(pages, index, routes)}${edit}</footer>
|
|
155
|
+
</main>
|
|
156
|
+
${rail(page)}
|
|
157
|
+
</div>
|
|
158
|
+
${footer(options)}`;
|
|
159
|
+
return document({
|
|
160
|
+
title: `${page.title} · ${options.title}`,
|
|
161
|
+
description: page.summary,
|
|
162
|
+
body,
|
|
163
|
+
social: {
|
|
164
|
+
site: options.title,
|
|
165
|
+
title: page.title,
|
|
166
|
+
url: routes.absolute(routes.page(slug)),
|
|
167
|
+
image: shareImage(options, routes, slug),
|
|
168
|
+
},
|
|
169
|
+
}, input.assets);
|
|
170
|
+
}
|
|
171
|
+
/** The home page: the docs folder's `index.md`, or a link to the manual. */
|
|
172
|
+
export function homePage(input) {
|
|
173
|
+
const { options, routes, pages, home } = input;
|
|
174
|
+
const first = routes.page(pages[0]?.slug ?? "");
|
|
175
|
+
const main = home
|
|
176
|
+
? `<main id="content" class="home"><h1>${escapeHtml(home.title || options.title)}</h1><div class="prose">
|
|
177
|
+
${home.html}</div></main>`
|
|
178
|
+
: `<main id="content" class="not-found"><h1>${escapeHtml(options.title)}</h1><p><a href="${first}">Read the manual</a></p></main>`;
|
|
179
|
+
const body = `${header(options, routes, pages, false)}
|
|
180
|
+
${main}
|
|
181
|
+
${footer(options)}`;
|
|
182
|
+
return document({
|
|
183
|
+
title: options.title,
|
|
184
|
+
description: options.description || home?.summary || "",
|
|
185
|
+
body,
|
|
186
|
+
social: {
|
|
187
|
+
site: options.title,
|
|
188
|
+
title: options.title,
|
|
189
|
+
url: routes.absolute(routes.home),
|
|
190
|
+
image: shareImage(options, routes),
|
|
191
|
+
},
|
|
192
|
+
}, input.assets);
|
|
193
|
+
}
|
|
194
|
+
export function notFoundPage(input) {
|
|
195
|
+
const { options, routes, pages } = input;
|
|
196
|
+
const items = pages
|
|
197
|
+
.map((page) => `<li><a href="${routes.page(page.slug)}">${escapeHtml(page.title)}</a></li>`)
|
|
198
|
+
.join("");
|
|
199
|
+
const body = `${header(options, routes, pages, false)}
|
|
200
|
+
<main id="content" class="not-found"><h1>Page not found</h1><p>These pages exist:</p><ul>${items}</ul></main>
|
|
201
|
+
${footer(options)}`;
|
|
202
|
+
return document({ title: `Page not found · ${options.title}`, description: "", body }, input.assets);
|
|
203
|
+
}
|
package/dist/manual.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A page of the manual. `slug` names the file in the docs folder and the route. */
|
|
2
|
+
export interface PageEntry {
|
|
3
|
+
slug: string;
|
|
4
|
+
title: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The pages a docs index lists, in its order. Each item of the index's first
|
|
8
|
+
* list links to `<slug>.md`, and the link text is the page title. A page the
|
|
9
|
+
* index does not list is not part of the site.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readManual(index: string, source?: string): PageEntry[];
|
package/dist/manual.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Marked } from "marked";
|
|
2
|
+
/**
|
|
3
|
+
* The pages a docs index lists, in its order. Each item of the index's first
|
|
4
|
+
* list links to `<slug>.md`, and the link text is the page title. A page the
|
|
5
|
+
* index does not list is not part of the site.
|
|
6
|
+
*/
|
|
7
|
+
export function readManual(index, source = "docs/README.md") {
|
|
8
|
+
const list = new Marked()
|
|
9
|
+
.lexer(index)
|
|
10
|
+
.find((token) => token.type === "list");
|
|
11
|
+
if (!list)
|
|
12
|
+
throw new Error(`${source} has no list of pages`);
|
|
13
|
+
return list.items.map((item) => {
|
|
14
|
+
const link = item.tokens
|
|
15
|
+
.flatMap((token) => (token.type === "text" ? (token.tokens ?? []) : []))
|
|
16
|
+
.find((token) => token.type === "link");
|
|
17
|
+
const slug = /^([a-z0-9-]+)\.md$/.exec(link?.href ?? "")?.[1];
|
|
18
|
+
if (!link || !slug) {
|
|
19
|
+
throw new Error(`${source}: "${item.text}" does not link to a page`);
|
|
20
|
+
}
|
|
21
|
+
return { slug, title: link.text.replaceAll("`", "") };
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PageEntry } from "./manual.ts";
|
|
2
|
+
export interface Heading {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
depth: number;
|
|
6
|
+
}
|
|
7
|
+
export interface RenderedPage {
|
|
8
|
+
/** The page's `#` heading, as plain text. */
|
|
9
|
+
title: string;
|
|
10
|
+
/** The page's first paragraph, as plain text: its meta description. */
|
|
11
|
+
summary: string;
|
|
12
|
+
/** The article body, without the `#` heading. */
|
|
13
|
+
html: string;
|
|
14
|
+
/** The `##` and `###` headings, in order. */
|
|
15
|
+
headings: Heading[];
|
|
16
|
+
/** The island names the page mounts, in order. */
|
|
17
|
+
islands: string[];
|
|
18
|
+
/** The docs folder's files the page's images show, relative to the folder. */
|
|
19
|
+
files: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface RenderContext {
|
|
22
|
+
pages: PageEntry[];
|
|
23
|
+
/** The route of a manual page. */
|
|
24
|
+
pageUrl: (slug: string) => string;
|
|
25
|
+
/** Where a `.md` link that is not a manual page points, such as GitHub. */
|
|
26
|
+
sourceUrl: (file: string) => string;
|
|
27
|
+
/** The URL the site serves a file of the docs folder at. */
|
|
28
|
+
fileUrl: (file: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* The pixel size of a docs folder image. The `<img>` carries it so the page
|
|
31
|
+
* keeps the image's space before it loads.
|
|
32
|
+
*/
|
|
33
|
+
imageSize?: (file: string) => {
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
};
|
|
37
|
+
/** Renders a paragraph of nothing but links as a row of buttons. */
|
|
38
|
+
actions?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** An image path inside the docs folder, such as `shots/home.png`. */
|
|
41
|
+
export declare const docsFile: RegExp;
|
|
42
|
+
export declare function escapeHtml(text: string): string;
|
|
43
|
+
export declare function renderPage(markdown: string, context: RenderContext): RenderedPage;
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Marked } from "marked";
|
|
2
|
+
import { highlight } from "./highlight.js";
|
|
3
|
+
/** An image path inside the docs folder, such as `shots/home.png`. */
|
|
4
|
+
export const docsFile = /^(?:\.\/)?((?:[\w-]+\/)*[\w-]+\.(?:png|jpe?g|gif|svg|webp|avif))$/;
|
|
5
|
+
/** `<!-- island NAME -->` on its own line places a mounted component. */
|
|
6
|
+
const islandComment = /^<!--\s*island\s+([a-z0-9-]+)\s*-->\s*$/;
|
|
7
|
+
export function escapeHtml(text) {
|
|
8
|
+
return text
|
|
9
|
+
.replaceAll("&", "&")
|
|
10
|
+
.replaceAll("<", "<")
|
|
11
|
+
.replaceAll(">", ">")
|
|
12
|
+
.replaceAll('"', """);
|
|
13
|
+
}
|
|
14
|
+
function slugify(text) {
|
|
15
|
+
return text
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.replaceAll("`", "")
|
|
18
|
+
.replace(/[^\p{L}\p{N}\s-]/gu, "")
|
|
19
|
+
.trim()
|
|
20
|
+
.replace(/\s+/g, "-");
|
|
21
|
+
}
|
|
22
|
+
function plain(text) {
|
|
23
|
+
return text
|
|
24
|
+
.replaceAll("`", "")
|
|
25
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
26
|
+
.replace(/\s+/g, " ")
|
|
27
|
+
.trim();
|
|
28
|
+
}
|
|
29
|
+
/** A link to another `.md` page becomes its route; a link to any other file stays. */
|
|
30
|
+
function rewriteHref(href, context) {
|
|
31
|
+
const match = /^(?:\.\/)?([a-z0-9-]+)\.md(#.*)?$/.exec(href);
|
|
32
|
+
if (!match)
|
|
33
|
+
return href;
|
|
34
|
+
const [, name = "", hash = ""] = match;
|
|
35
|
+
return context.pages.some((page) => page.slug === name)
|
|
36
|
+
? `${context.pageUrl(name)}${hash}`
|
|
37
|
+
: `${context.sourceUrl(`${name}.md`)}${hash}`;
|
|
38
|
+
}
|
|
39
|
+
export function renderPage(markdown, context) {
|
|
40
|
+
const ids = new WeakMap();
|
|
41
|
+
const headings = [];
|
|
42
|
+
const islands = [];
|
|
43
|
+
const files = [];
|
|
44
|
+
let title = "";
|
|
45
|
+
let summary = "";
|
|
46
|
+
const marked = new Marked({
|
|
47
|
+
renderer: {
|
|
48
|
+
code({ text, lang }) {
|
|
49
|
+
const language = (lang ?? "text").split(/\s/)[0] || "text";
|
|
50
|
+
return `<figure class="code"><figcaption><span>${escapeHtml(language)}</span><button class="copy" type="button">Copy</button></figcaption>${highlight(text, language)}</figure>\n`;
|
|
51
|
+
},
|
|
52
|
+
html({ text }) {
|
|
53
|
+
const island = islandComment.exec(text.trim())?.[1];
|
|
54
|
+
if (!island)
|
|
55
|
+
return text;
|
|
56
|
+
islands.push(island);
|
|
57
|
+
return `<div class="island" data-island="${island}"></div>\n`;
|
|
58
|
+
},
|
|
59
|
+
link({ href, title: linkTitle, tokens }) {
|
|
60
|
+
const target = rewriteHref(href, context);
|
|
61
|
+
const external = /^https?:/.test(target);
|
|
62
|
+
const attrs = [
|
|
63
|
+
`href="${escapeHtml(target)}"`,
|
|
64
|
+
linkTitle ? `title="${escapeHtml(linkTitle)}"` : "",
|
|
65
|
+
external ? 'rel="noreferrer"' : "",
|
|
66
|
+
]
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.join(" ");
|
|
69
|
+
return `<a ${attrs}>${this.parser.parseInline(tokens)}</a>`;
|
|
70
|
+
},
|
|
71
|
+
image({ href, title: imageTitle, text }) {
|
|
72
|
+
const file = docsFile.exec(href)?.[1];
|
|
73
|
+
if (file && !files.includes(file))
|
|
74
|
+
files.push(file);
|
|
75
|
+
const size = file ? context.imageSize?.(file) : undefined;
|
|
76
|
+
const attrs = [
|
|
77
|
+
`src="${escapeHtml(file ? context.fileUrl(file) : href)}"`,
|
|
78
|
+
`alt="${escapeHtml(text)}"`,
|
|
79
|
+
imageTitle ? `title="${escapeHtml(imageTitle)}"` : "",
|
|
80
|
+
size ? `width="${size.width}" height="${size.height}"` : "",
|
|
81
|
+
'loading="lazy"',
|
|
82
|
+
]
|
|
83
|
+
.filter(Boolean)
|
|
84
|
+
.join(" ");
|
|
85
|
+
return `<img ${attrs}>`;
|
|
86
|
+
},
|
|
87
|
+
paragraph({ tokens }) {
|
|
88
|
+
const inner = this.parser.parseInline(tokens);
|
|
89
|
+
const links = tokens.filter((token) => token.type === "link");
|
|
90
|
+
const onlyLinks = links.length > 0 &&
|
|
91
|
+
tokens.every((token) => token.type === "link" ||
|
|
92
|
+
(token.type === "text" && token.raw.trim() === ""));
|
|
93
|
+
if (!context.actions || !onlyLinks)
|
|
94
|
+
return `<p>${inner}</p>\n`;
|
|
95
|
+
const buttons = links
|
|
96
|
+
.map((token, index) => this.parser
|
|
97
|
+
.parseInline([token])
|
|
98
|
+
.replace("<a ", `<a class="btn ${index === 0 ? "btn-primary" : "btn-ghost"}" `))
|
|
99
|
+
.join("");
|
|
100
|
+
return `<p class="actions">${buttons}</p>\n`;
|
|
101
|
+
},
|
|
102
|
+
table(token) {
|
|
103
|
+
const cell = (entry, tag) => {
|
|
104
|
+
const align = entry.align ? ` style="text-align:${entry.align}"` : "";
|
|
105
|
+
return `<${tag}${align}>${this.parser.parseInline(entry.tokens)}</${tag}>`;
|
|
106
|
+
};
|
|
107
|
+
const head = token.header.map((entry) => cell(entry, "th")).join("");
|
|
108
|
+
const body = token.rows
|
|
109
|
+
.map((row) => `<tr>${row.map((entry) => cell(entry, "td")).join("")}</tr>`)
|
|
110
|
+
.join("");
|
|
111
|
+
return `<div class="table"><table><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table></div>\n`;
|
|
112
|
+
},
|
|
113
|
+
heading(token) {
|
|
114
|
+
if (token.depth === 1)
|
|
115
|
+
return "";
|
|
116
|
+
const id = ids.get(token) ?? slugify(token.text);
|
|
117
|
+
const inner = this.parser.parseInline(token.tokens);
|
|
118
|
+
return `<h${token.depth} id="${id}"><a class="anchor" href="#${id}">${inner}</a></h${token.depth}>\n`;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
const tokens = marked.lexer(markdown);
|
|
123
|
+
const used = new Map();
|
|
124
|
+
for (const token of tokens) {
|
|
125
|
+
if (token.type === "paragraph" && !summary && title)
|
|
126
|
+
summary = plain(token.text);
|
|
127
|
+
if (token.type !== "heading")
|
|
128
|
+
continue;
|
|
129
|
+
const heading = token;
|
|
130
|
+
if (heading.depth === 1) {
|
|
131
|
+
title ||= plain(heading.text);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const base = slugify(heading.text);
|
|
135
|
+
const count = used.get(base) ?? 0;
|
|
136
|
+
used.set(base, count + 1);
|
|
137
|
+
const id = count === 0 ? base : `${base}-${count}`;
|
|
138
|
+
ids.set(heading, id);
|
|
139
|
+
if (heading.depth <= 3)
|
|
140
|
+
headings.push({ id, title: plain(heading.text), depth: heading.depth });
|
|
141
|
+
}
|
|
142
|
+
const html = marked.parser(tokens);
|
|
143
|
+
return { title, summary, html, headings, islands, files };
|
|
144
|
+
}
|
package/dist/og.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** What one page's social card shows. */
|
|
2
|
+
export interface Card {
|
|
3
|
+
/** The brand above the title. The home card leaves it out: its title is the site. */
|
|
4
|
+
site?: string;
|
|
5
|
+
title: string;
|
|
6
|
+
summary: string;
|
|
7
|
+
/** The site's address without its scheme, in the card's footer. */
|
|
8
|
+
host?: string;
|
|
9
|
+
}
|
|
10
|
+
type Palette = Record<"bg" | "line" | "text" | "muted" | "faint" | "accent", string>;
|
|
11
|
+
/** The dark theme's colours: the first `:root` block of theme/tokens.css. */
|
|
12
|
+
export declare function darkPalette(tokens: string): Palette;
|
|
13
|
+
export declare const cardSize: {
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
};
|
|
17
|
+
/** A page's social card as a PNG, 1200 by 630. */
|
|
18
|
+
export declare function renderCard(card: Card): Promise<Uint8Array>;
|
|
19
|
+
export {};
|
package/dist/og.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { Resvg } from "@resvg/resvg-js";
|
|
4
|
+
import satori from "satori";
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const div = (style, children) => ({
|
|
7
|
+
type: "div",
|
|
8
|
+
props: { style, children },
|
|
9
|
+
});
|
|
10
|
+
/** The dark theme's colours: the first `:root` block of theme/tokens.css. */
|
|
11
|
+
export function darkPalette(tokens) {
|
|
12
|
+
const block = /:root\s*\{([^}]*)\}/.exec(tokens)?.[1] ?? "";
|
|
13
|
+
const values = Object.fromEntries([...block.matchAll(/--([\w-]+):\s*([^;]+);/g)].map((m) => [m[1], m[2]]));
|
|
14
|
+
const names = ["bg", "line", "text", "muted", "faint", "accent"];
|
|
15
|
+
for (const name of names)
|
|
16
|
+
if (!values[name])
|
|
17
|
+
throw new Error(`folio: tokens.css has no --${name}`);
|
|
18
|
+
return values;
|
|
19
|
+
}
|
|
20
|
+
/** Cuts text to `max` characters at a word boundary. */
|
|
21
|
+
function clip(text, max) {
|
|
22
|
+
if (text.length <= max)
|
|
23
|
+
return text;
|
|
24
|
+
const cut = text.slice(0, max).replace(/\s+\S*$/, "");
|
|
25
|
+
return `${cut}…`;
|
|
26
|
+
}
|
|
27
|
+
export const cardSize = { width: 1200, height: 630 };
|
|
28
|
+
const inset = { x: 80, y: 72 };
|
|
29
|
+
function layout(card, color) {
|
|
30
|
+
const brand = div({ display: "flex", alignItems: "center", gap: 20 }, [
|
|
31
|
+
div({
|
|
32
|
+
width: 36,
|
|
33
|
+
height: 36,
|
|
34
|
+
borderRadius: 9,
|
|
35
|
+
backgroundColor: color.accent,
|
|
36
|
+
}),
|
|
37
|
+
card.site
|
|
38
|
+
? div({ fontSize: 34, fontWeight: 600, color: color.text }, card.site)
|
|
39
|
+
: null,
|
|
40
|
+
]);
|
|
41
|
+
const body = div({ display: "flex", flexDirection: "column", gap: 28, width: "100%" }, [
|
|
42
|
+
div({
|
|
43
|
+
fontSize: card.title.length > 32 ? 68 : 84,
|
|
44
|
+
fontWeight: 700,
|
|
45
|
+
lineHeight: 1.05,
|
|
46
|
+
letterSpacing: "-0.02em",
|
|
47
|
+
color: color.text,
|
|
48
|
+
}, clip(card.title, 60)),
|
|
49
|
+
card.summary
|
|
50
|
+
? div({ fontSize: 32, lineHeight: 1.4, color: color.muted }, clip(card.summary, 140))
|
|
51
|
+
: null,
|
|
52
|
+
]);
|
|
53
|
+
const foot = div({
|
|
54
|
+
display: "flex",
|
|
55
|
+
paddingTop: 28,
|
|
56
|
+
borderTop: `2px solid ${color.line}`,
|
|
57
|
+
fontSize: 26,
|
|
58
|
+
color: color.faint,
|
|
59
|
+
}, card.host ?? "");
|
|
60
|
+
const frame = div({
|
|
61
|
+
width: cardSize.width - 2 * inset.x,
|
|
62
|
+
height: cardSize.height - 2 * inset.y,
|
|
63
|
+
margin: `${inset.y}px ${inset.x}px`,
|
|
64
|
+
display: "flex",
|
|
65
|
+
flexDirection: "column",
|
|
66
|
+
justifyContent: "space-between",
|
|
67
|
+
}, [brand, body, foot]);
|
|
68
|
+
return div({
|
|
69
|
+
...cardSize,
|
|
70
|
+
display: "flex",
|
|
71
|
+
backgroundColor: color.bg,
|
|
72
|
+
fontFamily: "Figtree",
|
|
73
|
+
}, [frame]);
|
|
74
|
+
}
|
|
75
|
+
let fonts;
|
|
76
|
+
let palette;
|
|
77
|
+
function loadPalette() {
|
|
78
|
+
const tokens = new URL("../theme/tokens.css", import.meta.url);
|
|
79
|
+
palette ??= readFile(tokens, "utf8").then(darkPalette);
|
|
80
|
+
return palette;
|
|
81
|
+
}
|
|
82
|
+
function loadFonts() {
|
|
83
|
+
const face = (weight) => readFile(require.resolve(`@fontsource/figtree/files/figtree-latin-${weight}-normal.woff`)).then((data) => ({ name: "Figtree", data, weight }));
|
|
84
|
+
fonts ??= Promise.all([face(400), face(600), face(700)]);
|
|
85
|
+
return fonts;
|
|
86
|
+
}
|
|
87
|
+
/** A page's social card as a PNG, 1200 by 630. */
|
|
88
|
+
export async function renderCard(card) {
|
|
89
|
+
const svg = await satori(layout(card, await loadPalette()), {
|
|
90
|
+
...cardSize,
|
|
91
|
+
fonts: await loadFonts(),
|
|
92
|
+
});
|
|
93
|
+
return new Resvg(svg).render().asPng();
|
|
94
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** A link in the site header. */
|
|
2
|
+
export interface NavLink {
|
|
3
|
+
text: string;
|
|
4
|
+
href: string;
|
|
5
|
+
}
|
|
6
|
+
export interface FolioOptions {
|
|
7
|
+
/** The project name: the header brand and the suffix of every page title. */
|
|
8
|
+
title?: string;
|
|
9
|
+
/** The version shown beside the brand. */
|
|
10
|
+
version?: string;
|
|
11
|
+
/** The home page's meta description. */
|
|
12
|
+
description?: string;
|
|
13
|
+
/** The manual's folder, relative to the Vite root. It holds `README.md`. */
|
|
14
|
+
docs?: string;
|
|
15
|
+
/** The repository's web address. Adds the GitHub link and "Edit this page". */
|
|
16
|
+
repository?: string;
|
|
17
|
+
/** The branch "Edit this page" opens. */
|
|
18
|
+
branch?: string;
|
|
19
|
+
/** The docs folder's path inside the repository. */
|
|
20
|
+
docsPath?: string;
|
|
21
|
+
/**
|
|
22
|
+
* A module, relative to the Vite root, whose default export maps an island
|
|
23
|
+
* name to a loader: `{ "hover-tap": () => import("./demos/hover-tap") }`.
|
|
24
|
+
*/
|
|
25
|
+
islands?: string;
|
|
26
|
+
/** Extra header links, after "Manual". */
|
|
27
|
+
nav?: NavLink[];
|
|
28
|
+
/** A footer line, as HTML. */
|
|
29
|
+
footer?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The site's public address, such as `https://example.com`. Enables the
|
|
32
|
+
* canonical link, absolute Open Graph URLs, social cards and the sitemap.
|
|
33
|
+
*/
|
|
34
|
+
url?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The social card every page shares: a URL, or a path under the site's base.
|
|
37
|
+
* Unset, folio draws one card per page. `false` shares none.
|
|
38
|
+
*/
|
|
39
|
+
ogImage?: string | false;
|
|
40
|
+
}
|
|
41
|
+
/** The fields of the nearest `package.json` that supply defaults. */
|
|
42
|
+
export interface PackageInfo {
|
|
43
|
+
name?: string;
|
|
44
|
+
version?: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
homepage?: string;
|
|
47
|
+
repository?: string | {
|
|
48
|
+
url?: string;
|
|
49
|
+
};
|
|
50
|
+
/** folio's options, for a site that has no Vite config of its own. */
|
|
51
|
+
folio?: FolioOptions;
|
|
52
|
+
}
|
|
53
|
+
type Optional = "repository" | "islands" | "version" | "url" | "ogImage";
|
|
54
|
+
export type ResolvedOptions = Required<Omit<FolioOptions, Optional>> & Pick<FolioOptions, Optional>;
|
|
55
|
+
/** A `repository` field as a web address, from `o/r`, `github:o/r` or a git URL. */
|
|
56
|
+
export declare function repositoryUrl(field: PackageInfo["repository"]): string | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* A `homepage` field as the site's address. `npm init` points `homepage` at
|
|
59
|
+
* the repository's README, which is not the site, so a GitHub address is none.
|
|
60
|
+
*/
|
|
61
|
+
export declare function siteUrl(homepage: string | undefined): string | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Fills each option the caller left out from `package.json`'s `folio` field,
|
|
64
|
+
* then from its package fields, then from folio's defaults.
|
|
65
|
+
*/
|
|
66
|
+
export declare function resolveOptions(options: FolioOptions, pkg?: PackageInfo): ResolvedOptions;
|
|
67
|
+
export {};
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** A `repository` field as a web address, from `o/r`, `github:o/r` or a git URL. */
|
|
2
|
+
export function repositoryUrl(field) {
|
|
3
|
+
const raw = typeof field === "string" ? field : field?.url;
|
|
4
|
+
if (!raw)
|
|
5
|
+
return undefined;
|
|
6
|
+
const shorthand = /^(?:github:)?([\w.-]+\/[\w.-]+)$/.exec(raw);
|
|
7
|
+
if (shorthand)
|
|
8
|
+
return `https://github.com/${shorthand[1]}`;
|
|
9
|
+
return raw
|
|
10
|
+
.replace(/^git\+/, "")
|
|
11
|
+
.replace(/^git:\/\//, "https://")
|
|
12
|
+
.replace(/^ssh:\/\/git@/, "https://")
|
|
13
|
+
.replace(/^git@([^:]+):/, "https://$1/")
|
|
14
|
+
.replace(/\.git$/, "");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A `homepage` field as the site's address. `npm init` points `homepage` at
|
|
18
|
+
* the repository's README, which is not the site, so a GitHub address is none.
|
|
19
|
+
*/
|
|
20
|
+
export function siteUrl(homepage) {
|
|
21
|
+
if (!homepage)
|
|
22
|
+
return undefined;
|
|
23
|
+
const parsed = URL.parse(homepage);
|
|
24
|
+
if (!parsed || parsed.hostname === "github.com")
|
|
25
|
+
return undefined;
|
|
26
|
+
return homepage;
|
|
27
|
+
}
|
|
28
|
+
function defined(options = {}) {
|
|
29
|
+
return Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Fills each option the caller left out from `package.json`'s `folio` field,
|
|
33
|
+
* then from its package fields, then from folio's defaults.
|
|
34
|
+
*/
|
|
35
|
+
export function resolveOptions(options, pkg = {}) {
|
|
36
|
+
return {
|
|
37
|
+
title: pkg.name?.replace(/^@[^/]+\//, "") ?? "Docs",
|
|
38
|
+
version: pkg.version,
|
|
39
|
+
description: pkg.description ?? "",
|
|
40
|
+
repository: repositoryUrl(pkg.repository),
|
|
41
|
+
url: siteUrl(pkg.homepage),
|
|
42
|
+
docs: "docs",
|
|
43
|
+
branch: "master",
|
|
44
|
+
docsPath: "docs",
|
|
45
|
+
nav: [],
|
|
46
|
+
footer: "",
|
|
47
|
+
...defined(pkg.folio),
|
|
48
|
+
...defined(options),
|
|
49
|
+
};
|
|
50
|
+
}
|