slidev-prerender 0.0.1-alpha.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yuma Ichimura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,184 @@
1
+ <h1 align="center">Slidev Prerender</h1>
2
+
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/slidev-prerender"><img src="https://img.shields.io/npm/v/slidev-prerender.svg" alt="npm version"></a>
5
+ <a href="https://www.npmjs.com/package/slidev-prerender"><img src="https://img.shields.io/npm/dm/slidev-prerender.svg" alt="npm downloads"></a>
6
+ <a href="https://github.com/slidev-prerender/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/slidev-prerender.svg" alt="license"></a>
7
+ </p>
8
+
9
+ <p align="center">
10
+ Pre-render your <a href="https://sli.dev">Slidev</a> presentations as Multi-Page Applications (MPA) with enhanced SEO and metadata support.
11
+ </p>
12
+
13
+ ## ✨ Features
14
+
15
+ - 🎯 **MPA Generation** - Convert your Slidev presentation into multiple HTML files (one per slide)
16
+ - πŸ” **SEO Optimization** - Customize meta tags, Open Graph, and Twitter Card for each slide
17
+ - ⚑ **Static Deployment** - Deploy to any static hosting service (Netlify, Vercel, GitHub Pages, etc.)
18
+ - 🎨 **Custom Metadata** - Set unique titles and SEO metadata for individual slides
19
+
20
+ ## πŸ“¦ Installation
21
+
22
+ ```bash
23
+ # npm
24
+ npm install -D slidev-prerender
25
+
26
+ # pnpm
27
+ pnpm add -D slidev-prerender
28
+
29
+ # yarn
30
+ yarn add -D slidev-prerender
31
+ ```
32
+
33
+ ## πŸš€ Quick Start
34
+
35
+ ### 1. Build your Slidev presentation
36
+
37
+ First, build your Slidev presentation:
38
+
39
+ ```bash
40
+ npx slidev build
41
+ ```
42
+
43
+ This will generate a `dist` folder with your Slidev build output.
44
+
45
+ ### 2. Run slidev-prerender
46
+
47
+ ```bash
48
+ npx slidev-prerender
49
+ ```
50
+
51
+ By default, this will:
52
+
53
+ - Read from `./dist` (your Slidev build output)
54
+ - Generate pre-rendered pages in `./dist-prerender`
55
+ - Create one HTML file per slide (1.html, 2.html, 3.html, etc.)
56
+
57
+ ## βš™οΈ Configuration
58
+
59
+ Create a `slidev-prerender.config.ts` file in your project root:
60
+
61
+ ```typescript
62
+ import { defineConfig } from "slidev-prerender";
63
+
64
+ export default defineConfig({
65
+ // Directory where Slidev built files are located
66
+ slidevDist: "./dist",
67
+
68
+ // Output directory for pre-rendered pages
69
+ outDir: "./dist-prerender",
70
+
71
+ // Port to use for the local server during rendering (default: 3030)
72
+ port: 3030,
73
+
74
+ // Configuration for individual pages
75
+ pages: [
76
+ {
77
+ id: "1",
78
+ meta: {
79
+ fileName: "Introduction - My Presentation",
80
+ seoMeta: {
81
+ ogTitle: "Welcome to My Presentation",
82
+ ogDescription: "An introduction to the main topics",
83
+ ogImage: "https://example.com/slide1.png",
84
+ twitterCard: "summary_large_image",
85
+ },
86
+ },
87
+ },
88
+ {
89
+ id: "2",
90
+ meta: {
91
+ fileName: "Key Concepts",
92
+ seoMeta: {
93
+ ogTitle: "Understanding the Key Concepts",
94
+ ogDescription: "Deep dive into the core ideas",
95
+ },
96
+ },
97
+ },
98
+ // Add more pages as needed
99
+ ],
100
+ });
101
+ ```
102
+
103
+ ### Configuration Options
104
+
105
+ #### `UserConfig`
106
+
107
+ | Option | Type | Default | Description |
108
+ | ------------ | --------------- | -------------------- | -------------------------------------------- |
109
+ | `slidevDist` | `string` | `"./dist"` | Path to your Slidev build output directory |
110
+ | `outDir` | `string` | `"./dist-prerender"` | Output directory for pre-rendered pages |
111
+ | `port` | `number` | `3030` | Port for the local server during rendering |
112
+ | `pages` | `PageConfig[]` | `[]` | Configuration for individual slides |
113
+
114
+ #### `PageConfig`
115
+
116
+ | Option | Type | Description |
117
+ | ------ | ------------------ | ------------------------------------------------- |
118
+ | `id` | `string` | Slide number or identifier (e.g., "1", "2", "3") |
119
+ | `meta` | `BuildHeadOptions` | Metadata configuration for the slide |
120
+
121
+ #### `BuildHeadOptions`
122
+
123
+ | Option | Type | Description |
124
+ | ---------- | --------- | ------------------------------------ |
125
+ | `title` | `string` | Page title (appears in browser tab) |
126
+ | `seoMeta` | `SEOMeta` | SEO and social media metadata |
127
+
128
+ #### `SEOMeta`
129
+
130
+ | Option | Type | Description |
131
+ | --------------------- | -------- | ------------------------------------------------- |
132
+ | `ogTitle` | `string` | Open Graph title |
133
+ | `ogDescription` | `string` | Open Graph description |
134
+ | `ogImage` | `string` | Open Graph image URL |
135
+ | `ogUrl` | `string` | Open Graph URL |
136
+ | `twitterCard` | `string` | Twitter Card type (e.g., "summary_large_image") |
137
+ | `twitterTitle` | `string` | Twitter title |
138
+ | `twitterDescription` | `string` | Twitter description |
139
+ | `twitterImage` | `string` | Twitter image URL |
140
+
141
+ ## 🌐 Deployment
142
+
143
+ Deploy the generated `dist-prerender` folder like any static site:
144
+
145
+ 1. Build and prerender:
146
+
147
+ ```bash
148
+ npx slidev build
149
+ npx slidev-prerender
150
+ ```
151
+
152
+ 2. Upload `dist-prerender` to your host (Netlify/Vercel static export, GitHub Pages, S3+CloudFront, etc.).
153
+ 3. Preview locally with any static server (e.g. `npx serve dist-prerender`).
154
+
155
+ For more details on hosting options, see the [Slidev Hosting Guide](https://sli.dev/guide/hosting#hosting).
156
+
157
+ ## πŸ€” Why Use This?
158
+
159
+ When you share a Slidev presentation link on social media (Twitter, Facebook, Slack, etc.), the preview often shows generic information instead of details about the specific slide. This happens because Slidev presentations are Single Page Applications (SPAs), which makes it difficult for social platforms to extract slide-specific metadata. `slidev-prerender` solves this by generating separate HTML files for each slide with custom Open Graph and Twitter Card metadata, ensuring that every slide shows rich, compelling previews when shared.
160
+
161
+ ## πŸ› οΈ How It Works
162
+
163
+ 1. Reads your Slidev build output
164
+ 2. Starts a local server with your presentation
165
+ 3. Uses Playwright to navigate to each slide
166
+ 4. Captures the rendered HTML and injects custom metadata
167
+ 5. Saves each slide as a separate HTML file
168
+ 6. Copies all assets (CSS, JS, images) to the output directory
169
+
170
+ ## πŸ“„ License
171
+
172
+ [MIT](LICENSE)
173
+
174
+ ## 🀝 Contributing
175
+
176
+ Contributions are welcome! Please feel free to submit a Pull Request.
177
+
178
+ ## πŸ’¬ Support
179
+
180
+ If you have any questions or issues, please [open an issue](https://github.com/slidev-prerender/issues) on GitHub.
181
+
182
+ ---
183
+
184
+ Made with ❀️ for the [Slidev](https://sli.dev) community
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+ import('../dist/run.mjs')
@@ -0,0 +1,31 @@
1
+ import { SeoMeta } from "@slidev/types";
2
+ import { ResolvableLink } from "unhead/types";
3
+
4
+ //#region src/build/handle-head.d.ts
5
+ type BuildHeadOptions = {
6
+ lang?: string;
7
+ title: string;
8
+ description?: string | null;
9
+ canonicalUrl?: string | null;
10
+ ogImage?: string | null;
11
+ twitterCard?: string | null;
12
+ favicon?: string | null;
13
+ webFonts?: ResolvableLink[];
14
+ seoMeta?: Partial<SeoMeta>;
15
+ };
16
+ //#endregion
17
+ //#region src/config/loadConfig.d.ts
18
+ type UserConfig = {
19
+ slidevDist?: string;
20
+ outDir?: string;
21
+ pages?: {
22
+ fileName: string;
23
+ meta?: BuildHeadOptions;
24
+ }[];
25
+ port?: number;
26
+ };
27
+ //#endregion
28
+ //#region src/config/defineConfig.d.ts
29
+ declare function defineConfig(config: UserConfig): UserConfig;
30
+ //#endregion
31
+ export { type UserConfig, defineConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ //#region src/config/defineConfig.ts
2
+ function defineConfig(config) {
3
+ return config;
4
+ }
5
+
6
+ //#endregion
7
+ export { defineConfig };
package/dist/run.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/run.mjs ADDED
@@ -0,0 +1,195 @@
1
+ import fs from "node:fs/promises";
2
+ import path, { resolve } from "node:path";
3
+ import { chromium } from "@playwright/test";
4
+ import http from "node:http";
5
+ import sirv from "sirv";
6
+ import { escapeHtml } from "markdown-it/lib/common/utils.mjs";
7
+ import { createHead, transformHtmlTemplate } from "unhead/server";
8
+ import { parseHtmlForUnheadExtraction } from "unhead/parser";
9
+ import { pathToFileURL } from "node:url";
10
+ import { existsSync } from "node:fs";
11
+
12
+ //#region src/build/handle-dist.ts
13
+ async function removeDist(outDir) {
14
+ await fs.rm(outDir, {
15
+ recursive: true,
16
+ force: true
17
+ });
18
+ await fs.mkdir(outDir, { recursive: true });
19
+ }
20
+ async function serveDist(distPath, port) {
21
+ const serve = sirv(distPath, {
22
+ etag: true,
23
+ single: true
24
+ });
25
+ const server = http.createServer((req, res) => serve(req, res));
26
+ await new Promise((resolve$1, reject) => {
27
+ server.once("error", reject);
28
+ server.listen(port, resolve$1);
29
+ });
30
+ return {
31
+ origin: `http://localhost:${port}`,
32
+ close: () => new Promise((resolve$1) => server.close(() => resolve$1()))
33
+ };
34
+ }
35
+ async function getPageHtml(page, pageUrl) {
36
+ await page.goto(pageUrl, { waitUntil: "networkidle" });
37
+ return "<!doctype html>\n" + await page.content();
38
+ }
39
+
40
+ //#endregion
41
+ //#region src/utils/file.ts
42
+ async function copyDir(src, dst) {
43
+ await fs.mkdir(dst, { recursive: true });
44
+ for (const e of await fs.readdir(src, { withFileTypes: true })) {
45
+ const s = path.join(src, e.name);
46
+ const d = path.join(dst, e.name);
47
+ e.isDirectory() ? await copyDir(s, d) : await fs.copyFile(s, d);
48
+ }
49
+ }
50
+
51
+ //#endregion
52
+ //#region src/build/handle-head.ts
53
+ function toAttrValue(unsafe) {
54
+ return JSON.stringify(escapeHtml(String(unsafe)));
55
+ }
56
+ async function applyHead(html, opt) {
57
+ const extracted = parseHtmlForUnheadExtraction(html).input;
58
+ const description = opt.description ? toAttrValue(opt.description) : null;
59
+ return await transformHtmlTemplate(createHead({ init: [extracted, {
60
+ htmlAttrs: opt.lang ? { lang: opt.lang } : void 0,
61
+ title: opt.title,
62
+ link: [
63
+ opt.favicon ? {
64
+ rel: "icon",
65
+ href: opt.favicon
66
+ } : null,
67
+ ...opt.webFonts ?? [],
68
+ opt.canonicalUrl ? {
69
+ rel: "canonical",
70
+ href: opt.canonicalUrl
71
+ } : null
72
+ ].filter(Boolean),
73
+ meta: [
74
+ {
75
+ name: "description",
76
+ content: description
77
+ },
78
+ {
79
+ property: "og:title",
80
+ content: opt.seoMeta?.ogTitle || opt.title
81
+ },
82
+ {
83
+ property: "og:description",
84
+ content: opt.seoMeta?.ogDescription || description
85
+ },
86
+ {
87
+ property: "og:image",
88
+ content: opt.ogImage ?? opt.seoMeta?.ogImage
89
+ },
90
+ {
91
+ property: "og:url",
92
+ content: opt.seoMeta?.ogUrl || opt.canonicalUrl
93
+ },
94
+ {
95
+ name: "twitter:card",
96
+ content: opt.twitterCard ?? opt.seoMeta?.twitterCard
97
+ },
98
+ {
99
+ name: "twitter:title",
100
+ content: opt.seoMeta?.twitterTitle || opt.title
101
+ },
102
+ {
103
+ name: "twitter:description",
104
+ content: opt.seoMeta?.twitterDescription || description
105
+ },
106
+ {
107
+ name: "twitter:image",
108
+ content: (opt.seoMeta?.twitterImage || opt.ogImage) ?? opt.seoMeta?.ogImage
109
+ },
110
+ {
111
+ name: "twitter:url",
112
+ content: opt.seoMeta?.twitterUrl || opt.canonicalUrl
113
+ }
114
+ ].filter((x) => x.content)
115
+ }] }), html);
116
+ }
117
+
118
+ //#endregion
119
+ //#region src/config/loadConfig.ts
120
+ const CONFIG_FILE_NAMES = [
121
+ "slidev-prerender.config.ts",
122
+ "slidev-prerender.config.mts",
123
+ "slidev-prerender.config.js",
124
+ "slidev-prerender.config.mjs"
125
+ ];
126
+ async function loadConfig(cwd = process.cwd()) {
127
+ for (const fileName of CONFIG_FILE_NAMES) {
128
+ const configPath = resolve(cwd, fileName);
129
+ if (existsSync(configPath)) try {
130
+ const configModule = await import(pathToFileURL(configPath).href);
131
+ return configModule.default || configModule;
132
+ } catch (error) {
133
+ throw new Error(`Failed to load config from ${configPath}: ${error}`);
134
+ }
135
+ }
136
+ return {};
137
+ }
138
+
139
+ //#endregion
140
+ //#region src/build/getConfig.ts
141
+ const DEFAULT_CONFIG = {
142
+ slidevDist: "dist",
143
+ outDir: "dist-prerender",
144
+ pages: [],
145
+ port: 4173
146
+ };
147
+ async function getConfig() {
148
+ const config = await loadConfig();
149
+ return {
150
+ slidevDist: config.slidevDist ?? DEFAULT_CONFIG.slidevDist,
151
+ outDir: config.outDir ?? DEFAULT_CONFIG.outDir,
152
+ pages: config.pages ?? DEFAULT_CONFIG.pages,
153
+ port: config.port ?? DEFAULT_CONFIG.port
154
+ };
155
+ }
156
+
157
+ //#endregion
158
+ //#region src/build/index.ts
159
+ async function run() {
160
+ const { slidevDist, outDir, pages, port } = await getConfig();
161
+ try {
162
+ await fs.access(slidevDist);
163
+ } catch {
164
+ throw new Error(`Slidev dist directory not found: ${slidevDist}\nPlease run 'slidev build' first.`);
165
+ }
166
+ await removeDist(outDir);
167
+ const assetsPath = path.join(slidevDist, "assets");
168
+ try {
169
+ await fs.access(assetsPath);
170
+ await copyDir(assetsPath, path.join(outDir, "assets"));
171
+ } catch {
172
+ console.warn(`Assets directory not found: ${assetsPath}, skipping...`);
173
+ }
174
+ const { origin, close: serverClose } = await serveDist(slidevDist, port);
175
+ const browser = await chromium.launch();
176
+ const page = await browser.newPage();
177
+ try {
178
+ for (const n of pages) {
179
+ console.log(`[SSG] page ${n.fileName}`);
180
+ const html = await applyHead(await getPageHtml(page, `${origin}/${n.fileName}`), n.meta ?? { title: `γƒšγƒΌγ‚Έ${n.fileName}` });
181
+ await fs.writeFile(path.join(outDir, `${n.fileName}.html`), html);
182
+ }
183
+ } finally {
184
+ await browser.close();
185
+ serverClose();
186
+ }
187
+ console.log("done");
188
+ }
189
+
190
+ //#endregion
191
+ //#region src/run.ts
192
+ await run();
193
+
194
+ //#endregion
195
+ export { };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "slidev-prerender",
3
+ "type": "module",
4
+ "version": "0.0.1-alpha.0",
5
+ "description": "",
6
+ "author": "petaxa <soccer.i_y@icloud.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/petaxa/slidev-prerender/packages/core#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/petaxa/slidev-prerender.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/petaxa/slidev-prerender/issues"
15
+ },
16
+ "exports": {
17
+ ".": "./dist/index.mjs",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "main": "./dist/index.mjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.mts",
23
+ "bin": {
24
+ "slidev-prerender": "./bin/slidev-prerender.mjs"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "bin"
29
+ ],
30
+ "dependencies": {
31
+ "@playwright/test": "^1.57.0",
32
+ "markdown-it": "^14.1.0",
33
+ "unhead": "^2.1.1",
34
+ "sirv": "^3.0.2"
35
+ },
36
+ "scripts": {
37
+ "build": "tsdown",
38
+ "dev": "tsdown --watch",
39
+ "test": "vitest",
40
+ "typecheck": "tsc --noEmit"
41
+ }
42
+ }