Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export markdoc object #50

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@markdoc/next.js",
"version": "0.3.6",
"version": "0.3.7",
"author": "Stripe, Inc.",
"description": "Markdoc plugin for Next.js",
"license": "MIT",
Expand Down
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',
},
},
});
});
Loading