1- import { Printer , doc } from 'prettier' ;
1+ import { doc } from 'prettier' ;
2+ import type { Printer as Printer2 } from 'prettier' ;
3+ import type { Doc as Doc3 , Printer as Printer3 } from 'prettier3' ;
24import { RawMarkupKinds } from '~/parser' ;
35import { LiquidHtmlNode , LiquidParserOptions , NodeTypes } from '~/types' ;
46
@@ -13,7 +15,10 @@ export const ParserMap: { [key in RawMarkupKinds]: string | null } = {
1315 [ RawMarkupKinds . text ] : null ,
1416} ;
1517
16- export const embed : Printer < LiquidHtmlNode > [ 'embed' ] = (
18+ // Prettier 2 and 3 have a slightly different API for embed.
19+ //
20+ // https://github.com/prettier/prettier/wiki/How-to-migrate-my-plugin-to-support-Prettier-v3%3F
21+ export const embed2 : Printer2 < LiquidHtmlNode > [ 'embed' ] = (
1722 path ,
1823 _print ,
1924 textToDoc ,
@@ -27,7 +32,8 @@ export const embed: Printer<LiquidHtmlNode>['embed'] = (
2732 return doc . utils . stripTrailingHardline (
2833 textToDoc ( node . value , {
2934 ...options ,
30- singleQuote : ( options as LiquidParserOptions ) . embeddedSingleQuote ,
35+ singleQuote : ( options as any as LiquidParserOptions )
36+ . embeddedSingleQuote ,
3137 parser,
3238 __embeddedInHtml : true ,
3339 } ) ,
@@ -38,3 +44,26 @@ export const embed: Printer<LiquidHtmlNode>['embed'] = (
3844 return null ;
3945 }
4046} ;
47+
48+ export const embed3 : Printer3 < LiquidHtmlNode > [ 'embed' ] = ( path , options ) => {
49+ return ( textToDoc ) => {
50+ const node = path . node as LiquidHtmlNode ;
51+ switch ( node . type ) {
52+ case NodeTypes . RawMarkup : {
53+ const parser = ParserMap [ node . kind ] ;
54+ if ( parser && node . value . trim ( ) !== '' ) {
55+ return textToDoc ( node . value , {
56+ ...options ,
57+ singleQuote : ( options as LiquidParserOptions ) . embeddedSingleQuote ,
58+ parser,
59+ __embeddedInHtml : true ,
60+ } ) . then ( ( document ) =>
61+ doc . utils . stripTrailingHardline ( document ) ,
62+ ) as Promise < Doc3 > ;
63+ }
64+ }
65+ default :
66+ return undefined ;
67+ }
68+ } ;
69+ } ;
0 commit comments