import path from 'path'; import glob from 'fast-glob'; async function inlineMarkdownFiles() { const files: { path: string; name: string }[] = []; // 添加 readme.md const readmePath = path.join(import.meta.dir, '..', 'readme.md'); files.push({ path: readmePath, name: 'readme' }); // 使用 fast-glob 动态获取 docs 目录下的 md 文件 const rootDir = path.join(import.meta.dir, '..', 'docs'); const mdFiles = await glob('**.md', { cwd: rootDir }); for (const filename of mdFiles) { // 将路径转为变量名,如 examples/base -> examples_base const name = filename.replace(/\.md$/, '').replace(/[^a-zA-Z0-9]/g, '_'); files.push({ path: path.join(rootDir, filename), name }); } let generatedCode = '// Generated by build script\n'; for (const file of files) { try { const content = await Bun.file(file.path).text(); // 转义模板字符串中的特殊字符 const escapedContent = content .replace(/\\/g, '\\\\') .replace(/`/g, '\\`') .replace(/\${/g, '\\${'); generatedCode += `export const ${file.name} = \`${escapedContent}\`;\n`; } catch (error) { console.warn(`Failed to read ${file.path}:`, error); generatedCode += `export const ${file.name} = '';\n`; } } // 写入生成的文件 await Bun.write(path.join(import.meta.dir, 'gen', 'index.ts'), generatedCode); } await inlineMarkdownFiles();