@@ -5,6 +5,8 @@ import { parsers as babelParsers } from 'prettier/plugins/babel';
5
5
import { parsers as htmlParsers } from 'prettier/plugins/html' ;
6
6
import { parsers as typescriptParsers } from 'prettier/plugins/typescript' ;
7
7
8
+ const EOL = '\n' ;
9
+
8
10
const addon = {
9
11
parseBabel : ( text : string , options : ParserOptions ) => babelParsers . babel . parse ( text , options ) ,
10
12
parseTypescript : ( text : string , options : ParserOptions ) =>
@@ -23,6 +25,37 @@ function transformParser(
23
25
text : string ,
24
26
options : ParserOptions & ThisPluginOptions ,
25
27
) : Promise < FormattedTextAST > => {
28
+ if ( options . parentParser === 'markdown' || options . parentParser === 'mdx' ) {
29
+ let codeblockStart = '```' ;
30
+ const codeblockEnd = '```' ;
31
+
32
+ if ( options . parser === 'babel' ) {
33
+ codeblockStart = '```jsx' ;
34
+ } else if ( options . parser === 'typescript' ) {
35
+ codeblockStart = '```tsx' ;
36
+ }
37
+
38
+ const formattedCodeblock = await format (
39
+ `${ codeblockStart } ${ EOL } ${ text } ${ EOL } ${ codeblockEnd } ` ,
40
+ {
41
+ ...options ,
42
+ plugins : [ ] ,
43
+ rangeEnd : Infinity ,
44
+ endOfLine : 'lf' ,
45
+ parser : options . parentParser ,
46
+ parentParser : undefined ,
47
+ } ,
48
+ ) ;
49
+ const formattedText = formattedCodeblock
50
+ . trim ( )
51
+ . slice ( `${ codeblockStart } ${ EOL } ` . length , - `${ EOL } ${ codeblockEnd } ` . length ) ;
52
+
53
+ return {
54
+ type : 'FormattedText' ,
55
+ body : formattedText ,
56
+ } ;
57
+ }
58
+
26
59
const plugins = options . plugins . filter ( ( plugin ) => typeof plugin !== 'string' ) as Plugin [ ] ;
27
60
28
61
let languageImplementedPlugin : Plugin | undefined ;
0 commit comments