Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse code blocks children to plain text #17

Merged
merged 2 commits into from
Dec 14, 2023
Merged

Conversation

remidej
Copy link
Contributor

@remidej remidej commented Dec 13, 2023

What does it do?

It adds a plainText prop to the renderer component for code blocks.

Why is it needed?

Like all other blocks, code blocks need to have an array of objects as children.

This makes it hard for users to implement syntax highlighting libraries that expect a plain text string. They need to parse the children to generate that string. This PR does this at the renderer level so they don't need to.

See this discord message

How to test it?

In a frontend app, use the renderer (link it via yarn), and pass a custom component for code that uses the plainText prop

@remidej remidej self-assigned this Dec 13, 2023
@remidej remidej added the pr: feature New or updates to features label Dec 13, 2023
Copy link
Contributor

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests we can do to validate this feature / fix? :)

@remidej
Copy link
Contributor Author

remidej commented Dec 13, 2023

@joshuaellis there's a test in the PR already 🤔

@joshuaellis
Copy link
Contributor

@joshuaellis there's a test in the PR already 🤔

That was weird, it didn't show – I think i checked on mobile so maybe it hadn't got all the data 🤦🏼‍♀️

Copy link
Contributor

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎁 3 for 3 – on a roll!

@remidej remidej merged commit 6524cba into main Dec 14, 2023
2 checks passed
@remidej remidej deleted the feature/code-parsing branch December 14, 2023 08:40
@benjaminrobinet
Copy link

benjaminrobinet commented Mar 13, 2024

Hello!

It would be great to have a plainText for headings too. It would be much more easier to add an ID attribute to headings to be able to create anchors to them.

@remidej
Copy link
Contributor Author

remidej commented Mar 13, 2024

Interesting idea @benjaminrobinet, thanks. I think we could go a step further and instead generate a kebab-case slug attribute. Or do you need to control the text-to-slug logic?

@benjaminrobinet
Copy link

benjaminrobinet commented Mar 13, 2024

@remidej I only thought about a "slugify ids", but maybe there is some other needs behind this. Maybe we could add something like:

<BlocksRenderer content={content} transformers={{
    heading: ({ plainText }) => {
        return { attrs: { id: slugify(plainText) } };
    },
}} />

For the specific case of id's we could just return the index of the block to be able to create the attribute like:

<BlocksRenderer
    content={content}
    blocks={{
        heading: ({ children, level, index }) => {
            const HeadingLevel = `h${level}` as keyof React.JSX.IntrinsicElements;
            return <HeadingLevel id={`heading-${index}`}>{children}</HeadingLevel>;
        },
    }}
/>

@benjaminrobinet
Copy link

Just created an issue / pull request to discuss it, we a simple implementation for the plainText of headings #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: feature New or updates to features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants