|
1 | | -import type { IncomingMessage } from 'node:http' |
2 | | -import type { Plugin, PreviewServerHook, ServerHook } from 'vite' |
| 1 | +import type { Plugin } from 'vite' |
3 | 2 | import fs from 'node:fs' |
4 | 3 | import path from 'node:path' |
5 | 4 | import process from 'node:process' |
@@ -118,109 +117,25 @@ export function NimiqVitepressVitePlugin({ |
118 | 117 | ] |
119 | 118 |
|
120 | 119 | // Add mdream plugin to generate markdown copies of each page |
121 | | - const markdownPlugin = guardMarkdownMiddleware( |
| 120 | + // Generates .md files alongside .html files in build output |
| 121 | + plugins.push( |
122 | 122 | viteHtmlToMarkdownPlugin({ |
123 | | - outputDir: 'markdown', |
124 | 123 | cacheEnabled: true, |
125 | 124 | exclude: [ |
126 | 125 | '**/node_modules/**', |
127 | 126 | '**/@vite/**', |
128 | 127 | '**/@id/**', |
129 | 128 | '**/@fs/**', |
| 129 | + '**/__*/**', // Exclude Vite internals |
130 | 130 | '**/*.js', |
131 | 131 | '**/*.mjs', |
132 | 132 | '**/*.ts', |
133 | 133 | '**/*.tsx', |
| 134 | + '**/*.json', |
| 135 | + '**/*.css', |
134 | 136 | ], |
135 | 137 | }), |
136 | 138 | ) |
137 | 139 |
|
138 | | - plugins.push(markdownPlugin) |
139 | | - |
140 | 140 | return plugins |
141 | 141 | } |
142 | | - |
143 | | -function guardMarkdownMiddleware(plugin: Plugin): Plugin { |
144 | | - const shouldServeMarkdown = (req: IncomingMessage | undefined) => { |
145 | | - if (!req?.url) { |
146 | | - return false |
147 | | - } |
148 | | - |
149 | | - if (req.url.endsWith('.md')) { |
150 | | - return true |
151 | | - } |
152 | | - |
153 | | - const accept = req.headers.accept ?? '' |
154 | | - return accept.includes('text/markdown') |
155 | | - } |
156 | | - interface HookObject<T> { |
157 | | - handler: T |
158 | | - order?: 'pre' | 'post' | null |
159 | | - } |
160 | | - |
161 | | - const isHookObject = <T>(value: unknown): value is HookObject<T> => { |
162 | | - return Boolean(value) && typeof value === 'object' && 'handler' in (value as Record<string, unknown>) |
163 | | - } |
164 | | - |
165 | | - const wrapHook = <T extends (server: any) => any>(hook?: T | HookObject<T>) => { |
166 | | - if (!hook) { |
167 | | - return hook |
168 | | - } |
169 | | - |
170 | | - const original = typeof hook === 'function' ? hook : hook.handler |
171 | | - |
172 | | - const wrapped: T = ((server: any) => { |
173 | | - const middlewares = (server as any)?.middlewares |
174 | | - if (!middlewares?.use) { |
175 | | - return original(server) |
176 | | - } |
177 | | - |
178 | | - const originalUse = middlewares.use.bind(middlewares) |
179 | | - |
180 | | - middlewares.use = (fn: any) => originalUse((req: IncomingMessage, res: any, next: any) => { |
181 | | - if (shouldServeMarkdown(req)) { |
182 | | - return fn(req, res, next) |
183 | | - } |
184 | | - return next() |
185 | | - }) |
186 | | - |
187 | | - const result = original(server) |
188 | | - |
189 | | - const restore = () => { |
190 | | - middlewares.use = originalUse |
191 | | - } |
192 | | - |
193 | | - if (typeof result === 'function') { |
194 | | - return (...args: any[]) => { |
195 | | - try { |
196 | | - return result(...args) |
197 | | - } |
198 | | - finally { |
199 | | - restore() |
200 | | - } |
201 | | - } |
202 | | - } |
203 | | - |
204 | | - if (result && typeof result.then === 'function') { |
205 | | - return (result.finally(restore)) |
206 | | - } |
207 | | - |
208 | | - restore() |
209 | | - return result |
210 | | - }) as T |
211 | | - |
212 | | - if (!isHookObject<T>(hook)) { |
213 | | - return wrapped |
214 | | - } |
215 | | - |
216 | | - return { |
217 | | - ...hook, |
218 | | - handler: wrapped, |
219 | | - } |
220 | | - } |
221 | | - |
222 | | - plugin.configureServer = wrapHook(plugin.configureServer as ServerHook | HookObject<ServerHook>) as typeof plugin.configureServer |
223 | | - plugin.configurePreviewServer = wrapHook(plugin.configurePreviewServer as PreviewServerHook | HookObject<PreviewServerHook>) as typeof plugin.configurePreviewServer |
224 | | - |
225 | | - return plugin |
226 | | -} |
0 commit comments