Skip to content

Commit

Permalink
Refactor some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 21, 2023
1 parent 2c511a4 commit e525db9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
66 changes: 21 additions & 45 deletions packages/mdx/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
* you should probably set `baseUrl` too.
*/

import {unreachable} from 'devlop'
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
Expand Down Expand Up @@ -158,85 +159,60 @@ const removedOptions = [
* Processor.
*/
export function createProcessor(options) {
const {
SourceMapGenerator,
development,
elementAttributeNameCase,
jsx,
format,
outputFormat,
providerImportSource,
recmaPlugins,
rehypePlugins,
remarkPlugins,
remarkRehypeOptions,
stylePropertyNameCase,
tableCellAlignToStyle,
...rest
} = options || {}
const settings = options || {}
let index = -1

while (++index < removedOptions.length) {
const key = removedOptions[index]
if (options && key in options) {
throw new Error(
'`options.' +
if (key in settings) {
unreachable(
'Unexpected removed option `' +
key +
'` is no longer supported. Please see <https://mdxjs.com/migrating/v2/> for more information'
'`; see <https://mdxjs.com/migrating/v2/> on how to migrate'
)
}
}

// @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"
if (settings.format === 'detect') {
unreachable(
"Unexpected `format: 'detect'`, which is not supported by `createProcessor`, expected `'mdx'` or `'md'`"
)
}

const pipeline = unified().use(remarkParse)

if (format !== 'md') {
if (settings.format !== 'md') {
pipeline.use(remarkMdx)
}

const extraNodeTypes = remarkRehypeOptions
? remarkRehypeOptions.passThrough || []
: []
const remarkRehypeOptions = settings.remarkRehypeOptions || {}

pipeline
.use(remarkMarkAndUnravel)
.use(remarkPlugins || [])
.use(settings.remarkPlugins || [])
.use(remarkRehype, {
...remarkRehypeOptions,
allowDangerousHtml: true,
passThrough: [...extraNodeTypes, ...nodeTypes]
passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes]
})
.use(rehypePlugins || [])
.use(settings.rehypePlugins || [])

if (format === 'md') {
if (settings.format === 'md') {
pipeline.use(rehypeRemoveRaw)
}

pipeline
.use(rehypeRecma, {
elementAttributeNameCase,
stylePropertyNameCase,
tableCellAlignToStyle
})
.use(recmaDocument, {...rest, outputFormat})
.use(recmaJsxRewrite, {
development,
providerImportSource,
outputFormat
})
.use(rehypeRecma, settings)
.use(recmaDocument, settings)
.use(recmaJsxRewrite, settings)

if (!jsx) {
pipeline.use(recmaJsxBuild, {development, outputFormat})
if (!settings.jsx) {
pipeline.use(recmaJsxBuild, settings)
}

pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || [])
pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || [])

// @ts-expect-error: we added plugins with if-checks, which TS doesn’t get.
return pipeline
Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('@mdx-js/mdx: compile', async function (t) {
assert.throws(function () {
// @ts-expect-error: check how the runtime handles a removed option.
compile('# hi!', {filepath: 'example.mdx'})
}, /`options.filepath` is no longer supported/)
}, /Unexpected removed option `filepath`/)
})

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

1 comment on commit e525db9

@vercel
Copy link

@vercel vercel bot commented on e525db9 Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.