-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvite.config.ts
More file actions
343 lines (308 loc) · 12 KB
/
Copy pathvite.config.ts
File metadata and controls
343 lines (308 loc) · 12 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import mdx from '@mdx-js/rollup'
import remarkGfm from 'remark-gfm'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
import rehypeSlug from 'rehype-slug'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeShiki from '@shikijs/rehype'
import { createReadStream, cpSync, existsSync, statSync } from 'fs'
import { resolve, dirname, extname, join, relative } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const BASE = '/ts-stack/'
const DOCS_ROOT = resolve(__dirname, '../docs')
const DOCS_ASSETS = resolve(DOCS_ROOT, 'assets')
const assetMimeTypes: Record<string, string> = {
'.gif': 'image/gif',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.pdf': 'application/pdf',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.webp': 'image/webp',
}
function docsAssetPathFromUrl(url: string | undefined) {
const pathname = (url ?? '').split('?')[0]
const assetPath = pathname.startsWith(`${BASE}assets/`)
? pathname.slice(BASE.length)
: pathname.startsWith('/assets/')
? pathname.slice(1)
: null
if (!assetPath?.match(/^assets\/(?:diagrams|images)\//)) return null
return decodeURIComponent(assetPath.slice('assets/'.length))
}
function resolveDocsAsset(assetPath: string) {
const file = resolve(DOCS_ASSETS, assetPath)
const rel = relative(DOCS_ASSETS, file)
if (rel.startsWith('..') || rel.startsWith('/')) return null
return file
}
function docsAssetsPlugin(): import('vite').Plugin {
return {
name: 'docs-assets',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const assetPath = docsAssetPathFromUrl(req.url)
if (!assetPath) return next()
const file = resolveDocsAsset(assetPath)
if (!file || !existsSync(file) || !statSync(file).isFile()) return next()
res.setHeader('Content-Type', assetMimeTypes[extname(file).toLowerCase()] ?? 'application/octet-stream')
createReadStream(file).pipe(res)
})
},
closeBundle() {
const distAssets = resolve(__dirname, 'dist/assets')
for (const folder of ['diagrams', 'images']) {
const sourceRoot = join(DOCS_ASSETS, folder)
cpSync(sourceRoot, join(distAssets, folder), {
recursive: true,
filter: (source) => !relative(sourceRoot, source).split(/[/\\]/).some((part) => part.startsWith('.')),
})
}
},
}
}
function isExternalHref(href: string) {
return /^(?:https?:|data:|mailto:|#|\/\/)/.test(href)
}
function splitUrlSuffix(url: string) {
const suffixIndex = url.search(/[?#]/)
if (suffixIndex === -1) return { pathname: url, suffix: '' }
return {
pathname: url.slice(0, suffixIndex),
suffix: url.slice(suffixIndex),
}
}
function docsRouteFromMarkdownPath(relPath: string) {
if (relPath === 'index.md') return ''
if (relPath.endsWith('/index.md')) return relPath.slice(0, -'index.md'.length)
return `${relPath.slice(0, -'.md'.length)}/`
}
function routeForMarkdownFile(file: string, suffix: string) {
const relPath = relative(DOCS_ROOT, file).replaceAll('\\', '/')
if (relPath.startsWith('../') || relPath === '..' || relPath.startsWith('/') || !relPath.endsWith('.md')) {
return null
}
return `${BASE}${docsRouteFromMarkdownPath(relPath)}${suffix}`
}
function resolveDocsHrefPath(pathname: string, sourceFile: string) {
const normalized = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname
const targetPath = pathname.startsWith('/docs/')
? resolve(DOCS_ROOT, pathname.slice('/docs/'.length))
: pathname.startsWith('/')
? resolve(DOCS_ROOT, pathname.slice(1))
: resolve(dirname(sourceFile), normalized)
if (pathname.endsWith('.md')) return targetPath
if (/\.[a-z0-9]+$/i.test(pathname)) return null
const directFile = `${targetPath}.md`
if (existsSync(directFile)) return directFile
const indexFile = join(targetPath, 'index.md')
if (existsSync(indexFile)) return indexFile
return null
}
function sourceFilePath(file: Record<string, unknown> | undefined) {
if (typeof file?.path === 'string') return file.path
const history = file?.history
if (Array.isArray(history)) {
const last = history[history.length - 1]
if (typeof last === 'string') return last
}
return resolve(DOCS_ROOT, 'index.md')
}
function normalizeDocsPageHref(href: string, sourceFile: string) {
if (isExternalHref(href)) return href
const { pathname, suffix } = splitUrlSuffix(href)
if (!pathname || pathname.startsWith(BASE)) return href
const targetFile = resolveDocsHrefPath(pathname, sourceFile)
if (!targetFile) return href
return routeForMarkdownFile(targetFile, suffix) ?? href
}
function normalizeDocsAssetSrc(src: string) {
if (isExternalHref(src)) return src
const match = src.match(/(?:^|\/)(assets\/(?:diagrams|images)\/[^?#]+)/)
if (!match) return src
const assetPath = match[1]
const suffix = src.slice(src.indexOf(assetPath) + assetPath.length)
return `${BASE}${assetPath}${suffix}`
}
function rehypeDocsPaths() {
return (tree: Record<string, unknown>, file: Record<string, unknown>) => {
rewriteDocsPaths(tree, sourceFilePath(file))
}
}
function rewriteDocsPaths(node: Record<string, unknown>, sourceFile: string) {
if (node.type === 'element' && node.tagName === 'a') {
const properties = node.properties as Record<string, unknown> | undefined
if (properties && typeof properties.href === 'string') {
properties.href = normalizeDocsPageHref(properties.href, sourceFile)
}
}
if (node.type === 'element' && node.tagName === 'img') {
const properties = node.properties as Record<string, unknown> | undefined
if (properties && typeof properties.src === 'string') {
properties.src = normalizeDocsAssetSrc(properties.src)
}
}
const children = node.children
if (Array.isArray(children)) {
for (const child of children) {
if (child && typeof child === 'object') rewriteDocsPaths(child as Record<string, unknown>, sourceFile)
}
}
}
/**
* Remark plugin: find `html` MDAST nodes that contain known JSX component
* tags (e.g. `<AsyncApiEmbed slug="brc29" />`) and convert them to a custom
* MDAST node type. The corresponding remarkRehypeOptions.handlers entry then
* turns those into proper HAST elements. This sidesteps rehypeRemoveRaw, which
* MDX adds when processing .md files in format:'md' mode and which silently
* strips all raw HTML nodes before user rehype plugins can see them.
*/
function remarkLiftHtmlComponents() {
return (tree: { type: string; children: Array<Record<string, unknown>> }) => {
liftInChildren(tree.children)
}
}
function liftInChildren(children: Array<Record<string, unknown>>) {
for (let i = 0; i < children.length; i++) {
const node = children[i]
if (node.type === 'html' && typeof node.value === 'string') {
const asyncApiMatch = (node.value as string).match(/<AsyncApiEmbed\s+slug="([^"]+)"\s*\/>/)
if (asyncApiMatch) {
children[i] = { type: 'asyncApiEmbed', slug: asyncApiMatch[1] }
continue
}
if ((node.value as string).match(/<HomeHero\s*\/>/)) {
children[i] = { type: 'homeHero' }
continue
}
}
const childList = node.children
if (Array.isArray(childList)) liftInChildren(childList as Array<Record<string, unknown>>)
}
}
const stripAudioComments: import('vite').Plugin = {
name: 'strip-audio-comments',
enforce: 'pre',
transform(code, id) {
if (!id.endsWith('.md') && !id.endsWith('.mdx')) return
return { code: code.replace(/<!--\s*audio:[^>]*-->/g, ''), map: null }
},
}
export default defineConfig({
base: BASE,
plugins: [
stripAudioComments,
docsAssetsPlugin(),
{
enforce: 'pre',
...mdx({
remarkPlugins: [remarkGfm, remarkFrontmatter, remarkMdxFrontmatter, remarkLiftHtmlComponents],
remarkRehypeOptions: {
handlers: {
homeHero: () => ({
type: 'element',
tagName: 'div',
properties: { className: ['bsv-hero'] },
children: [{
type: 'element',
tagName: 'div',
properties: { className: ['bsv-hero__inner'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: ['bsv-hero__title'] },
children: [
{
type: 'element',
tagName: 'span',
properties: {},
children: [{ type: 'text', value: 'BSV' }],
},
{ type: 'text', value: ' application infrastructure in TypeScript' },
],
},
{
type: 'element',
tagName: 'p',
properties: { className: ['bsv-hero__lede'] },
children: [{
type: 'text',
value: 'Reference packages, protocol specs, infrastructure contracts, and conformance vectors for building wallet-aware BSV applications. Use it as an app developer, a wallet implementer, or the baseline another language implementation must match.',
}],
},
{
type: 'element',
tagName: 'a',
properties: { className: ['bsv-hero__cta'], href: `${BASE}get-started/` },
children: [
{ type: 'text', value: 'Choose your entry point' },
{
type: 'element',
tagName: 'span',
properties: { className: ['bsv-hero__cta-arrow'], ariaHidden: 'true' },
children: [{ type: 'text', value: '->' }],
},
],
},
],
}],
}),
asyncApiEmbed: (_state: unknown, node: Record<string, unknown>) => ({
type: 'element',
tagName: 'iframe',
properties: {
src: `${BASE}assets/asyncapi/${node.slug}/index.html`,
style: 'width: 100%; min-height: 900px; border: none; background: #011627; border-radius: 8px;',
title: `AsyncAPI Specification (${node.slug})`,
loading: 'lazy',
},
children: [],
}),
},
},
rehypePlugins: [
rehypeDocsPaths,
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
[rehypeShiki, {
theme: 'github-dark-dimmed',
langs: ['typescript', 'javascript', 'bash', 'json', 'yaml', 'markdown', 'html', 'css', 'sql', 'go', 'rust', 'python'],
transformers: [{
pre(node: { properties: Record<string, unknown> }) {
const lang = (this as { options?: { lang?: string } }).options?.lang
if (lang) node.properties['data-language'] = lang
},
}],
}],
],
providerImportSource: '@mdx-js/react',
}),
},
react({ include: /\.(mdx?|jsx?|tsx?)$/ }),
],
resolve: {
alias: {
'@docs': resolve(__dirname, '../docs'),
'@': resolve(__dirname, 'src'),
'@mdx-js/react': resolve(__dirname, 'node_modules/@mdx-js/react/index.js'),
// MDX compiles repo-level docs/*.md into JSX that imports react/jsx-runtime.
// Those files live outside docs-site's node_modules tree, so the bundler
// cannot resolve the runtime relative to them. Pin it explicitly.
'react/jsx-runtime': resolve(__dirname, 'node_modules/react/jsx-runtime.js'),
'react/jsx-dev-runtime': resolve(__dirname, 'node_modules/react/jsx-dev-runtime.js'),
},
},
server: {
fs: { allow: ['..', resolve(__dirname, '../docs')] },
},
build: {
outDir: 'dist',
rollupOptions: {
external: [/^\/_pagefind\//],
},
},
})