Skip to content

Commit e525db9

Browse files
committedOct 21, 2023
Refactor some errors
1 parent 2c511a4 commit e525db9

File tree

2 files changed

+23
-47
lines changed

2 files changed

+23
-47
lines changed
 

‎packages/mdx/lib/core.js

+21-45
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
* you should probably set `baseUrl` too.
126126
*/
127127

128+
import {unreachable} from 'devlop'
128129
import remarkMdx from 'remark-mdx'
129130
import remarkParse from 'remark-parse'
130131
import remarkRehype from 'remark-rehype'
@@ -158,85 +159,60 @@ const removedOptions = [
158159
* Processor.
159160
*/
160161
export function createProcessor(options) {
161-
const {
162-
SourceMapGenerator,
163-
development,
164-
elementAttributeNameCase,
165-
jsx,
166-
format,
167-
outputFormat,
168-
providerImportSource,
169-
recmaPlugins,
170-
rehypePlugins,
171-
remarkPlugins,
172-
remarkRehypeOptions,
173-
stylePropertyNameCase,
174-
tableCellAlignToStyle,
175-
...rest
176-
} = options || {}
162+
const settings = options || {}
177163
let index = -1
178164

179165
while (++index < removedOptions.length) {
180166
const key = removedOptions[index]
181-
if (options && key in options) {
182-
throw new Error(
183-
'`options.' +
167+
if (key in settings) {
168+
unreachable(
169+
'Unexpected removed option `' +
184170
key +
185-
'` is no longer supported. Please see <https://mdxjs.com/migrating/v2/> for more information'
171+
'`; see <https://mdxjs.com/migrating/v2/> on how to migrate'
186172
)
187173
}
188174
}
189175

190176
// @ts-expect-error: throw an error for a runtime value which is not allowed
191177
// by the types.
192-
if (format === 'detect') {
193-
throw new Error(
194-
"Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format"
178+
if (settings.format === 'detect') {
179+
unreachable(
180+
"Unexpected `format: 'detect'`, which is not supported by `createProcessor`, expected `'mdx'` or `'md'`"
195181
)
196182
}
197183

198184
const pipeline = unified().use(remarkParse)
199185

200-
if (format !== 'md') {
186+
if (settings.format !== 'md') {
201187
pipeline.use(remarkMdx)
202188
}
203189

204-
const extraNodeTypes = remarkRehypeOptions
205-
? remarkRehypeOptions.passThrough || []
206-
: []
190+
const remarkRehypeOptions = settings.remarkRehypeOptions || {}
207191

208192
pipeline
209193
.use(remarkMarkAndUnravel)
210-
.use(remarkPlugins || [])
194+
.use(settings.remarkPlugins || [])
211195
.use(remarkRehype, {
212196
...remarkRehypeOptions,
213197
allowDangerousHtml: true,
214-
passThrough: [...extraNodeTypes, ...nodeTypes]
198+
passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes]
215199
})
216-
.use(rehypePlugins || [])
200+
.use(settings.rehypePlugins || [])
217201

218-
if (format === 'md') {
202+
if (settings.format === 'md') {
219203
pipeline.use(rehypeRemoveRaw)
220204
}
221205

222206
pipeline
223-
.use(rehypeRecma, {
224-
elementAttributeNameCase,
225-
stylePropertyNameCase,
226-
tableCellAlignToStyle
227-
})
228-
.use(recmaDocument, {...rest, outputFormat})
229-
.use(recmaJsxRewrite, {
230-
development,
231-
providerImportSource,
232-
outputFormat
233-
})
207+
.use(rehypeRecma, settings)
208+
.use(recmaDocument, settings)
209+
.use(recmaJsxRewrite, settings)
234210

235-
if (!jsx) {
236-
pipeline.use(recmaJsxBuild, {development, outputFormat})
211+
if (!settings.jsx) {
212+
pipeline.use(recmaJsxBuild, settings)
237213
}
238214

239-
pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || [])
215+
pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || [])
240216

241217
// @ts-expect-error: we added plugins with if-checks, which TS doesn’t get.
242218
return pipeline

‎packages/mdx/test/compile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('@mdx-js/mdx: compile', async function (t) {
2525
assert.throws(function () {
2626
// @ts-expect-error: check how the runtime handles a removed option.
2727
compile('# hi!', {filepath: 'example.mdx'})
28-
}, /`options.filepath` is no longer supported/)
28+
}, /Unexpected removed option `filepath`/)
2929
})
3030

3131
await t.test('should compile', async function () {
@@ -1396,6 +1396,6 @@ test('@mdx-js/mdx: createProcessor', async function (t) {
13961396
assert.throws(function () {
13971397
// @ts-expect-error: check how runtime handles an incorrect `format: 'detect'`.
13981398
createProcessor({format: 'detect'})
1399-
}, new Error("Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format"))
1399+
}, /Unexpected `format: 'detect'`/)
14001400
})
14011401
})

1 commit comments

Comments
 (1)

vercel[bot] commented on Oct 21, 2023

@vercel[bot]

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdx-git-main-mdx.vercel.app
v2.mdxjs.com
mdxjs.com

Please sign in to comment.