-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(demo): integrated autogenerated docs into the demo (#490)
- Loading branch information
1 parent
fbc977c
commit 74790ff
Showing
16 changed files
with
154 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ build/ | |
coverage/ | ||
storybook-static/ | ||
.eslintcache | ||
demo/docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as fs from 'fs/promises'; | ||
import * as path from 'path'; | ||
|
||
const inputDir = path.join(__dirname, '../../docs'); | ||
const outputDir = path.join(__dirname, '../../demo/docs'); | ||
|
||
/** | ||
* Converts a kebab-case string to camelCase | ||
*/ | ||
const kebabToCamelCase = (str: string): string => { | ||
return str.replace(/-./g, (match) => match.charAt(1).toUpperCase()); | ||
}; | ||
|
||
/** | ||
* Generates the content for the MDX file | ||
*/ | ||
const getContent = (title: string, updatedContent: string): string => ` | ||
{/* | ||
This file is auto-generated. Any changes made to this file will be overwritten | ||
*/} | ||
import { Meta, Markdown } from '@storybook/blocks'; | ||
<Meta title="Docs / ${title}" /> | ||
<Markdown>{${JSON.stringify(updatedContent)}}</Markdown> | ||
`; | ||
|
||
/** | ||
* Writes the MDX file to the specified path | ||
*/ | ||
const generateMdxFile = async ( | ||
_: string, | ||
outputFilePath: string, | ||
title: string, | ||
updatedContent: string, | ||
): Promise<void> => { | ||
const content = getContent(title, updatedContent); | ||
await fs.writeFile(outputFilePath, content, 'utf8'); | ||
console.log(`Generated: ${outputFilePath}`); | ||
}; | ||
|
||
const TITLE_MATCH = /^#####\s+(.*)$/m; | ||
|
||
/** | ||
* Clears the output directory | ||
*/ | ||
const clearOutputDir = async (): Promise<void> => { | ||
try { | ||
await fs.rm(outputDir, {recursive: true, force: true}); | ||
console.log(`Cleared directory: ${outputDir}`); | ||
} catch (error) { | ||
console.error(`Failed to clear directory: ${outputDir}`, error); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* Generate MDX files from Markdown | ||
*/ | ||
const generateDocs = async (): Promise<void> => { | ||
console.log('Running docs:generate...'); | ||
try { | ||
await clearOutputDir(); | ||
await fs.mkdir(outputDir, {recursive: true}); | ||
|
||
const files = await fs.readdir(inputDir); | ||
|
||
for (const file of files) { | ||
if (path.extname(file) === '.md') { | ||
const inputFilePath = path.join(inputDir, file); | ||
const content = await fs.readFile(inputFilePath, 'utf8'); | ||
|
||
const titleMatch = content.match(TITLE_MATCH); | ||
if (!titleMatch) { | ||
console.warn(`No title found in ${file}, skipping.`); | ||
continue; | ||
} | ||
|
||
const title = titleMatch[1].trim(); | ||
const baseName = kebabToCamelCase(file.replace(/\.md$/, '')); | ||
const outputFilePath = path.join(outputDir, `${baseName}.mdx`); | ||
|
||
await generateMdxFile(inputFilePath, outputFilePath, title, content); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error generating docs:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* Custom storybook addon for generate docs | ||
*/ | ||
export default { | ||
name: 'generate-docs', | ||
async managerEntries(entries: string[] = []): Promise<string[]> { | ||
try { | ||
await generateDocs(); | ||
} catch (error) { | ||
console.error('Error running docs:generate:', error); | ||
} | ||
return entries; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
##### Develop / Preview | ||
|
||
## How to Add Preview for Markup Mode | ||
|
||
### Add a Preview component | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
##### Develop / Editor customization | ||
|
||
## How to customize the editor | ||
You can use CSS variables to make editor contents fit your own needs | ||
|
||
| **Variable** | **Description** | **CSS Property Type** | **Default** | | ||
| :---: | :---: | :---: | :---: | | ||
| `--g-md-toolbar-padding` | Toolbar padding | padding | 0px | | ||
| `--g-md-toolbar-sticky-padding` | Toolbar padding in sticky mode | padding | -4px | | ||
| `--g-md-toolbar-sticky-inset` | Toolbar inset in sticky mode | inset | -4px | | ||
| `--g-md-toolbar-sticky-offset` | Toolbar offset in sticky mode | top | 0px | | ||
| `--g-md-editor-padding` | Editor contents padding | padding | 0px | | ||
| `--g-md-editor-padding` | Editor contents padding | padding | 0px | |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters