mdxで特定のコンポーネント以外は消すスクリプト
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkMdx from 'remark-mdx';
import remarkStringify from 'remark-stringify';
import { visit, SKIP } from 'unist-util-visit';
import { readFile, writeFile } from 'fs/promises';
import {MdxJsxFlowElement} from 'mdast-util-mdx-jsx';
interface ParentNode extends Node {
children: Node[];
}
async function processMdxFile(filePath: string): Promise<void> {
try {
const content = await readFile(filePath, 'utf-8');
const result = await unified()
.use(remarkParse)
.use(remarkMdx)
.use(() => (tree) => {
visit(tree, 'mdxJsxFlowElement', (node: MdxJsxFlowElement, index: number, parent: ParentNode) => {
if (node.name !== 'Demo') {
parent.children.splice(index, 1);
return [SKIP, index];
}
});
})
.use(remarkStringify)
.process(content);
await writeFile(filePath, result.toString());
console.log(filePath);
} catch (error) {
console.error('error', error);
}
}
processMdxFile('src/pages/core/pin-input.mdx');