1+ import matter = require( 'gray-matter' ) ;
12import type {
23 Parser ,
34 ParserOptions ,
@@ -20,6 +21,7 @@ import { PugPrinter } from './printer';
2021interface AstPathStackEntry {
2122 content : string ;
2223 tokens : Token [ ] ;
24+ frontmatter : matter . GrayMatterFile < string > ;
2325}
2426
2527/** The plugin object that is picked up by prettier. */
@@ -42,6 +44,8 @@ export const plugin: Plugin<AstPathStackEntry> = {
4244 pug : {
4345 parse ( text , options ) {
4446 logger . debug ( '[parsers:pug:parse]:' , { text } ) ;
47+ const frontmatter = matter ( text ) ;
48+ text = frontmatter . content ;
4549
4650 let trimmedAndAlignedContent : string = text . replace ( / ^ \s * \n / , '' ) ;
4751 const contentIndentation : RegExpExecArray | null = / ^ \s * / . exec (
@@ -64,7 +68,7 @@ export const plugin: Plugin<AstPathStackEntry> = {
6468 // logger.debug('[parsers:pug:parse]: tokens', JSON.stringify(tokens, undefined, 2));
6569 // const ast: AST = parse(tokens, {});
6670 // logger.debug('[parsers:pug:parse]: ast', JSON.stringify(ast, undefined, 2));
67- return { content, tokens } ;
71+ return { content, tokens, frontmatter } ;
6872 } ,
6973
7074 astFormat : 'pug-ast' ,
@@ -93,13 +97,21 @@ export const plugin: Plugin<AstPathStackEntry> = {
9397 } ,
9498 printers : {
9599 'pug-ast' : {
96- // @ts -expect-error: Prettier allow it to be async if we don't do recursively print
97- async print ( path , options : ParserOptions & PugParserOptions ) {
98- const entry : AstPathStackEntry = path . stack [ 0 ] ! ;
99- const { content, tokens } = entry ;
100+ print (
101+ path : AstPath ,
102+ options : ParserOptions & PugParserOptions ,
103+ print : ( path : AstPath ) => Doc ,
104+ ) : Doc {
105+ const entry : AstPathStackEntry = path . stack [ 0 ] ;
106+ const { content, tokens, frontmatter } = entry ;
100107 const pugOptions : PugPrinterOptions = convergeOptions ( options ) ;
101- const printer : PugPrinter = new PugPrinter ( content , tokens , pugOptions ) ;
102- const result : string = await printer . build ( ) ;
108+ const printer : PugPrinter = new PugPrinter (
109+ content ,
110+ tokens ,
111+ pugOptions ,
112+ frontmatter ,
113+ ) ;
114+ const result : string = printer . build ( ) ;
103115 logger . debug ( '[printers:pug-ast:print]:' , result ) ;
104116 return result ;
105117 } ,
0 commit comments