Skip to content

Commit ae2c458

Browse files
committed
feat: handle compiler error
1 parent 49d6e53 commit ae2c458

File tree

1 file changed

+68
-58
lines changed

1 file changed

+68
-58
lines changed

packages/markdown-editor/src/mdx-compiler.ts

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,76 +70,86 @@ export const mdxCompiler = async (
7070

7171
const onig = await fetch(`${onigHostUrl}/wasm/onig.wasm`)
7272
setWasm(onig)
73+
74+
const remarkPlugins = (
75+
[
76+
remarkParse,
77+
// remarkMermaid, // should be before remarkRemoveImports because contains `import { Mermaid } from ...`
78+
[
79+
remarkNpm2Yarn, // should be before remarkRemoveImports because contains `import { Tabs as $Tabs, Tab as $Tab } from ...`
80+
{
81+
packageName: 'nextra/components',
82+
tabNamesProp: 'items',
83+
storageKey: 'selectedPackageManager',
84+
},
85+
] satisfies Pluggable,
86+
remarkRemoveImports,
87+
remarkGfm,
88+
[remarkMdxDisableExplicitJsx, { whiteList: ['details', 'summary'] }] satisfies Pluggable,
89+
remarkCustomHeadingId,
90+
[remarkHeadings, { isRemoteContent: true }] satisfies Pluggable,
91+
remarkStaticImage,
92+
remarkReadingTime,
93+
remarkMath,
94+
remarkReplaceImports,
95+
[
96+
remarkRehype,
97+
{
98+
allowDangerousHtml: true,
99+
passThrough: [
100+
'mdxjsEsm',
101+
'mdxFlowExpression',
102+
'mdxJsxFlowElement',
103+
'mdxJsxTextElement',
104+
'mdxTextExpression',
105+
],
106+
},
107+
],
108+
[
109+
clonedRemarkLinkRewrite,
110+
{
111+
pattern: MARKDOWN_URL_EXTENSION_REGEX,
112+
replace: '',
113+
excludeExternalLinks: true,
114+
},
115+
] satisfies Pluggable,
116+
] as any
117+
).filter(truthy)
118+
119+
const rehypePlugins = (
120+
[
121+
// [rehypeRaw, { allowDangerousHtml: true }],
122+
[parseMeta, { defaultShowCopyCode }] satisfies Pluggable,
123+
rehypeKatex,
124+
attachMeta,
125+
rehypeStringify,
126+
] as any
127+
).filter(truthy)
128+
73129
try {
74130
const result = await serialize(content, {
75131
mdxOptions: {
76132
format: isError ? 'md' : 'mdx',
77133
development: false,
78-
remarkPlugins: (
79-
[
80-
remarkParse,
81-
// remarkMermaid, // should be before remarkRemoveImports because contains `import { Mermaid } from ...`
82-
[
83-
remarkNpm2Yarn, // should be before remarkRemoveImports because contains `import { Tabs as $Tabs, Tab as $Tab } from ...`
84-
{
85-
packageName: 'nextra/components',
86-
tabNamesProp: 'items',
87-
storageKey: 'selectedPackageManager',
88-
},
89-
] satisfies Pluggable,
90-
remarkRemoveImports,
91-
remarkGfm,
92-
[
93-
remarkMdxDisableExplicitJsx,
94-
{ whiteList: ['details', 'summary'] },
95-
] satisfies Pluggable,
96-
remarkCustomHeadingId,
97-
[remarkHeadings, { isRemoteContent: true }] satisfies Pluggable,
98-
remarkStaticImage,
99-
remarkReadingTime,
100-
remarkMath,
101-
remarkReplaceImports,
102-
[
103-
remarkRehype,
104-
{
105-
allowDangerousHtml: true,
106-
passThrough: [
107-
'mdxjsEsm',
108-
'mdxFlowExpression',
109-
'mdxJsxFlowElement',
110-
'mdxJsxTextElement',
111-
'mdxTextExpression',
112-
],
113-
},
114-
],
115-
[
116-
clonedRemarkLinkRewrite,
117-
{
118-
pattern: MARKDOWN_URL_EXTENSION_REGEX,
119-
replace: '',
120-
excludeExternalLinks: true,
121-
},
122-
] satisfies Pluggable,
123-
] as any
124-
).filter(truthy),
125-
rehypePlugins: (
126-
[
127-
// [rehypeRaw, { allowDangerousHtml: true }],
128-
[parseMeta, { defaultShowCopyCode }] satisfies Pluggable,
129-
rehypeKatex,
130-
attachMeta,
131-
rehypeStringify,
132-
] as any
133-
).filter(truthy),
134+
remarkPlugins,
135+
rehypePlugins,
134136
},
135137
})
136138

137139
return result
138140
} catch (error: any) {
139141
if (process.env.NODE_ENV === 'development') {
140142
console.log('Failed to compile source', error)
141-
throw new Error(error)
142143
}
143-
return null
144+
const result = await serialize(content, {
145+
mdxOptions: {
146+
format: 'md',
147+
development: false,
148+
remarkPlugins,
149+
rehypePlugins,
150+
},
151+
})
152+
153+
return result
144154
}
145155
}

0 commit comments

Comments
 (0)