@mintlify/common
Version:
Commonly shared code within Mintlify
34 lines (33 loc) • 985 B
JavaScript
import { Parser } from 'acorn';
import jsx from 'acorn-jsx';
import { isTsxFile } from './isJsxOrTsx.js';
import { stripTsxTypes } from './stripTsxTypes.js';
const JSXParser = Parser.extend(jsx());
/**
* Get the AST for a JSX/TSX snippet
* @param jsxContent the content of the JSX/TSX snippet
* @param filePath optional file path, used to determine if TypeScript stripping is needed
* @returns the AST for the snippet
*/
export const getJsxEsmTree = (jsxContent, filePath) => {
let content = jsxContent.trim();
if (filePath && isTsxFile(filePath)) {
content = stripTsxTypes(content).trim();
}
const estree = JSXParser.parse(content, {
ecmaVersion: 'latest',
sourceType: 'module',
allowReturnOutsideFunction: true,
});
const mdxJsEsmNode = {
type: 'mdxjsEsm',
value: content,
data: {
estree,
},
};
return {
type: 'root',
children: [mdxJsEsmNode],
};
};