-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate-mdx, generate-registry
- Loading branch information
1 parent
b475431
commit fba42df
Showing
4 changed files
with
122 additions
and
113 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
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,69 @@ | ||
import chalk from "chalk"; | ||
import { existsSync, promises as fs, readFileSync } from "fs"; | ||
import path, { basename } from "node:path"; | ||
|
||
import { generateMDXTemplate } from "./utils/generate-mdx-template.js"; | ||
|
||
const PUBLIC_PATH = path.join(process.cwd(), "public"); | ||
const SNIPPETS_PATH = path.join(process.cwd(), "snippets"); | ||
|
||
interface GenerateMDXProps { | ||
target: string; | ||
} | ||
|
||
async function generateMDX(props: GenerateMDXProps) { | ||
const { target } = props; | ||
|
||
const targetMDXPath = path.join(PUBLIC_PATH, "mdx"); | ||
const targetSnippetPath = path.join(SNIPPETS_PATH, "example", target); | ||
|
||
if (!existsSync(targetMDXPath)) { | ||
console.log(chalk.red(`[Generate Registry] ${chalk.bgRed(targetMDXPath)} dir does not exist!`)); | ||
await fs.mkdir(targetMDXPath, { recursive: true }); | ||
} | ||
|
||
if (!existsSync(targetSnippetPath)) { | ||
console.log( | ||
chalk.red(`[Generate Registry] ${chalk.bgRed(targetSnippetPath)} dir does not exist!`), | ||
); | ||
await fs.mkdir(targetSnippetPath, { recursive: true }); | ||
} | ||
|
||
const snippets = await fs.readdir(targetSnippetPath); | ||
for (const snippet of snippets) { | ||
const snippetPath = path.join(targetSnippetPath, snippet); | ||
|
||
if (!existsSync(snippetPath)) { | ||
console.log( | ||
chalk.red(`[Generate Registry] ${chalk.bgRed(target)} Snippet file does not exist!`), | ||
); | ||
return null; | ||
} | ||
|
||
const content = readFileSync(snippetPath, "utf-8"); | ||
const code = generateMDXTemplate({ | ||
language: "tsx", | ||
template: content, | ||
copy: true, | ||
}); | ||
const fileName = basename(snippet).split(".")[0]; | ||
const writeDir = path.join(targetMDXPath, target); | ||
|
||
if (!existsSync(writeDir)) { | ||
await fs.mkdir(writeDir, { recursive: true }); | ||
} | ||
|
||
await fs.writeFile(path.join(writeDir, `${fileName}.mdx`), code, "utf-8"); | ||
} | ||
} | ||
|
||
function main() { | ||
console.log(chalk.gray("Generate MDX...")); | ||
|
||
generateMDX({ target: "react" }); | ||
generateMDX({ target: "stackflow" }); | ||
|
||
console.log(chalk.green("MDX Generated!")); | ||
} | ||
|
||
main(); |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { dedent } from "ts-dedent"; | ||
|
||
interface GenerateMDXTemplateProps { | ||
language: string; | ||
template: string; | ||
|
||
/** | ||
* @example ["1", "2-5", "3"] | ||
*/ | ||
highlightLines?: string[]; | ||
|
||
/** | ||
* @example ["import", "export"] | ||
*/ | ||
subStrings?: string[]; | ||
|
||
copy?: boolean; | ||
showLineNumbers?: boolean; | ||
|
||
/** | ||
* @example "BoxButtonPreview.tsx" | ||
*/ | ||
filename?: string; | ||
} | ||
|
||
export function generateMDXTemplate(props: GenerateMDXTemplateProps) { | ||
const { language, highlightLines, subStrings, copy, showLineNumbers, filename, template } = props; | ||
|
||
const highlightLinesCode = highlightLines && `{${highlightLines.join(",")}}`; | ||
const subStringsCode = subStrings && `/${subStrings.join(",")}/`; | ||
const copyCode = copy && "copy"; | ||
const showLineNumbersCode = showLineNumbers && "showLineNumbers"; | ||
const filenameCode = filename && `filename="${filename}"`; | ||
const metas = [highlightLinesCode, subStringsCode, copyCode, showLineNumbersCode, filenameCode] | ||
.filter(Boolean) | ||
.join(" "); | ||
|
||
return dedent` | ||
\`\`\`${language} ${metas} | ||
${template} | ||
\`\`\`\n | ||
`; | ||
} |