@@ -285,3 +285,94 @@ export default function MarkdocComponent(props) {
285
285
}
286
286
"
287
287
` ;
288
+
289
+ exports [` import as frontend component 1` ] = `
290
+ "import React from 'react';
291
+ import yaml from 'js-yaml';
292
+ // renderers is imported separately so Markdoc isn't sent to the client
293
+ import Markdoc, { renderers } from '@markdoc/markdoc'
294
+
295
+ import { getSchema , defaultObject } from './src/runtime.js';
296
+ /**
297
+ * Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
298
+ * This enables typescript/ESnext support
299
+ */
300
+ const schema = { } ;
301
+
302
+ const tokenizer = new Markdoc.Tokenizer({ \\" allowComments\\ " :true } );
303
+
304
+ /**
305
+ * Source will never change at runtime, so parse happens at the file root
306
+ */
307
+ const source = \\ "---\\\\ ntitle: Custom title\\\\ n---\\\\ n\\\\ n# { % $markdoc .frontmatter .title % } \\\\ n\\\\ n{ % tag /% } \\\\ n\\ ";
308
+ const filepath = undefined;
309
+ const tokens = tokenizer.tokenize(source);
310
+ const parseOptions = { \\" slots\\ " :false } ;
311
+ const ast = Markdoc.parse(tokens, parseOptions);
312
+
313
+ /**
314
+ * Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
315
+ * This unblocks future features, such a per-page dataFetchingFunction.
316
+ */
317
+ const frontmatter = ast.attributes.frontmatter
318
+ ? yaml.load(ast.attributes.frontmatter)
319
+ : { } ;
320
+
321
+ const { components , ... rest } = getSchema(schema)
322
+
323
+ function getMarkdocData(context = { } ) {
324
+ const partials = {};
325
+
326
+ // Ensure Node.transformChildren is available
327
+ Object .keys (partials ).forEach ((key ) => {
328
+ const tokens = tokenizer .tokenize (partials [key ]);
329
+ partials [key ] = Markdoc .parse (tokens , parseOptions );
330
+ });
331
+
332
+ const cfg = {
333
+ ... rest ,
334
+ variables: {
335
+ ... (rest ? rest .variables : {}),
336
+ // user can't override this namespace
337
+ markdoc: {frontmatter },
338
+ // Allows users to eject from Markdoc rendering and pass in dynamic variables via getServerSideProps
339
+ ... (context .variables || {})
340
+ },
341
+ partials ,
342
+ source ,
343
+ };
344
+
345
+ /**
346
+ * transform must be called in dataFetchingFunction to support server-side rendering while
347
+ * accessing variables on the server
348
+ */
349
+ const content = Markdoc .transform (ast , cfg );
350
+
351
+ // Removes undefined
352
+ return JSON .parse (
353
+ JSON .stringify ({
354
+ content ,
355
+ frontmatter ,
356
+ file: {
357
+ path: filepath ,
358
+ },
359
+ })
360
+ );
361
+ }
362
+
363
+
364
+
365
+ export const markdoc = { frontmatter } ;
366
+ export default function MarkdocComponent(props) {
367
+ const markdoc = getMarkdocData ();
368
+ // Only execute HMR code in development
369
+ return renderers .react (markdoc .content , React , {
370
+ components: {
371
+ ... components ,
372
+ // Allows users to override default components at runtime, via their _app
373
+ ... props .components ,
374
+ },
375
+ });
376
+ }
377
+ "
378
+ ` ;
0 commit comments