Skip to content

Commit

Permalink
Fix doc parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUltDev committed Jun 15, 2024
1 parent c246fd5 commit 1dbc10b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
4 changes: 1 addition & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions toolkit/vocs/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async function processComponents(files: string[], paths: string[]) {
files.flat(1).filter(f => f.endsWith('.mdx')).map(async path => {
const {name, base} = getPartsFromDocPath(path);
const component = await readFile(`${input}/${base}/${name}.tsx`, 'utf8');
const [doc] = parseDocs(component);
// TODO: remove this when parsing is fixed properly via babel options in parseDocs
const sanitized = component.replace(/as const|as any/g, '');
const [doc] = parseDocs(sanitized);
const componentName = doc.displayName || '';
let mdx = await readFile(`${input}/${base}/${name}.docs.mdx`, 'utf8');
mdx = mdx.replace(/:::header:::/g, `\n# ${componentName}\n\n> ${doc.description}`);
Expand All @@ -60,7 +62,7 @@ async function processComponents(files: string[], paths: string[]) {
const imports = mdx.match(/:::imports(.*?):::/s)?.[1];
if (demo) mdx = mdx.replace(/:::demo(.*?):::/gs, demo.trim());
if (imports) mdx = mdx.replace(/:::imports(.*?):::/gs, imports.trim());
mdx = mdx.replace(/:::usage:::/g, getCodeBlock(componentName, imports, demo));
mdx = mdx.replace(/:::usage:::/g, getCodeBlock(imports, demo));
const final = getDefaultImports() + '\n' + mdx;
await ensureDir([output, ...base.split('/')].slice(0, -1).join('/'));
await writeFile(`${output}/${base}.mdx`, final, 'utf8');
Expand All @@ -73,13 +75,12 @@ function getDefaultImports() {
].join('\n');
}

function getCodeBlock(componentName: string, imports?: string, demo?: string) {
function getCodeBlock(imports?: string, demo?: string) {
return [
'```tsx twoslash',
'import React from \'react\';',
'// ---cut---',
'// @log: ↓ Import the component',
`import {${componentName}} from \'design\';`,
imports ? imports?.trim() + '\n' : '',
'// @log: ↓ Try the example',
demo?.trim(),
Expand Down

0 comments on commit 1dbc10b

Please sign in to comment.