atelierdisko / payload-lexical-react-renderer

MIT License
24 stars 3 forks source link

forward `text` from text nodes as prop #4

Closed schardev closed 1 month ago

schardev commented 2 months ago

Currently there's no easy way to get the text of a text node from the renderers:

<PayloadLexicalReactRenderer
key={node.id}
content={node}
elementRenderers={{
    ...defaultElementRenderers,
    heading: (props) => {
        // props.children is nested with fragments and there's no easy way to get text from it
        return (
            <CustomHeading
                id={`#${props.text}`} // intended usecase
                as={props.tag}
             >
                 {props.children}
            </CustomHeading>
        );
    },
}}
/>

The intended usecase is for adding the text as ids to the heading element.

schaschjan commented 1 month ago

@schardev there is no easy fix for this, as headings can always contain various marks. You would need to iterate through the children and create a slug from the texts, but I don't think this is vary practical. I would consider using a custom block with a slug and title field to accomplish this.