@@ -78,12 +78,9 @@ Add some code in `example.js` to compile `example.mdx` to JavaScript:
78
78
import {promises as fs } from ' node:fs'
79
79
import {compile } from ' @mdx-js/mdx'
80
80
81
- main ( )
81
+ const compiled = await compile ( await fs . readFile ( ' example.mdx ' ) )
82
82
83
- async function main () {
84
- const compiled = await compile (await fs .readFile (' example.mdx' ))
85
- console .log (String (compiled))
86
- }
83
+ console .log (String (compiled))
87
84
```
88
85
89
86
Yields roughly:
@@ -285,12 +282,10 @@ A module `example.js`:
285
282
``` js
286
283
import {compile } from ' @mdx-js/mdx'
287
284
288
- main ( ' export const no = 3.14\n\n # hi {no}' )
285
+ const code = ' export const no = 3.14\n\n # hi {no}'
289
286
290
- async function main (code ) {
291
- console .log (String (await compile (code, {outputFormat: ' program' }))) // Default
292
- console .log (String (await compile (code, {outputFormat: ' function-body' })))
293
- }
287
+ console .log (String (await compile (code, {outputFormat: ' program' }))) // Default
288
+ console .log (String (await compile (code, {outputFormat: ' function-body' })))
294
289
```
295
290
296
291
…yields:
@@ -383,13 +378,10 @@ Say we have a module `example.js`:
383
378
``` js
384
379
import {compile } from ' @mdx-js/mdx'
385
380
386
- main ()
381
+ const code = ' export {number} from "./data.js"\n\n # hi'
382
+ const baseUrl = ' https://a.full/url' // Typically `import.meta.url`
387
383
388
- async function main () {
389
- const code = ' export {number} from "./data.js"\n\n # hi'
390
- const baseUrl = ' https://a.full/url' // Typically `import.meta.url`
391
- console .log (String (await compile (code, {baseUrl})))
392
- }
384
+ console .log (String (await compile (code, {baseUrl})))
393
385
```
394
386
395
387
…now running ` node example.js ` yields:
@@ -428,14 +420,11 @@ import {promises as fs} from 'node:fs'
428
420
import * as runtime from ' react/jsx-runtime'
429
421
import {evaluate } from ' @mdx-js/mdx'
430
422
431
- main ()
423
+ const path = ' example.mdx'
424
+ const value = await fs .readFile (path)
425
+ const MDXContent = (await evaluate ({path, value}, runtime)).default
432
426
433
- async function main () {
434
- const path = ' example.mdx'
435
- const value = await fs .readFile (path)
436
- const MDXContent = (await evaluate ({path, value}, runtime)).default
437
- console .log (MDXContent ())
438
- }
427
+ console .log (MDXContent ())
439
428
```
440
429
441
430
Running that would normally (production) yield:
@@ -451,14 +440,12 @@ Error: Expected component `NoteIcon` to be defined: you likely forgot to import,
451
440
But if we change add ` development: true ` to our example:
452
441
453
442
``` diff
454
- @@ -7,6 +7,6 @@ main()
455
- async function main() {
456
- const path = 'example.mdx'
457
- const value = await fs.readFile(path)
458
- - const MDXContent = (await evaluate({path, value}, runtime)).default
459
- + const MDXContent = (await evaluate({path, value}, {development: true, ...runtime})).default
460
- console.log(MDXContent({}))
461
- }
443
+ @@ -7,6 +7,6 @@
444
+ const path = 'example.mdx'
445
+ const value = await fs.readFile(path)
446
+ - const MDXContent = (await evaluate({path, value}, runtime)).default
447
+ + const MDXContent = (await evaluate({path, value}, {development: true, ...runtime})).default
448
+ console.log(MDXContent({}))
462
449
```
463
450
464
451
And we’d run it again, we’d get:
@@ -491,16 +478,12 @@ import {promises as fs} from 'node:fs'
491
478
import {SourceMapGenerator } from ' source-map'
492
479
import {compile } from ' @mdx-js/mdx'
493
480
494
- main ()
495
-
496
- async function main () {
497
- const file = await compile (
498
- {path: ' example.mdx' , value: await fs .readFile (' example.mdx' )},
499
- {SourceMapGenerator}
500
- )
481
+ const file = await compile (
482
+ {path: ' example.mdx' , value: await fs .readFile (' example.mdx' )},
483
+ {SourceMapGenerator}
484
+ )
501
485
502
- console .log (file .map )
503
- }
486
+ console .log (file .map )
504
487
```
505
488
506
489
…yields:
@@ -724,12 +707,9 @@ import remarkPresetLintConsistent from 'remark-preset-lint-consistent' // Lint r
724
707
import {reporter } from ' vfile-reporter'
725
708
import {compile } from ' @mdx-js/mdx'
726
709
727
- main ( )
710
+ const file = await compile ( ' *like this* or _like this_? ' , {remarkPlugins : [remarkPresetLintConsistent]} )
728
711
729
- async function main () {
730
- const file = await compile (' *like this* or _like this_?' , {remarkPlugins: [remarkPresetLintConsistent]})
731
- console .error (reporter (file))
732
- }
712
+ console .error (reporter (file))
733
713
```
734
714
735
715
Yields:
0 commit comments