From cc267184945342aa93dd443a4e1ed86083153cbb Mon Sep 17 00:00:00 2001 From: Mike Fix <62121649+mfix-stripe@users.noreply.github.com> Date: Fri, 1 Sep 2023 09:36:56 -0700 Subject: [PATCH] pass slots through to all parse calls (#38) --- src/loader.js | 30 +++++++++++++++----------- tests/__snapshots__/index.test.js.snap | 15 +++++++------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/loader.js b/src/loader.js index 56904dd..1ab497d 100644 --- a/src/loader.js +++ b/src/loader.js @@ -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()) { @@ -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 + )), }; } } @@ -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. @@ -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 @@ -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'; @@ -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. @@ -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 = { diff --git a/tests/__snapshots__/index.test.js.snap b/tests/__snapshots__/index.test.js.snap index dbdd179..df11526 100644 --- a/tests/__snapshots__/index.test.js.snap +++ b/tests/__snapshots__/index.test.js.snap @@ -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. @@ -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 = { @@ -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. @@ -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 = { @@ -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. @@ -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 = {