Skip to content

Commit 8509284

Browse files
committed
fix(vitepress-theme): update mdream v0.12.3
- Update @mdream/vite from v0.12.2 to v0.12.3 - Remove guardMarkdownMiddleware workaround (~80 lines) - v0.12.3 fixes Vite module interception issue - Simplify plugin config, direct viteHtmlToMarkdownPlugin use
1 parent 9e977c1 commit 8509284

File tree

3 files changed

+17
-102
lines changed

3 files changed

+17
-102
lines changed

packages/nimiq-vitepress-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"dependencies": {
7272
"@iconify-json/tabler": "catalog:icons",
7373
"@iconify-json/vscode-icons": "catalog:icons",
74-
"@mdream/vite": "^0.12.2",
7574
"@nolebase/vitepress-plugin-git-changelog": "catalog:build",
7675
"@vueuse/core": "catalog:utils",
7776
"@vueuse/motion": "^3.0.3",
@@ -87,6 +86,7 @@
8786
},
8887
"devDependencies": {
8988
"@iconify/utils": "catalog:icons",
89+
"@mdream/vite": "^0.12.3",
9090
"@types/mark.js": "^8.11.12",
9191
"@types/markdown-it": "catalog:types",
9292
"@unocss/cli": "catalog:style",

packages/nimiq-vitepress-theme/src/vite/index.ts

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { IncomingMessage } from 'node:http'
2-
import type { Plugin, PreviewServerHook, ServerHook } from 'vite'
1+
import type { Plugin } from 'vite'
32
import fs from 'node:fs'
43
import path from 'node:path'
54
import process from 'node:process'
@@ -118,109 +117,25 @@ export function NimiqVitepressVitePlugin({
118117
]
119118

120119
// 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(
122122
viteHtmlToMarkdownPlugin({
123-
outputDir: 'markdown',
124123
cacheEnabled: true,
125124
exclude: [
126125
'**/node_modules/**',
127126
'**/@vite/**',
128127
'**/@id/**',
129128
'**/@fs/**',
129+
'**/__*/**', // Exclude Vite internals
130130
'**/*.js',
131131
'**/*.mjs',
132132
'**/*.ts',
133133
'**/*.tsx',
134+
'**/*.json',
135+
'**/*.css',
134136
],
135137
}),
136138
)
137139

138-
plugins.push(markdownPlugin)
139-
140140
return plugins
141141
}
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-
}

pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)