-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplugin-readme.ts
149 lines (132 loc) · 3.74 KB
/
plugin-readme.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
import { Marked } from "marked";
import { baseUrl } from "marked-base-url";
import { markedHighlight } from "marked-highlight";
import fsp from "node:fs/promises";
import path from "node:path";
import { codeToHtml } from "shiki";
import type { Plugin } from "vite";
type PluginReadmeOptions = {
title: string;
description: string;
watchDir: string;
baseUrl: string;
};
export const pluginReadme = (options: PluginReadmeOptions): Plugin => {
const watchDir = path.join(__dirname, options.watchDir);
let base = "/";
const marked = new Marked(
markedHighlight({
async: true,
async highlight(code, lang) {
const highlightedCode = await codeToHtml(code, {
lang,
theme: "github-dark",
});
return `{@html ${JSON.stringify(highlightedCode)}}\n`;
},
}),
baseUrl(options.baseUrl),
{
renderer: {
html(html) {
if (html.text.includes("render:")) {
// Parse the name from the comment (after "render:")
const regex = /<!--\s*render:(\w+)\s*-->/;
const match = html.text.match(regex);
const componentName = match?.[1];
if (!componentName) {
return html.text;
}
return `<div class="code-demo"><${componentName} /></div>`;
}
return html.text;
},
code(code) {
// Shiki returns a string with the code wrapped in a <pre> tag.
// We need to return the code without the extra <pre> tag.
return code.text;
},
},
gfm: true,
breaks: true,
},
);
return {
name: "vite:process-readme",
// Run before the Svelte plugin.
enforce: "pre",
configureServer(server) {
server.watcher.add(watchDir);
},
handleHotUpdate({ file, server }) {
// If a README.md file changed, force reload the page
// This ensures clean state and no duplicate scripts.
if (file.endsWith("README.md")) {
server.ws.send({ type: "full-reload" });
return [];
}
// If a Svelte component in the watched directory changed,
// let the Svelte plugin handle the HMR.
if (file.startsWith(watchDir) && file.endsWith(".svelte")) {
return;
}
},
configResolved(config) {
// Get the base URL from Vite config.
base = config.base;
},
transformIndexHtml(html) {
return `<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="${options.description}"
/>
<link rel="icon" type="image/svg+xml" href="${base}favicon.svg" />
<title>${options.title}</title>
<style>
html {
color-scheme: dark;
}
main {
max-width: 960px;
margin: auto;
padding: 0 1rem;
}
.markdown-body pre.shiki {
border-radius: 0;
}
.code-demo {
padding: 1rem;
border: 1px solid #24292e;
}
</style>
</head>
<body class="markdown-body">
<main id="readme"></main>
${html}
</body>
</html>
`;
},
async transform(code, id) {
if (id.endsWith("README.md")) {
let imports = "";
const watchedFiles = await fsp.readdir(
path.join(__dirname, options.watchDir),
);
for (const file of watchedFiles) {
if (file.endsWith(".svelte")) {
const moduleName = file.replace(".svelte", "");
imports += `import ${moduleName} from "${options.watchDir}/${file}";\n`;
}
}
const importsBlock = `<script>${imports}</script>`;
const html = await marked.parse(code);
return importsBlock + html;
}
},
};
};