-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
core.js
160 lines (145 loc) · 5.26 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('mdast').Root} Root
* @typedef {import('remark-rehype').Options} RemarkRehypeOptions
* @typedef {import('unified').PluggableList} PluggableList
* @typedef {import('unified').Processor<Root, Program, Program, Program, string>} Processor
* @typedef {import('./plugin/recma-document.js').Options} RecmaDocumentOptions
* @typedef {import('./plugin/recma-jsx-rewrite.js').Options} RecmaJsxRewriteOptions
* @typedef {import('./plugin/recma-stringify.js').Options} RecmaStringifyOptions
* @typedef {import('./plugin/rehype-recma.js').Options} RehypeRecmaOptions
*/
/**
* @typedef BaseProcessorOptions
* Base configuration.
* @property {boolean | null | undefined} [jsx=false]
* Whether to keep JSX (default: `false`).
* @property {'md' | 'mdx' | null | undefined} [format='mdx']
* Format of the files to be processed (default: `'mdx'`).
* @property {'function-body' | 'program'} [outputFormat='program']
* Whether to compile to a whole program or a function body (default:
* `'program'`).
* @property {ReadonlyArray<string> | null | undefined} [mdExtensions]
* Extensions (with `.`) for markdown (default: `['.md', '.markdown', …]`).
* @property {ReadonlyArray<string> | null | undefined} [mdxExtensions]
* Extensions (with `.`) for MDX (default: `['.mdx']`).
* @property {PluggableList | null | undefined} [recmaPlugins]
* List of recma (esast, JavaScript) plugins (optional).
* @property {PluggableList | null | undefined} [remarkPlugins]
* List of remark (mdast, markdown) plugins (optional).
* @property {PluggableList | null | undefined} [rehypePlugins]
* List of rehype (hast, HTML) plugins (optional).
* @property {Readonly<RemarkRehypeOptions> | null | undefined} [remarkRehypeOptions]
* Options to pass through to `remark-rehype` (optional).
*
* @typedef {Omit<RecmaDocumentOptions & RecmaJsxRewriteOptions & RecmaStringifyOptions & RehypeRecmaOptions, 'outputFormat'>} PluginOptions
* Configuration for internal plugins.
*
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
* Configuration for processor.
*/
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
import {recmaDocument} from './plugin/recma-document.js'
import {recmaJsxBuild} from './plugin/recma-jsx-build.js'
import {recmaJsxRewrite} from './plugin/recma-jsx-rewrite.js'
import {recmaStringify} from './plugin/recma-stringify.js'
import {rehypeRecma} from './plugin/rehype-recma.js'
import {rehypeRemoveRaw} from './plugin/rehype-remove-raw.js'
import {remarkMarkAndUnravel} from './plugin/remark-mark-and-unravel.js'
import {nodeTypes} from './node-types.js'
import {development as defaultDevelopment} from '#condition'
const removedOptions = [
'compilers',
'filepath',
'hastPlugins',
'mdPlugins',
'skipExport',
'wrapExport'
]
/**
* Pipeline to:
*
* 1. Parse MDX (serialized markdown with embedded JSX, ESM, and expressions)
* 2. Transform through remark (mdast), rehype (hast), and recma (esast)
* 3. Serialize as JavaScript
*
* @param {Readonly<ProcessorOptions> | null | undefined} [options]
* Configuration (optional).
* @return {Processor}
* Processor.
*/
export function createProcessor(options) {
const {
development,
jsx,
format,
outputFormat,
providerImportSource,
recmaPlugins,
rehypePlugins,
remarkPlugins,
remarkRehypeOptions,
elementAttributeNameCase,
stylePropertyNameCase,
SourceMapGenerator,
...rest
} = options || {}
const dev =
development === null || development === undefined
? defaultDevelopment
: development
let index = -1
while (++index < removedOptions.length) {
const key = removedOptions[index]
if (options && key in options) {
throw new Error(
'`options.' +
key +
'` is no longer supported. Please see <https://mdxjs.com/migrating/v2/> for more information'
)
}
}
// @ts-expect-error: throw an error for a runtime value which is not allowed
// by the types.
if (format === 'detect') {
throw new Error(
"Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format"
)
}
const pipeline = unified().use(remarkParse)
if (format !== 'md') {
pipeline.use(remarkMdx)
}
const extraNodeTypes = remarkRehypeOptions
? remarkRehypeOptions.passThrough || []
: []
pipeline
.use(remarkMarkAndUnravel)
.use(remarkPlugins || [])
.use(remarkRehype, {
...remarkRehypeOptions,
allowDangerousHtml: true,
passThrough: [...extraNodeTypes, ...nodeTypes]
})
.use(rehypePlugins || [])
if (format === 'md') {
pipeline.use(rehypeRemoveRaw)
}
pipeline
.use(rehypeRecma, {elementAttributeNameCase, stylePropertyNameCase})
.use(recmaDocument, {...rest, outputFormat})
.use(recmaJsxRewrite, {
development: dev,
providerImportSource,
outputFormat
})
if (!jsx) {
pipeline.use(recmaJsxBuild, {development: dev, outputFormat})
}
pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || [])
// @ts-expect-error: we added plugins with if-checks, which TS doesn’t get.
return pipeline
}