Skip to content

Commit 0c7212c

Browse files
feat(mdx): Add script for React mdx conversion (#67)
* DO NOT MERGE: Test mdx conversion of the md files from react-core * DO NOT MERGE: complete test conversion script for react * Remove testing content * fix(tsconfig): remove testing change from tsconfig * Add to cli
1 parent 5bb7239 commit 0c7212c

File tree

7 files changed

+458
-22
lines changed

7 files changed

+458
-22
lines changed

cli/cli.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getConfig } from './getConfig.js'
1111
import { symLinkConfig } from './symLinkConfig.js'
1212
import { buildPropsData } from './buildPropsData.js'
1313
import { hasFile } from './hasFile.js'
14+
import { convertToMDX } from './convertToMDX.js'
1415

1516
function updateContent(program: Command) {
1617
const { verbose } = program.opts()
@@ -89,7 +90,7 @@ program.command('init').action(async () => {
8990

9091
program.command('start').action(async () => {
9192
updateContent(program)
92-
93+
9394
// if a props file hasn't been generated yet, but the consumer has propsData, it will cause a runtime error so to
9495
// prevent that we're just creating a props file regardless of what they say if one doesn't exist yet
9596
const hasPropsFile = await hasFile(join(astroRoot, 'dist', 'props.json'))
@@ -132,4 +133,11 @@ program.command('sync').action(async () => {
132133
sync({ root: astroRoot })
133134
})
134135

136+
program
137+
.command('convert-to-mdx')
138+
.argument('<globPath>', 'The glob path to the files to convert')
139+
.action(async (globPath: string) => {
140+
await convertToMDX(globPath)
141+
})
142+
135143
program.parse(process.argv)

cli/convertToMDX.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { readFile, writeFile } from 'fs/promises'
2+
import { glob } from 'glob'
3+
4+
export async function convertToMDX(globPath: string) {
5+
const files = await glob(globPath)
6+
7+
files.forEach(async (file) => {
8+
const fileContent = await readFile(file, 'utf-8')
9+
10+
//regex link: https://regexr.com/8f0er
11+
const importRegex = /(?<!```no[lL]ive\n)import {?[\w\s,\n]*}?.*\n/g
12+
13+
//removes all top level imports from the md file that the old docs framework used to determine what imports are needed
14+
const withoutImports = fileContent.replace(importRegex, '')
15+
16+
//regex link: https://regexr.com/8f0bu
17+
const ExampleBlockRegex =
18+
/```[tj]s file=['"]\.?\/?(\w*)\.(\w*)['"]\s*\n```/g
19+
20+
//the first capture group is the example file name without the extension or path, the second is the extension
21+
const replacementString = `\nimport $1 from "./$1.$2?raw"\n\n<LiveExample src={$1} />`
22+
const examplesConvertedToMDX = withoutImports.replace(
23+
ExampleBlockRegex,
24+
replacementString,
25+
)
26+
27+
//we want to strip the nolive/noLive tags from codeblocks as that was custom to the old docs framework
28+
const noLiveRemoved = examplesConvertedToMDX.replace(/```no[lL]ive/g, '```')
29+
30+
await writeFile(file + 'x', noLiveRemoved)
31+
})
32+
}

0 commit comments

Comments
 (0)