Skip to content

Commit cc26718

Browse files
authored
pass slots through to all parse calls (#38)
1 parent db73338 commit cc26718

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/loader.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function normalize(s) {
88
return s.replace(/\\/g, path.win32.sep.repeat(2));
99
}
1010

11-
async function gatherPartials(ast, schemaDir, tokenizer) {
11+
async function gatherPartials(ast, schemaDir, tokenizer, parseOptions) {
1212
let partials = {};
1313

1414
for (const node of ast.walk()) {
@@ -26,11 +26,17 @@ async function gatherPartials(ast, schemaDir, tokenizer) {
2626

2727
if (content) {
2828
const tokens = tokenizer.tokenize(content);
29-
const ast = Markdoc.parse(tokens);
29+
const ast = Markdoc.parse(tokens, parseOptions);
3030
partials = {
3131
...partials,
3232
[file]: content,
33-
...(await gatherPartials.call(this, ast, schemaDir, tokenizer)),
33+
...(await gatherPartials.call(
34+
this,
35+
ast,
36+
schemaDir,
37+
tokenizer,
38+
parseOptions
39+
)),
3440
};
3541
}
3642
}
@@ -59,10 +65,11 @@ async function load(source) {
5965
} = this.getOptions() || {};
6066

6167
const tokenizer = new Markdoc.Tokenizer(options);
68+
const parseOptions = {slots};
6269

6370
const schemaDir = path.resolve(dir, schemaPath || DEFAULT_SCHEMA_PATH);
6471
const tokens = tokenizer.tokenize(source);
65-
const ast = Markdoc.parse(tokens, {slots});
72+
const ast = Markdoc.parse(tokens, parseOptions);
6673

6774
// Grabs the path of the file relative to the `/{app,pages}` directory
6875
// to pass into the app props later.
@@ -75,7 +82,8 @@ async function load(source) {
7582
this,
7683
ast,
7784
path.resolve(schemaDir, 'partials'),
78-
tokenizer
85+
tokenizer,
86+
parseOptions
7987
);
8088

8189
// IDEA: consider making this an option per-page
@@ -120,13 +128,10 @@ async function load(source) {
120128

121129
this.addContextDependency(schemaDir);
122130

123-
const nextjsExports = [
124-
'metadata',
125-
'revalidate',
126-
]
131+
const nextjsExports = ['metadata', 'revalidate'];
127132
const nextjsExportsCode = nextjsExports
128133
.map((name) => `export const ${name} = frontmatter.nextjs?.${name};`)
129-
.join('\n')
134+
.join('\n');
130135

131136
const result = `import React from 'react';
132137
import yaml from 'js-yaml';
@@ -152,7 +157,8 @@ const tokenizer = new Markdoc.Tokenizer(${
152157
const source = ${JSON.stringify(source)};
153158
const filepath = ${JSON.stringify(filepath)};
154159
const tokens = tokenizer.tokenize(source);
155-
const ast = Markdoc.parse(tokens, {slots: ${JSON.stringify(slots)}});
160+
const parseOptions = ${JSON.stringify(parseOptions)};
161+
const ast = Markdoc.parse(tokens, parseOptions);
156162
157163
/**
158164
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -170,7 +176,7 @@ async function getMarkdocData(context = {}) {
170176
// Ensure Node.transformChildren is available
171177
Object.keys(partials).forEach((key) => {
172178
const tokens = tokenizer.tokenize(partials[key]);
173-
partials[key] = Markdoc.parse(tokens);
179+
partials[key] = Markdoc.parse(tokens, parseOptions);
174180
});
175181
176182
const cfg = {

tests/__snapshots__/index.test.js.snap

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
2121
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
2222
const filepath = \\"/test/index.md\\";
2323
const tokens = tokenizer.tokenize(source);
24-
const ast = Markdoc.parse(tokens, {slots: false});
24+
const parseOptions = {\\"slots\\":false};
25+
const ast = Markdoc.parse(tokens, parseOptions);
2526
2627
/**
2728
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -39,7 +40,7 @@ async function getMarkdocData(context = {}) {
3940
// Ensure Node.transformChildren is available
4041
Object.keys(partials).forEach((key) => {
4142
const tokens = tokenizer.tokenize(partials[key]);
42-
partials[key] = Markdoc.parse(tokens);
43+
partials[key] = Markdoc.parse(tokens, parseOptions);
4344
});
4445
4546
const cfg = {
@@ -116,7 +117,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
116117
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
117118
const filepath = undefined;
118119
const tokens = tokenizer.tokenize(source);
119-
const ast = Markdoc.parse(tokens, {slots: false});
120+
const parseOptions = {\\"slots\\":false};
121+
const ast = Markdoc.parse(tokens, parseOptions);
120122
121123
/**
122124
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -134,7 +136,7 @@ async function getMarkdocData(context = {}) {
134136
// Ensure Node.transformChildren is available
135137
Object.keys(partials).forEach((key) => {
136138
const tokens = tokenizer.tokenize(partials[key]);
137-
partials[key] = Markdoc.parse(tokens);
139+
partials[key] = Markdoc.parse(tokens, parseOptions);
138140
});
139141
140142
const cfg = {
@@ -206,7 +208,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
206208
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
207209
const filepath = \\"/test/index.md\\";
208210
const tokens = tokenizer.tokenize(source);
209-
const ast = Markdoc.parse(tokens, {slots: false});
211+
const parseOptions = {\\"slots\\":false};
212+
const ast = Markdoc.parse(tokens, parseOptions);
210213
211214
/**
212215
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -224,7 +227,7 @@ async function getMarkdocData(context = {}) {
224227
// Ensure Node.transformChildren is available
225228
Object.keys(partials).forEach((key) => {
226229
const tokens = tokenizer.tokenize(partials[key]);
227-
partials[key] = Markdoc.parse(tokens);
230+
partials[key] = Markdoc.parse(tokens, parseOptions);
228231
});
229232
230233
const cfg = {

0 commit comments

Comments
 (0)