Skip to content

Commit

Permalink
pass slots through to all parse calls (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix-stripe authored Sep 1, 2023
1 parent db73338 commit cc26718
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
30 changes: 18 additions & 12 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function normalize(s) {
return s.replace(/\\/g, path.win32.sep.repeat(2));
}

async function gatherPartials(ast, schemaDir, tokenizer) {
async function gatherPartials(ast, schemaDir, tokenizer, parseOptions) {
let partials = {};

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

if (content) {
const tokens = tokenizer.tokenize(content);
const ast = Markdoc.parse(tokens);
const ast = Markdoc.parse(tokens, parseOptions);
partials = {
...partials,
[file]: content,
...(await gatherPartials.call(this, ast, schemaDir, tokenizer)),
...(await gatherPartials.call(
this,
ast,
schemaDir,
tokenizer,
parseOptions
)),
};
}
}
Expand Down Expand Up @@ -59,10 +65,11 @@ async function load(source) {
} = this.getOptions() || {};

const tokenizer = new Markdoc.Tokenizer(options);
const parseOptions = {slots};

const schemaDir = path.resolve(dir, schemaPath || DEFAULT_SCHEMA_PATH);
const tokens = tokenizer.tokenize(source);
const ast = Markdoc.parse(tokens, {slots});
const ast = Markdoc.parse(tokens, parseOptions);

// Grabs the path of the file relative to the `/{app,pages}` directory
// to pass into the app props later.
Expand All @@ -75,7 +82,8 @@ async function load(source) {
this,
ast,
path.resolve(schemaDir, 'partials'),
tokenizer
tokenizer,
parseOptions
);

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

this.addContextDependency(schemaDir);

const nextjsExports = [
'metadata',
'revalidate',
]
const nextjsExports = ['metadata', 'revalidate'];
const nextjsExportsCode = nextjsExports
.map((name) => `export const ${name} = frontmatter.nextjs?.${name};`)
.join('\n')
.join('\n');

const result = `import React from 'react';
import yaml from 'js-yaml';
Expand All @@ -152,7 +157,8 @@ const tokenizer = new Markdoc.Tokenizer(${
const source = ${JSON.stringify(source)};
const filepath = ${JSON.stringify(filepath)};
const tokens = tokenizer.tokenize(source);
const ast = Markdoc.parse(tokens, {slots: ${JSON.stringify(slots)}});
const parseOptions = ${JSON.stringify(parseOptions)};
const ast = Markdoc.parse(tokens, parseOptions);
/**
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
Expand All @@ -170,7 +176,7 @@ async function getMarkdocData(context = {}) {
// Ensure Node.transformChildren is available
Object.keys(partials).forEach((key) => {
const tokens = tokenizer.tokenize(partials[key]);
partials[key] = Markdoc.parse(tokens);
partials[key] = Markdoc.parse(tokens, parseOptions);
});
const cfg = {
Expand Down
15 changes: 9 additions & 6 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
const filepath = \\"/test/index.md\\";
const tokens = tokenizer.tokenize(source);
const ast = Markdoc.parse(tokens, {slots: false});
const parseOptions = {\\"slots\\":false};
const ast = Markdoc.parse(tokens, parseOptions);
/**
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
Expand All @@ -39,7 +40,7 @@ async function getMarkdocData(context = {}) {
// Ensure Node.transformChildren is available
Object.keys(partials).forEach((key) => {
const tokens = tokenizer.tokenize(partials[key]);
partials[key] = Markdoc.parse(tokens);
partials[key] = Markdoc.parse(tokens, parseOptions);
});
const cfg = {
Expand Down Expand Up @@ -116,7 +117,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
const filepath = undefined;
const tokens = tokenizer.tokenize(source);
const ast = Markdoc.parse(tokens, {slots: false});
const parseOptions = {\\"slots\\":false};
const ast = Markdoc.parse(tokens, parseOptions);
/**
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
Expand All @@ -134,7 +136,7 @@ async function getMarkdocData(context = {}) {
// Ensure Node.transformChildren is available
Object.keys(partials).forEach((key) => {
const tokens = tokenizer.tokenize(partials[key]);
partials[key] = Markdoc.parse(tokens);
partials[key] = Markdoc.parse(tokens, parseOptions);
});
const cfg = {
Expand Down Expand Up @@ -206,7 +208,8 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true});
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
const filepath = \\"/test/index.md\\";
const tokens = tokenizer.tokenize(source);
const ast = Markdoc.parse(tokens, {slots: false});
const parseOptions = {\\"slots\\":false};
const ast = Markdoc.parse(tokens, parseOptions);
/**
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
Expand All @@ -224,7 +227,7 @@ async function getMarkdocData(context = {}) {
// Ensure Node.transformChildren is available
Object.keys(partials).forEach((key) => {
const tokens = tokenizer.tokenize(partials[key]);
partials[key] = Markdoc.parse(tokens);
partials[key] = Markdoc.parse(tokens, parseOptions);
});
const cfg = {
Expand Down

0 comments on commit cc26718

Please sign in to comment.