|
| 1 | +import * as fs from 'fs/promises'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +const inputDir = path.join(__dirname, '../../docs'); |
| 5 | +const outputDir = path.join(__dirname, '../../demo/docs'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Converts a kebab-case string to camelCase |
| 9 | + */ |
| 10 | +const kebabToCamelCase = (str: string): string => { |
| 11 | + return str.replace(/-./g, (match) => match.charAt(1).toUpperCase()); |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Generates the content for the MDX file |
| 16 | + */ |
| 17 | +const getContent = (title: string, updatedContent: string): string => ` |
| 18 | +{/* |
| 19 | +This file is auto-generated. Any changes made to this file will be overwritten |
| 20 | +*/} |
| 21 | +
|
| 22 | +import { Meta, Markdown } from '@storybook/blocks'; |
| 23 | +
|
| 24 | +<Meta title="Docs / ${title}" /> |
| 25 | +
|
| 26 | +<Markdown>{${JSON.stringify(updatedContent)}}</Markdown> |
| 27 | +`; |
| 28 | + |
| 29 | +/** |
| 30 | + * Writes the MDX file to the specified path |
| 31 | + */ |
| 32 | +const generateMdxFile = async ( |
| 33 | + _: string, |
| 34 | + outputFilePath: string, |
| 35 | + title: string, |
| 36 | + updatedContent: string, |
| 37 | +): Promise<void> => { |
| 38 | + const content = getContent(title, updatedContent); |
| 39 | + await fs.writeFile(outputFilePath, content, 'utf8'); |
| 40 | + console.log(`Generated: ${outputFilePath}`); |
| 41 | +}; |
| 42 | + |
| 43 | +const TITLE_MATCH = /^#####\s+(.*)$/m; |
| 44 | + |
| 45 | +/** |
| 46 | + * Clears the output directory |
| 47 | + */ |
| 48 | +const clearOutputDir = async (): Promise<void> => { |
| 49 | + try { |
| 50 | + await fs.rm(outputDir, {recursive: true, force: true}); |
| 51 | + console.log(`Cleared directory: ${outputDir}`); |
| 52 | + } catch (error) { |
| 53 | + console.error(`Failed to clear directory: ${outputDir}`, error); |
| 54 | + throw error; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * Generate MDX files from Markdown |
| 60 | + */ |
| 61 | +const generateDocs = async (): Promise<void> => { |
| 62 | + console.log('Running docs:generate...'); |
| 63 | + try { |
| 64 | + await clearOutputDir(); |
| 65 | + await fs.mkdir(outputDir, {recursive: true}); |
| 66 | + |
| 67 | + const files = await fs.readdir(inputDir); |
| 68 | + |
| 69 | + for (const file of files) { |
| 70 | + if (path.extname(file) === '.md') { |
| 71 | + const inputFilePath = path.join(inputDir, file); |
| 72 | + const content = await fs.readFile(inputFilePath, 'utf8'); |
| 73 | + |
| 74 | + const titleMatch = content.match(TITLE_MATCH); |
| 75 | + if (!titleMatch) { |
| 76 | + console.warn(`No title found in ${file}, skipping.`); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const title = titleMatch[1].trim(); |
| 81 | + const baseName = kebabToCamelCase(file.replace(/\.md$/, '')); |
| 82 | + const outputFilePath = path.join(outputDir, `${baseName}.mdx`); |
| 83 | + |
| 84 | + await generateMdxFile(inputFilePath, outputFilePath, title, content); |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + console.error('Error generating docs:', error); |
| 89 | + throw error; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Custom storybook addon for generate docs |
| 95 | + */ |
| 96 | +export default { |
| 97 | + name: 'generate-docs', |
| 98 | + async managerEntries(entries: string[] = []): Promise<string[]> { |
| 99 | + try { |
| 100 | + await generateDocs(); |
| 101 | + } catch (error) { |
| 102 | + console.error('Error running docs:generate:', error); |
| 103 | + } |
| 104 | + return entries; |
| 105 | + }, |
| 106 | +}; |
0 commit comments