|
| 1 | +import { createJiti } from 'jiti' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | +import { writeFileSync, unlinkSync, mkdtempSync, existsSync, rmdirSync } from 'node:fs' |
| 4 | +import { join, dirname } from 'node:path' |
| 5 | + |
| 6 | +/** |
| 7 | + * Remark plugin that transforms ```spac code blocks into tabbed examples |
| 8 | + * showing both the spac TypeScript source and the resulting OpenAPI YAML. |
| 9 | + * |
| 10 | + * The code is evaluated at build time via jiti, so the YAML is always in sync. |
| 11 | + * |
| 12 | + * The code MUST export the Api instance as `export default api`. |
| 13 | + */ |
| 14 | + |
| 15 | +function findRoot(): string { |
| 16 | + let dir = dirname(fileURLToPath(import.meta.url)) |
| 17 | + while (dir !== dirname(dir)) { |
| 18 | + if (existsSync(join(dir, 'pnpm-workspace.yaml'))) return dir |
| 19 | + dir = dirname(dir) |
| 20 | + } |
| 21 | + return process.cwd() |
| 22 | +} |
| 23 | + |
| 24 | +const root = findRoot() |
| 25 | +const jiti = createJiti(join(root, 'packages', 'website', 'eval.ts')) |
| 26 | + |
| 27 | +function evaluateSpacCode(code: string): string | null { |
| 28 | + const tmp = mkdtempSync(join(root, '.spac-eval-')) |
| 29 | + const entryFile = join(tmp, 'entry.ts') |
| 30 | + |
| 31 | + // Strip `export default` — jiti will handle module.exports |
| 32 | + const cleanCode = code.replace(/export\s+default\s+/, 'module.exports = ') |
| 33 | + writeFileSync(entryFile, cleanCode) |
| 34 | + |
| 35 | + try { |
| 36 | + const mod = jiti(entryFile) as any |
| 37 | + const api = mod?.default ?? mod |
| 38 | + if (!api?.emit) { |
| 39 | + console.warn('[remark-spac-example] No Api.emit() found in code') |
| 40 | + return null |
| 41 | + } |
| 42 | + const result = api.emit({ yaml: true }) |
| 43 | + return typeof result === 'string' ? result : result.yaml |
| 44 | + } catch (e: any) { |
| 45 | + console.warn('[remark-spac-example] Failed to evaluate:', e.message?.slice(0, 300)) |
| 46 | + return null |
| 47 | + } finally { |
| 48 | + try { unlinkSync(entryFile) } catch {} |
| 49 | + try { rmdirSync(tmp) } catch {} |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function walk( |
| 54 | + node: any, |
| 55 | + type: string, |
| 56 | + fn: (node: any, index: number, parent: any) => void, |
| 57 | + _index = 0, |
| 58 | + parent: any = null, |
| 59 | +): void { |
| 60 | + if (node.type === type && parent != null) { |
| 61 | + fn(node, _index, parent) |
| 62 | + } |
| 63 | + if (node.children) { |
| 64 | + for (let i = node.children.length - 1; i >= 0; i--) { |
| 65 | + walk(node.children[i], type, fn, i, node) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export function remarkSpacExample() { |
| 71 | + return (tree: any) => { |
| 72 | + walk(tree, 'code', (node, index, parent) => { |
| 73 | + if (node.lang !== 'spac') return |
| 74 | + |
| 75 | + const tsCode = node.value as string |
| 76 | + const yaml = evaluateSpacCode(tsCode) |
| 77 | + |
| 78 | + if (!yaml) { |
| 79 | + node.lang = 'ts' |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + const tabsNode = { |
| 84 | + type: 'mdxJsxFlowElement', |
| 85 | + name: 'Tabs', |
| 86 | + attributes: [ |
| 87 | + { |
| 88 | + type: 'mdxJsxAttribute', |
| 89 | + name: 'items', |
| 90 | + value: { |
| 91 | + type: 'mdxJsxAttributeValueExpression', |
| 92 | + value: "['spac', 'OpenAPI YAML']", |
| 93 | + data: { |
| 94 | + estree: { |
| 95 | + type: 'Program', |
| 96 | + body: [{ |
| 97 | + type: 'ExpressionStatement', |
| 98 | + expression: { |
| 99 | + type: 'ArrayExpression', |
| 100 | + elements: [ |
| 101 | + { type: 'Literal', value: 'spac' }, |
| 102 | + { type: 'Literal', value: 'OpenAPI YAML' }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + }], |
| 106 | + sourceType: 'module', |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + children: [ |
| 113 | + { |
| 114 | + type: 'mdxJsxFlowElement', |
| 115 | + name: 'Tab', |
| 116 | + attributes: [{ type: 'mdxJsxAttribute', name: 'value', value: 'spac' }], |
| 117 | + children: [{ type: 'code', lang: 'ts', value: tsCode }], |
| 118 | + }, |
| 119 | + { |
| 120 | + type: 'mdxJsxFlowElement', |
| 121 | + name: 'Tab', |
| 122 | + attributes: [{ type: 'mdxJsxAttribute', name: 'value', value: 'OpenAPI YAML' }], |
| 123 | + children: [{ type: 'code', lang: 'yaml', value: yaml }], |
| 124 | + }, |
| 125 | + ], |
| 126 | + } |
| 127 | + |
| 128 | + parent.children.splice(index, 1, tabsNode) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments