rich-text icon indicating copy to clipboard operation
rich-text copied to clipboard

`documentToReactComponents` generating incorrect HTML attributes with custom render options

Open pjaws opened this issue 3 years ago • 0 comments

In our blog articles, we are generating a table of contents with anchor links by generating HTML ids for each h2 and passing in a custom renderer for BLOCKS.HEADING_2.

This works pretty well in most cases, but in at least one, we're seeing some weird behavior with duplicate/incorrect ids being rendered. I'm 100% positive we're passing in the correct input to documentToReactComponents, which you can see in these logs: Screen Shot 2021-10-13 at 2 34 53 PM

However, this is the output we're getting: Screen Shot 2021-10-13 at 2 36 01 PM

As you can see, the span with id="Criterion_2_Fees" is repeated, and then the fourth block has the id the third should have had.

For reference, here is the custom renderer we're passing in (with the log statements included):

const renderOptions = {
  renderNode: {
    [BLOCKS.HEADING_2]: (node, children) => {
      console.log(
        'assigning this id:',
        generateHtmlIdFromContent(node.content[0]?.value)
      );
      console.log('to this heading', children);
      return (
        <>
          <Anchor id={generateHtmlIdFromContent(node.content[0]?.value)} />
          <BodyHeading as="h2">
            {children}
          </BodyHeading>
        </>
      );
    },
    [BLOCKS.PARAGRAPH]: (node, children) => <StyledText mt={3}>{children}</StyledText>,
  },
};

You can see a live example of this happening here.

Let me know if I can provide any additional detail!

pjaws avatar Oct 13 '21 21:10 pjaws