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]: Extract plain text from blocks #21

Open
takumiyoshikawa opened this issue Jan 17, 2024 · 1 comment
Open

[feat]: Extract plain text from blocks #21

takumiyoshikawa opened this issue Jan 17, 2024 · 1 comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature

Comments

@takumiyoshikawa
Copy link

A clear and concise description of what the feature is

This feature adds a method to Strapi's rich-text rendering system, enabling the extraction of plain text from rich-text content.

Why should this feature be included?

This feature is useful in various content markup scenarios. For instance, in creating FAQ sections, it's crucial to have a method that efficiently strips formatting to present clear, concise text.

Please provide an example for how this would work

The feature would work similarly to PortableText's toPlainText.
https://github.com/portabletext/toolkit/blob/main/src/toPlainText.ts

@takumiyoshikawa takumiyoshikawa changed the title [feat]: Extract Plain Text from blocks [feat]: Extract plain text from blocks Jan 17, 2024
@takumiyoshikawa
Copy link
Author

I think this minimum Implementation below works.

interface Node {
    text?: string;
    type: string;
    children: Node[];
}

export function getPlainText(block: Node[]) {
    const text: string = block.reduce((acc, node) => {
        if (node.type === 'text') {
            return acc + node.text;
        }
        return acc + getPlainText(node.children);
    }, '');

    return text;
}

@joshuaellis joshuaellis added the issue: enhancement Issue suggesting an enhancement to an existing feature label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants