Skip to content

Commit

Permalink
export markdoc object
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix-stripe committed Dec 20, 2023
1 parent 85085df commit 7743fe7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ ${
}`
}
${appDir ? nextjsExportsCode : ''}
export const markdoc = {frontmatter};
export default${appDir ? ' async' : ''} function MarkdocComponent(props) {
const markdoc = ${appDir ? 'await getMarkdocData()' : 'props.markdoc'};
// Only execute HMR code in development
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function getStaticProps(context) {
};
}
export const markdoc = {frontmatter};
export default function MarkdocComponent(props) {
const markdoc = props.markdoc;
// Only execute HMR code in development
Expand Down Expand Up @@ -173,6 +174,7 @@ async function getMarkdocData(context = {}) {
export const metadata = frontmatter.nextjs?.metadata;
export const revalidate = frontmatter.nextjs?.revalidate;
export const markdoc = {frontmatter};
export default async function MarkdocComponent(props) {
const markdoc = await getMarkdocData();
// Only execute HMR code in development
Expand Down Expand Up @@ -269,6 +271,7 @@ export async function getStaticProps(context) {
};
}
export const markdoc = {frontmatter};
export default function MarkdocComponent(props) {
const markdoc = props.markdoc;
// Only execute HMR code in development
Expand Down
23 changes: 20 additions & 3 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ test('file output is correct', async () => {
expect(evaluate(output)).toEqual({
default: expect.any(Function),
getStaticProps: expect.any(Function),
markdoc: {
frontmatter: {
title: 'Custom title',
},
},
});

const data = await page.getStaticProps({});
Expand Down Expand Up @@ -158,14 +163,19 @@ test('file output is correct', async () => {
});

test('app router', async () => {
const output = await callLoader(options({ appDir: true }), source);
const output = await callLoader(options({appDir: true}), source);

expect(normalizeOperatingSystemPaths(output)).toMatchSnapshot();

const page = evaluate(output);

expect(evaluate(output)).toEqual({
default: expect.any(Function),
markdoc: {
frontmatter: {
title: 'Custom title',
},
},
});

expect(await page.default({})).toEqual(
Expand All @@ -179,11 +189,13 @@ test('app router', async () => {

test('app router metadata', async () => {
const output = await callLoader(
options({ appDir: true }),
options({appDir: true}),
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 Down Expand Up @@ -248,5 +260,10 @@ test('mode="server"', async () => {
expect(evaluate(output)).toEqual({
default: expect.any(Function),
getServerSideProps: expect.any(Function),
markdoc: {
frontmatter: {
title: 'Custom title',
},
},
});
});

0 comments on commit 7743fe7

Please sign in to comment.