Skip to content

Commit

Permalink
Export markdoc object (#50)
Browse files Browse the repository at this point in the history
* export markdoc object

* 0.3.7
  • Loading branch information
mfix-stripe authored Jan 3, 2024
1 parent 7b2482a commit 413e446
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
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',
},
},
});
});

0 comments on commit 413e446

Please sign in to comment.