@@ -30,13 +30,7 @@ async function gatherPartials(ast, schemaDir, tokenizer, parseOptions) {
30
30
partials = {
31
31
...partials ,
32
32
[ 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 ) ) ,
40
34
} ;
41
35
}
42
36
}
@@ -63,6 +57,7 @@ async function load(source) {
63
57
} ,
64
58
nextjsExports = [ 'metadata' , 'revalidate' ] ,
65
59
appDir = false ,
60
+ pagesDir,
66
61
} = this . getOptions ( ) || { } ;
67
62
68
63
const tokenizer = new Markdoc . Tokenizer ( options ) ;
@@ -72,6 +67,8 @@ async function load(source) {
72
67
const tokens = tokenizer . tokenize ( source ) ;
73
68
const ast = Markdoc . parse ( tokens , parseOptions ) ;
74
69
70
+ const isPage = this . resourcePath . startsWith ( appDir || pagesDir ) ;
71
+
75
72
// Grabs the path of the file relative to the `/{app,pages}` directory
76
73
// to pass into the app props later.
77
74
// This array access @ index 1 is safe since Next.js guarantees that
@@ -88,8 +85,7 @@ async function load(source) {
88
85
) ;
89
86
90
87
// IDEA: consider making this an option per-page
91
- const dataFetchingFunction =
92
- mode === 'server' ? 'getServerSideProps' : 'getStaticProps' ;
88
+ const dataFetchingFunction = mode === 'server' ? 'getServerSideProps' : 'getStaticProps' ;
93
89
94
90
let schemaCode = 'const schema = {};' ;
95
91
try {
@@ -138,18 +134,14 @@ import yaml from 'js-yaml';
138
134
// renderers is imported separately so Markdoc isn't sent to the client
139
135
import Markdoc, {renderers} from '@markdoc/markdoc'
140
136
141
- import {getSchema, defaultObject} from '${ normalize (
142
- await resolve ( __dirname , './runtime' )
143
- ) } ';
137
+ import {getSchema, defaultObject} from '${ normalize ( await resolve ( __dirname , './runtime' ) ) } ';
144
138
/**
145
139
* Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
146
140
* This enables typescript/ESnext support
147
141
*/
148
142
${ schemaCode }
149
143
150
- const tokenizer = new Markdoc.Tokenizer(${
151
- options ? JSON . stringify ( options ) : ''
152
- } );
144
+ const tokenizer = new Markdoc.Tokenizer(${ options ? JSON . stringify ( options ) : '' } );
153
145
154
146
/**
155
147
* Source will never change at runtime, so parse happens at the file root
@@ -170,7 +162,7 @@ const frontmatter = ast.attributes.frontmatter
170
162
171
163
const {components, ...rest} = getSchema(schema)
172
164
173
- async function getMarkdocData(context = {}) {
165
+ ${ isPage ? ' async ' : '' } function getMarkdocData(context = {}) {
174
166
const partials = ${ JSON . stringify ( partials ) } ;
175
167
176
168
// Ensure Node.transformChildren is available
@@ -196,7 +188,7 @@ async function getMarkdocData(context = {}) {
196
188
* transform must be called in dataFetchingFunction to support server-side rendering while
197
189
* accessing variables on the server
198
190
*/
199
- const content = await Markdoc.transform(ast, cfg);
191
+ const content = ${ isPage ? ' await ' : '' } Markdoc.transform(ast, cfg);
200
192
201
193
// Removes undefined
202
194
return JSON.parse(
@@ -211,7 +203,7 @@ async function getMarkdocData(context = {}) {
211
203
}
212
204
213
205
${
214
- appDir
206
+ appDir || ! isPage
215
207
? ''
216
208
: `export async function ${ dataFetchingFunction } (context) {
217
209
return {
221
213
};
222
214
}`
223
215
}
224
- ${ appDir ? nextjsExportsCode : '' }
216
+ ${ appDir && isPage ? nextjsExportsCode : '' }
225
217
export 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
+ } ;
228
222
// Only execute HMR code in development
229
223
return renderers.react(markdoc.content, React, {
230
224
components: {
0 commit comments