1
1
/**
2
2
* @import {LoadFnOutput, LoadHook, LoadHookContext} from 'node:module'
3
+ * @import {Process} from '@mdx-js/mdx/internal-create-format-aware-processors'
3
4
* @import {CompileOptions} from '@mdx-js/mdx'
4
5
*/
5
6
14
15
* exception that the `development` option is supported based on
15
16
* whether you run Node with `--conditions development`.
16
17
* You cannot pass it manually.
18
+ *
19
+ * @typedef {[regex: RegExp, process: Process] } Settings
17
20
*/
18
21
19
22
import fs from 'node:fs/promises'
@@ -27,21 +30,24 @@ import {development as defaultDevelopment} from '#condition'
27
30
/**
28
31
* Create Node.js hooks to handle markdown and MDX.
29
32
*
30
- * @param {Readonly<Options> | null | undefined } [options ]
33
+ * @param {Readonly<Options> | null | undefined } [loaderOptions ]
31
34
* Configuration (optional).
32
35
* @returns
33
36
* Node.js hooks.
34
37
*/
35
- export function createLoader ( options ) {
36
- const options_ = options || { }
37
- const { extnames, process} = createFormatAwareProcessors ( {
38
- development : defaultDevelopment ,
39
- ...options_ ,
40
- SourceMapGenerator
41
- } )
42
- const regex = extnamesToRegex ( extnames )
38
+ export function createLoader ( loaderOptions ) {
39
+ /** @type {Settings } */
40
+ let settings = configure ( loaderOptions || { } )
43
41
44
- return { load}
42
+ return { initialize, load}
43
+
44
+ /**
45
+ *
46
+ * @param {Readonly<Options> | null | undefined } options
47
+ */
48
+ async function initialize ( options ) {
49
+ settings = configure ( { ...loaderOptions , ...options } )
50
+ }
45
51
46
52
/**
47
53
* Load `file:` URLs to MD(X) files.
@@ -58,6 +64,7 @@ export function createLoader(options) {
58
64
*/
59
65
async function load ( href , context , nextLoad ) {
60
66
const url = new URL ( href )
67
+ const [ regex , process ] = settings
61
68
62
69
if ( url . protocol === 'file:' && regex . test ( url . pathname ) ) {
63
70
const value = await fs . readFile ( url )
@@ -82,3 +89,18 @@ export function createLoader(options) {
82
89
return nextLoad ( href , context )
83
90
}
84
91
}
92
+
93
+ /**
94
+ * @param {Readonly<Options> } options
95
+ * @returns {Settings }
96
+ */
97
+ function configure ( options ) {
98
+ const { extnames, process} = createFormatAwareProcessors ( {
99
+ development : defaultDevelopment ,
100
+ ...options ,
101
+ SourceMapGenerator
102
+ } )
103
+ const regex = extnamesToRegex ( extnames )
104
+
105
+ return [ regex , process ]
106
+ }
0 commit comments