diff --git a/src/loader.js b/src/loader.js index 245dd19..9c654ed 100644 --- a/src/loader.js +++ b/src/loader.js @@ -164,7 +164,7 @@ const ast = Markdoc.parse(tokens, parseOptions); * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. * This unblocks future features, such a per-page dataFetchingFunction. */ -const frontmatter = ast.attributes.frontmatter +export const frontmatter = ast.attributes.frontmatter ? yaml.load(ast.attributes.frontmatter) : {}; diff --git a/tests/__snapshots__/index.test.js.snap b/tests/__snapshots__/index.test.js.snap index 6e25b4e..d660083 100644 --- a/tests/__snapshots__/index.test.js.snap +++ b/tests/__snapshots__/index.test.js.snap @@ -28,7 +28,7 @@ const ast = Markdoc.parse(tokens, parseOptions); * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. * This unblocks future features, such a per-page dataFetchingFunction. */ -const frontmatter = ast.attributes.frontmatter +export const frontmatter = ast.attributes.frontmatter ? yaml.load(ast.attributes.frontmatter) : {}; @@ -125,7 +125,7 @@ const ast = Markdoc.parse(tokens, parseOptions); * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. * This unblocks future features, such a per-page dataFetchingFunction. */ -const frontmatter = ast.attributes.frontmatter +export const frontmatter = ast.attributes.frontmatter ? yaml.load(ast.attributes.frontmatter) : {}; @@ -217,7 +217,7 @@ const ast = Markdoc.parse(tokens, parseOptions); * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. * This unblocks future features, such a per-page dataFetchingFunction. */ -const frontmatter = ast.attributes.frontmatter +export const frontmatter = ast.attributes.frontmatter ? yaml.load(ast.attributes.frontmatter) : {}; diff --git a/tests/index.test.js b/tests/index.test.js index 6f2fef3..a4972ed 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -77,9 +77,7 @@ function options(config = {}) { const resolve = enhancedResolve.create(options); return async (context, file) => new Promise((res, rej) => - resolve(context, file, (err, result) => - err ? rej(err) : res(result) - ) + resolve(context, file, (err, result) => (err ? rej(err) : res(result))) ).then(normalizeAbsolutePath); }, resourcePath: '/Users/someone/a-next-js-repo/pages/test/index.md', @@ -102,15 +100,13 @@ async function callLoader(config, source) { } test('should not fail build if default `schemaPath` is used', async () => { - await expect(callLoader(options(), source)).resolves.toEqual( - expect.any(String) - ); + await expect(callLoader(options(), source)).resolves.toEqual(expect.any(String)); }); test('should fail build if invalid `schemaPath` is used', async () => { - await expect( - callLoader(options({schemaPath: 'unknown_schema_path'}), source) - ).rejects.toThrow("Cannot find module 'unknown_schema_path'"); + await expect(callLoader(options({schemaPath: 'unknown_schema_path'}), source)).rejects.toThrow( + "Cannot find module 'unknown_schema_path'" + ); }); test('file output is correct', async () => { @@ -128,6 +124,9 @@ test('file output is correct', async () => { title: 'Custom title', }, }, + frontmatter: { + title: 'Custom title', + }, }); const data = await page.getStaticProps({}); @@ -154,11 +153,7 @@ test('file output is correct', async () => { }); expect(page.default(data.props)).toEqual( - React.createElement( - 'article', - undefined, - React.createElement('h1', undefined, 'Custom title') - ) + React.createElement('article', undefined, React.createElement('h1', undefined, 'Custom title')) ); }); @@ -176,14 +171,13 @@ test('app router', async () => { title: 'Custom title', }, }, + frontmatter: { + title: 'Custom title', + }, }); expect(await page.default({})).toEqual( - React.createElement( - 'article', - undefined, - React.createElement('h1', undefined, 'Custom title') - ) + React.createElement('article', undefined, React.createElement('h1', undefined, 'Custom title')) ); }); @@ -193,9 +187,7 @@ test('app router metadata', async () => { source.replace('---', '---\nmetadata:\n title: Metadata title') ); - expect(output).toContain( - 'export const metadata = frontmatter.nextjs?.metadata;' - ); + expect(output).toContain('export const metadata = frontmatter.nextjs?.metadata;'); }); test.each([ @@ -211,9 +203,7 @@ test.each([ const page = evaluate(output); const data = await page.getStaticProps({}); - expect(data.props.markdoc.content.children[0].children[0]).toEqual( - 'Custom title' - ); + expect(data.props.markdoc.content.children[0].children[0]).toEqual('Custom title'); expect(data.props.markdoc.content.children[1]).toEqual(expectedChild); }); @@ -265,5 +255,8 @@ test('mode="server"', async () => { title: 'Custom title', }, }, + frontmatter: { + title: 'Custom title', + }, }); });