Skip to content

Commit

Permalink
Don't modify the options passed in (#206)
Browse files Browse the repository at this point in the history
* Don't modify the options passed in

* Modified test to ensure options not modified directly
  • Loading branch information
matthewoates committed Nov 8, 2021
1 parent 9b1fd09 commit 6e18f67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 deletions __tests__/index.tsx
Expand Up @@ -90,14 +90,16 @@ describe('serialize', () => {
})

test('with options', async () => {
const result = await renderStatic('~> hello', {
const options = {
mdxOptions: {
remarkPlugins: [paragraphCustomAlerts],
},
})
}
const result = await renderStatic('~> hello', options)
expect(result).toMatchInlineSnapshot(
`"<div class=\\"alert alert-warning g-type-body\\" role=\\"alert\\"><p>hello</p></div>"`
)
expect(options.mdxOptions.remarkPlugins.length).toBe(1)
})

test('with scope', async () => {
Expand Down Expand Up @@ -182,7 +184,7 @@ export const foo = 'bar'`)
const mdx = `import foo from 'bar'
foo **bar**
export const foo = 'bar'`

const resultA = await serialize(mdx, { target: 'esnext' })
Expand Down
8 changes: 7 additions & 1 deletion src/serialize.ts
Expand Up @@ -63,10 +63,16 @@ export async function serialize(
target = ['es2020', 'node12'],
}: SerializeOptions = {}
): Promise<MDXRemoteSerializeResult> {
mdxOptions.remarkPlugins = [
// don't modify the original object when adding our own plugin
// this allows code to reuse the same options object
const remarkPlugins = [
...(mdxOptions.remarkPlugins || []),
removeImportsExportsPlugin,
]
mdxOptions = {
...mdxOptions,
remarkPlugins,
}

const compiledMdx = await mdx(source, { ...mdxOptions, skipExport: true })
const transformResult = await transform(compiledMdx, {
Expand Down

0 comments on commit 6e18f67

Please sign in to comment.