import fs from 'node:fs'; import path from 'node:path'; import { visit } from 'unist-util-visit'; import { collectSampleFiles, readDocsCodeFile, type DocsCodeFile } from './docs-samples'; interface RemarkCodeExampleOptions { /** * Base GitHub URL for the repository. When omitted, no "View on GitHub" * links are generated. * Example: 'https://github.com/cloudpdf/cloudpdf/blob/main/cloudpdf/website/' */ githubBaseUrl?: string; } /** * Remark plugin that processes components, reading the referenced * source files from disk so they can be highlighted and displayed. * * Usage: * * */ let demoManifestCache: Record> | null = null; /** The built demos (`public/demos/demos-manifest.json`): each sample's module, by integration. */ export function readDemoManifest(): Record> { if (demoManifestCache) return demoManifestCache; try { demoManifestCache = JSON.parse( fs.readFileSync( path.resolve(process.cwd(), 'public', 'demos', 'demos-manifest.json'), 'utf-8', ), ); } catch { demoManifestCache = {}; } return demoManifestCache!; } export const remarkCodeExample = (options: RemarkCodeExampleOptions = {}) => { const { githubBaseUrl } = options; return (tree: any) => { visit(tree, 'mdxJsxFlowElement', (node: any) => { // : code only, one file per framework under // samples/snippets/ (docs/content/snippets/), resolved exactly like an example. if (node.name === 'Example' || node.name === 'Snippet') { const nameAttr = node.attributes?.find( (attr: any) => attr.type === 'mdxJsxAttribute' && attr.name === 'name', ); if (typeof nameAttr?.value !== 'string') return; const sampleName = node.name === 'Snippet' ? `snippets/${nameAttr.value}` : nameAttr.value; const byFramework = collectSampleFiles(sampleName, githubBaseUrl); // For rehype-code-example, which highlights the files, stores them under the sample's // name and leaves the page only a reference to them (docs-kit `mdx/code-panels`). node.attributes.push( { type: 'mdxJsxAttribute', name: '__sample', value: sampleName }, { type: 'mdxJsxAttribute', name: '__fwFiles', value: JSON.stringify(byFramework) }, ); // Live demos: built by vite.demos.config.ts before the docs build; // presence in the manifest = a mounted preview exists. A snippet is code only, even // when an example has the same name. const demos = node.name === 'Example' ? readDemoManifest()[nameAttr.value] : undefined; if (demos) { node.attributes.push({ type: 'mdxJsxAttribute', name: 'demosByFramework', value: JSON.stringify(demos), }); } node.attributes.push({ type: 'mdxJsxAttribute', name: '__needsHighlighting', value: 'true', }); return; } if (node.name !== 'CodeExample') return; const codePathAttr = node.attributes?.find( (attr: any) => attr.type === 'mdxJsxAttribute' && attr.name === 'codePath', ); const codePathsAttr = node.attributes?.find( (attr: any) => attr.type === 'mdxJsxAttribute' && attr.name === 'codePaths', ); let paths: string[] = []; if (codePathAttr?.value && typeof codePathAttr.value === 'string') { paths = [codePathAttr.value]; } if (codePathsAttr?.value) { const exprValue = codePathsAttr.value; if (exprValue?.type === 'mdxJsxAttributeValueExpression') { try { const estree = exprValue.data?.estree; const expr = estree?.body?.[0]?.expression; if (expr?.type === 'ArrayExpression') { paths = expr.elements .filter((el: any) => el?.type === 'Literal' && typeof el.value === 'string') .map((el: any) => el.value); } } catch { console.warn('[remark-code-example] Could not parse codePaths expression'); } } } if (paths.length === 0) return; const files: DocsCodeFile[] = paths .map((p) => readDocsCodeFile(p, githubBaseUrl)) .filter((f): f is DocsCodeFile => f !== null); if (files.length === 0) return; node.attributes = node.attributes.filter( (attr: any) => attr.name !== 'codePath' && attr.name !== 'codePaths', ); node.attributes = node.attributes.filter((attr: any) => attr.name !== 'githubUrl'); node.attributes.push({ type: 'mdxJsxAttribute', name: '__codeFiles', value: JSON.stringify(files), }); node.attributes.push({ type: 'mdxJsxAttribute', name: '__needsHighlighting', value: 'true', }); }); }; };