Skip to content

Commit a43ab11

Browse files
Jeroen Langeveldtaniki
authored andcommitted
green: Use gray-matter to handle the frontmatter
1 parent 1c46482 commit a43ab11

4 files changed

Lines changed: 144 additions & 92 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"tsconfig.json"
6363
],
6464
"dependencies": {
65-
"pug-lexer": "^5.0.1"
65+
"gray-matter": "^4.0.3",
66+
"pug-lexer": "^5.0.0"
6667
},
6768
"devDependencies": {
6869
"@eslint/compat": "1.4.1",

pnpm-lock.yaml

Lines changed: 119 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import matter = require('gray-matter');
12
import type {
23
Parser,
34
ParserOptions,
@@ -20,6 +21,7 @@ import { PugPrinter } from './printer';
2021
interface 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
},

src/printer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { types } from 'node:util';
1+
import matter = require('gray-matter');
22
import type { BuiltInParserName, Options, RequiredOptions } from 'prettier';
33
import { format } from 'prettier';
44
import type PrettierAngularPlugin from 'prettier/plugins/angular';
@@ -242,7 +242,9 @@ export class PugPrinter {
242242
private readonly content: string,
243243
private tokens: Token[],
244244
private readonly options: PugPrinterOptions,
245+
private frontmatter: matter.GrayMatterFile<string>,
245246
) {
247+
this.frontmatter = frontmatter;
246248
this.indentString = options.pugUseTabs
247249
? '\t'
248250
: ' '.repeat(options.pugTabWidth);
@@ -389,8 +391,7 @@ export class PugPrinter {
389391

390392
token = this.getNextToken();
391393
}
392-
393-
return results.join('');
394+
return matter.stringify(results.join(''), this.frontmatter.data);
394395
}
395396

396397
private getNextToken(): Token | null {

0 commit comments

Comments
 (0)