|
125 | 125 | * you should probably set `baseUrl` too. |
126 | 126 | */ |
127 | 127 |
|
| 128 | +import {unreachable} from 'devlop' |
128 | 129 | import remarkMdx from 'remark-mdx' |
129 | 130 | import remarkParse from 'remark-parse' |
130 | 131 | import remarkRehype from 'remark-rehype' |
@@ -158,85 +159,60 @@ const removedOptions = [ |
158 | 159 | * Processor. |
159 | 160 | */ |
160 | 161 | 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 || {} |
177 | 163 | let index = -1 |
178 | 164 |
|
179 | 165 | while (++index < removedOptions.length) { |
180 | 166 | 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 `' + |
184 | 170 | 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' |
186 | 172 | ) |
187 | 173 | } |
188 | 174 | } |
189 | 175 |
|
190 | 176 | // @ts-expect-error: throw an error for a runtime value which is not allowed |
191 | 177 | // 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'`" |
195 | 181 | ) |
196 | 182 | } |
197 | 183 |
|
198 | 184 | const pipeline = unified().use(remarkParse) |
199 | 185 |
|
200 | | - if (format !== 'md') { |
| 186 | + if (settings.format !== 'md') { |
201 | 187 | pipeline.use(remarkMdx) |
202 | 188 | } |
203 | 189 |
|
204 | | - const extraNodeTypes = remarkRehypeOptions |
205 | | - ? remarkRehypeOptions.passThrough || [] |
206 | | - : [] |
| 190 | + const remarkRehypeOptions = settings.remarkRehypeOptions || {} |
207 | 191 |
|
208 | 192 | pipeline |
209 | 193 | .use(remarkMarkAndUnravel) |
210 | | - .use(remarkPlugins || []) |
| 194 | + .use(settings.remarkPlugins || []) |
211 | 195 | .use(remarkRehype, { |
212 | 196 | ...remarkRehypeOptions, |
213 | 197 | allowDangerousHtml: true, |
214 | | - passThrough: [...extraNodeTypes, ...nodeTypes] |
| 198 | + passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes] |
215 | 199 | }) |
216 | | - .use(rehypePlugins || []) |
| 200 | + .use(settings.rehypePlugins || []) |
217 | 201 |
|
218 | | - if (format === 'md') { |
| 202 | + if (settings.format === 'md') { |
219 | 203 | pipeline.use(rehypeRemoveRaw) |
220 | 204 | } |
221 | 205 |
|
222 | 206 | 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) |
234 | 210 |
|
235 | | - if (!jsx) { |
236 | | - pipeline.use(recmaJsxBuild, {development, outputFormat}) |
| 211 | + if (!settings.jsx) { |
| 212 | + pipeline.use(recmaJsxBuild, settings) |
237 | 213 | } |
238 | 214 |
|
239 | | - pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || []) |
| 215 | + pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || []) |
240 | 216 |
|
241 | 217 | // @ts-expect-error: we added plugins with if-checks, which TS doesn’t get. |
242 | 218 | return pipeline |
|
0 commit comments