fix: treat undefined markdown as empty string - #960
BetterAndBetterII wants to merge 1 commit into
Conversation
MDXEditor crashed when the markdown prop was undefined because optional chaining only guarded params, not initialMarkdown itself: params?.initialMarkdown.trim() threw on reading trim. Coerce undefined/null to empty string in postInit (matching init), setMarkdown$, and the pre-ready markdown path. Fixes mdx-editor#803
|
Thanks for the PR! One thing to sort out before this moves forward: This goes against the declared TS types.
As a result, the runtime would accept values the types say are impossible — TS consumers would silently get an empty editor for a contract violation instead of an error. (The issue reporter also notes the caller-side workaround is trivial: To be fair, there is one genuine latent bug fixed here: @BetterAndBetterII could you clarify the necessity for the runtime guards beyond that fix? If we do want to tolerate |
Summary
MDXEditor crashed when
markdownwasundefined/null. Optional chaining only guardedparams, soparams?.initialMarkdown.trim()threwCannot read properties of undefined (reading 'trim').Treat undefined/null as an empty string in
postInit(same asinit),setMarkdown$, and the pre-ready markdown path so the editor renders empty instead of throwing.Fixes #803
Test plan
npx vitest --run src/test/core.test.tsx(22 passed, including undefined/null markdown andsetMarkdown(undefined))