@@ -30,13 +30,7 @@ async function gatherPartials(ast, schemaDir, tokenizer, parseOptions) {
3030 partials = {
3131 ...partials ,
3232 [ file ] : content ,
33- ...( await gatherPartials . call (
34- this ,
35- ast ,
36- schemaDir ,
37- tokenizer ,
38- parseOptions
39- ) ) ,
33+ ...( await gatherPartials . call ( this , ast , schemaDir , tokenizer , parseOptions ) ) ,
4034 } ;
4135 }
4236 }
@@ -63,6 +57,7 @@ async function load(source) {
6357 } ,
6458 nextjsExports = [ 'metadata' , 'revalidate' ] ,
6559 appDir = false ,
60+ pagesDir,
6661 } = this . getOptions ( ) || { } ;
6762
6863 const tokenizer = new Markdoc . Tokenizer ( options ) ;
@@ -72,6 +67,8 @@ async function load(source) {
7267 const tokens = tokenizer . tokenize ( source ) ;
7368 const ast = Markdoc . parse ( tokens , parseOptions ) ;
7469
70+ const isPage = this . resourcePath . startsWith ( appDir || pagesDir ) ;
71+
7572 // Grabs the path of the file relative to the `/{app,pages}` directory
7673 // to pass into the app props later.
7774 // This array access @ index 1 is safe since Next.js guarantees that
@@ -88,8 +85,7 @@ async function load(source) {
8885 ) ;
8986
9087 // IDEA: consider making this an option per-page
91- const dataFetchingFunction =
92- mode === 'server' ? 'getServerSideProps' : 'getStaticProps' ;
88+ const dataFetchingFunction = mode === 'server' ? 'getServerSideProps' : 'getStaticProps' ;
9389
9490 let schemaCode = 'const schema = {};' ;
9591 try {
@@ -138,18 +134,14 @@ import yaml from 'js-yaml';
138134// renderers is imported separately so Markdoc isn't sent to the client
139135import Markdoc, {renderers} from '@markdoc/markdoc'
140136
141- import {getSchema, defaultObject} from '${ normalize (
142- await resolve ( __dirname , './runtime' )
143- ) } ';
137+ import {getSchema, defaultObject} from '${ normalize ( await resolve ( __dirname , './runtime' ) ) } ';
144138/**
145139 * Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
146140 * This enables typescript/ESnext support
147141 */
148142${ schemaCode }
149143
150- const tokenizer = new Markdoc.Tokenizer(${
151- options ? JSON . stringify ( options ) : ''
152- } );
144+ const tokenizer = new Markdoc.Tokenizer(${ options ? JSON . stringify ( options ) : '' } );
153145
154146/**
155147 * Source will never change at runtime, so parse happens at the file root
@@ -170,7 +162,7 @@ const frontmatter = ast.attributes.frontmatter
170162
171163const {components, ...rest} = getSchema(schema)
172164
173- async function getMarkdocData(context = {}) {
165+ ${ isPage ? ' async ' : '' } function getMarkdocData(context = {}) {
174166 const partials = ${ JSON . stringify ( partials ) } ;
175167
176168 // Ensure Node.transformChildren is available
@@ -196,7 +188,7 @@ async function getMarkdocData(context = {}) {
196188 * transform must be called in dataFetchingFunction to support server-side rendering while
197189 * accessing variables on the server
198190 */
199- const content = await Markdoc.transform(ast, cfg);
191+ const content = ${ isPage ? ' await ' : '' } Markdoc.transform(ast, cfg);
200192
201193 // Removes undefined
202194 return JSON.parse(
@@ -211,7 +203,7 @@ async function getMarkdocData(context = {}) {
211203}
212204
213205${
214- appDir
206+ appDir || ! isPage
215207 ? ''
216208 : `export async function ${ dataFetchingFunction } (context) {
217209 return {
221213 };
222214}`
223215}
224- ${ appDir ? nextjsExportsCode : '' }
216+ ${ appDir && isPage ? nextjsExportsCode : '' }
225217export const markdoc = {frontmatter};
226- export default${ appDir ? ' async' : '' } function MarkdocComponent(props) {
227- const markdoc = ${ appDir ? 'await getMarkdocData()' : 'props.markdoc' } ;
218+ export default${ appDir && isPage ? ' async' : '' } function MarkdocComponent(props) {
219+ const markdoc = ${
220+ isPage ? ( appDir ? 'await getMarkdocData()' : 'props.markdoc' ) : 'getMarkdocData()'
221+ } ;
228222 // Only execute HMR code in development
229223 return renderers.react(markdoc.content, React, {
230224 components: {
0 commit comments