Skip to content

Commit

Permalink
export frontmatter directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix-stripe committed Aug 27, 2024
1 parent fb61eb8 commit 1760501
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: {};
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: {};
Expand Down Expand Up @@ -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)
: {};
Expand Down Expand Up @@ -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)
: {};
Expand Down
43 changes: 18 additions & 25 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 () => {
Expand All @@ -128,6 +124,9 @@ test('file output is correct', async () => {
title: 'Custom title',
},
},
frontmatter: {
title: 'Custom title',
},
});

const data = await page.getStaticProps({});
Expand All @@ -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'))
);
});

Expand All @@ -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'))
);
});

Expand All @@ -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([
Expand All @@ -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);
});

Expand Down Expand Up @@ -265,5 +255,8 @@ test('mode="server"', async () => {
title: 'Custom title',
},
},
frontmatter: {
title: 'Custom title',
},
});
});

0 comments on commit 1760501

Please sign in to comment.