11import ts from 'typescript' ;
2- import { getPropertyValues } from '../../typescript/ast-helpers.js' ;
2+ import { getDefaultImportName , getImportMap , getPropertyValues , stripQuotes } from '../../typescript/ast-helpers.js' ;
33
44export const getPageExtensions = ( sourceFile : ts . SourceFile ) => {
55 const pageExtensions : Set < string > = new Set ( ) ;
@@ -17,3 +17,50 @@ export const getPageExtensions = (sourceFile: ts.SourceFile) => {
1717
1818 return Array . from ( pageExtensions ) ;
1919} ;
20+
21+ const isNamedProp = ( prop : ts . ObjectLiteralElementLike , name : string ) =>
22+ ts . isPropertyAssignment ( prop ) && prop . name . getText ( ) === name ;
23+
24+ export const getMdxPlugins = ( sourceFile : ts . SourceFile ) => {
25+ const plugins = new Set < string > ( ) ;
26+ const importMap = getImportMap ( sourceFile ) ;
27+ const mdxImportName = getDefaultImportName ( importMap , '@next/mdx' ) ;
28+
29+ if ( ! mdxImportName ) return plugins ;
30+
31+ function visit ( node : ts . Node ) : boolean {
32+ if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) && node . expression . text === mdxImportName ) {
33+ if ( node . arguments . length > 0 && ts . isObjectLiteralExpression ( node . arguments [ 0 ] ) ) {
34+ const options = node . arguments [ 0 ] ?. properties . find ( prop => isNamedProp ( prop , 'options' ) ) ;
35+ if ( options && ts . isPropertyAssignment ( options ) ) {
36+ if ( ts . isObjectLiteralExpression ( options . initializer ) ) {
37+ for ( const pluginType of [ 'remarkPlugins' , 'rehypePlugins' , 'recmaPlugins' ] ) {
38+ const props = options . initializer . properties . find ( prop => isNamedProp ( prop , pluginType ) ) ;
39+ if ( props && ts . isPropertyAssignment ( props ) ) {
40+ if ( ts . isArrayLiteralExpression ( props . initializer ) ) {
41+ for ( const element of props . initializer . elements ) {
42+ if ( ts . isStringLiteral ( element ) ) {
43+ plugins . add ( stripQuotes ( element . text ) ) ;
44+ } else if ( ts . isArrayLiteralExpression ( element ) && element . elements . length > 0 ) {
45+ const firstElement = element . elements [ 0 ] ;
46+ if ( ts . isStringLiteral ( firstElement ) ) {
47+ plugins . add ( stripQuotes ( firstElement . text ) ) ;
48+ }
49+ }
50+ }
51+ }
52+ }
53+ }
54+ }
55+ }
56+ }
57+ return true ;
58+ }
59+
60+ return ts . forEachChild ( node , visit ) ?? false ;
61+ }
62+
63+ visit ( sourceFile ) ;
64+
65+ return plugins ;
66+ } ;
0 commit comments