0.0.4 • Published 3 years ago
edoc-mdx-parser v0.0.4
edoc-mdx-parser
基于 unified 提供的一套 mdx 解析器,支持 CommonJS & ESM
Install
npm install edoc-mdx-parser
Use
import { EdocMdxParser } from 'edoc-mdx-parser';
const MDXContent = `---
title: 支持 Yaml 配置
---
## Hello, world`;
const compiler = new EdocMdxParser({
remarkPlugins: [], // 支持 remarkPlugins
rehypePlugins: [], // 支持 rehypePlugins
transformer: {
// 对任意类型节点的修改
heading: (node, index, parent, emit) => {}
},
// transformer 调用 emit 方法,会通知到这个钩子
emitListener: (name, ...values) => {},
});
// compiler.use(); // 可接入 remarkPlugin 或 rehypePlugin
// MDX => JSX
const jsxCode = compiler.md2JsxSync(MDXContent); // <h2>Hello, world</h2>
// JSX => JS(<h1></h1> => React.createElement)
const jsCode = compiler.transformJsx(jsxCode);
// jsCode => @ies/edoc-mdx-semi renderer
const renderer = compiler.js2MdxRenderer(jsCode);
console.log(compiler.yamlData); // { title: '支持 Yaml 配置' }
console.log(compiler.hast); // markdown AST