The evalUnserializableExpressions option uses eval() to attempt to evaluate expressions in JSX props that cannot be serialized as JSON (like functions or variables).
⚠️ WARNING: This is extremely dangerous if used with untrusted or user-generated content.
Recommended approach: Instead of enabling this, use the renderRule option to selectively handle specific nodes and expressions safely. This allows you to use a lookup table of trusted handlers or a controlled eval with an allowlist.
// Instead of eval'ing arbitrary expressions, handle them selectively in renderRule:
const handlers = {
handleClick: () => console.log('clicked'),
handleSubmit: () => console.log('submitted'),
}
compiler(markdown, {
renderRule(next, node) {
if (
node.type === RuleType.htmlBlock &&
typeof node.attrs?.onClick === 'string'
) {
// Option 1: Named handler lookup (safest)
const handler = handlers[node.attrs.onClick]
if (handler) {
return <button onClick={handler}>{/* ... */}</button>
}
// Option 2: Selective eval with allowlist (still risky)
if (
node.tag === 'TrustedComponent' &&
node.attrs.onClick.startsWith('() =>')
) {
try {
const fn = eval(`(${node.attrs.onClick})`)
return <button onClick={fn}>{/* ... */}</button>
} catch (e) {
// Handle error
}
}
}
return next()
},
})