portabletext / portabletext/toolkit

If markDefs is null, .find breaks

Open
#95 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
14
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Not sure what happened here, but we have a Sanity instance. Apparently occasionally after duplicating or copying a content block, like our 'rich text' (which is just the WYSIWYG editor), the 'markDefs' may be null.

I genuinely don't know why this happens, but when it does, I get this error:

TypeError: Cannot read properties of null (reading 'find')
    at buildMarksTree (file:///Users/.../web/node_modules/@portabletext/toolkit/dist/index.js:89:34)
    at serializeBlock (file:///Users/.../web/node_modules/@portabletext/react/dist/index.js:381:16)
    at renderBlock (file:///Users/.../web/node_modules/@portabletext/react/dist/index.js:319:29)
    at renderNode (file:///Users/.../web/node_modules/@portabletext/react/dist/index.js:225:323)
    at <unknown> (file:///Users/.../web/node_modules/@portabletext/react/dist/index.js:207:44)
    at Array.map (<anonymous>)
    at PortableText (file:///Users/.../web/node_modules/@portabletext/react/dist/index.js:207:23)
    at renderWithHooks (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5732:15)
    at renderElement (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5957:7)
    at renderNodeDestructiveImpl (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6115:11)
    at renderNodeDestructive (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6087:14)
    at renderNode (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6270:12)
    at renderHostElement (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5642:3)
    at renderElement (file:///Users/.../web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5963:5)

So, for some reason:

Screenshot 2024-12-09 at 12 59 58 PM

I'm able to see where this is happening: https://github.com/portabletext/toolkit/blob/acf4977c232bcc9ec6c9132a2783d87e6fdf2f1a/src/buildMarksTree.ts#L93

Not sure this is widespread or not, but in code, I've got this helper now:

  function sanitizeData(data) {
    return data.map((block) => ({
      ...block,
      markDefs: block.markDefs || []
    }));
  }

Either way, it seems that having that fall back or doing a check on markDefs may be the right call? Or this is an edge error in our editor.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/buildMarksTree.ts around line 93, where the reported .find failure occurs, and trace how a block with markDefs set to null reaches it. Reproduce the Portable Text rendering case from the report and confirm the chosen handling no longer throws while preserving normal markDefs behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.