Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@contentful/rich-text-plain-text-renderer
Advanced tools
Plain text renderer for the Rich Text document.
@contentful/rich-text-plain-text-renderer is an npm package that provides functionality to convert Contentful's rich text documents into plain text. This can be useful for scenarios where you need to extract and manipulate text content without any formatting or HTML tags.
Convert Rich Text to Plain Text
This feature allows you to convert a Contentful rich text document into a plain text string. The code sample demonstrates how to use the `documentToPlainTextString` function to achieve this.
const { documentToPlainTextString } = require('@contentful/rich-text-plain-text-renderer');
const richTextDocument = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello, world!',
marks: [],
data: {}
}
],
data: {}
}
],
data: {}
};
const plainText = documentToPlainTextString(richTextDocument);
console.log(plainText); // Output: 'Hello, world!'
The `html-to-text` package converts HTML content into plain text. It is more general-purpose compared to @contentful/rich-text-plain-text-renderer, which is specifically designed for Contentful's rich text format. However, `html-to-text` can be used for a wider range of HTML content.
The `turndown` package is a highly customizable HTML to Markdown converter. While it focuses on converting HTML to Markdown, it can also be used to strip HTML tags and extract plain text. This makes it a versatile tool for text extraction, though it is not specifically tailored for Contentful's rich text format.
The `sanitize-html` package is used to clean up and sanitize HTML content by removing unwanted tags and attributes. While its primary purpose is to sanitize HTML, it can also be configured to strip all tags, effectively converting HTML to plain text. This package offers more control over the sanitization process compared to @contentful/rich-text-plain-text-renderer.
Plain text renderer for the Rich Text document.
Using npm:
npm install @contentful/rich-text-plain-text-renderer
Using yarn:
yarn add @contentful/rich-text-plain-text-renderer
import { documentToPlainTextString } from '@contentful/rich-text-plain-text-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ nodeType: 'bold' }]
},
{
nodeType: 'text',
value: ' world!',
marks: [{ nodeType: 'italic' }]
},
],
},
]
};
documentToPlainTextString(document); // -> Hello world!
FAQs
Plain text renderer for the Rich Text document.
We found that @contentful/rich-text-plain-text-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.