npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-block-text-parser

v1.0.5

Published

A JavaScript function for rendering rich text blocks fetched from Strapi. This function converts structured article bodies into HTML elements, handling various content types like headings, lists, quotes, paragraphs, and images.

Downloads

5

Readme

BlocksRenderer

This function is designed to render rich text blocks fetched from Strapi. It converts the structured data of the article body into HTML elements based on the type of content present.

Functionality

The BlocksRenderer function iterates through each block of the article body and converts it into HTML elements based on its type. Here's how each type of block is handled:

  • Heading: Renders headings (<h1>, <h2>, <h3>, etc.) based on the level specified in the article.
  • List: Renders either ordered or unordered lists (<ol> or <ul>) depending on the format of the list in the article.
  • Quote: Renders a blockquote (<q>) element.
  • Paragraph: Renders paragraphs (<p>) for regular text content.
  • Image: Renders an image (<img>) element with the source taken from the URL specified in the article.

Usage

To use this function, pass the article body fetched from Strapi as an argument to the function. It will return an array of HTML strings, each representing a rendered block of the article body.

import { BlocksRenderer } from './BlocksRenderer';

const articleBody = /* Fetch article body from Strapi */;
const renderedBlocks = BlocksRenderer(articleBody);
// Render the HTML elements in your application

Ensure that the article body passed to the function follows the expected structure with appropriate types and content.

BlocksRenderer Example

Here's an example demonstrating how to use the BlocksRenderer function with sample data:

Suppose we have an article body represented as an array of objects:

const articleBody = [
  { type: 'heading', level: 1, children: [{ text: 'Sample Heading' }] },
  { type: 'paragraph', children: [{ text: 'This is a sample paragraph.' }] },
  { type: 'list', format: 'unordered', children: [{ children: [{ text: 'Item 1' }] }, { children: [{ text: 'Item 2' }] }] },
  { type: 'quote', children: [{ text: 'This is a sample quote.' }] },
  { type: 'image', image: { url: 'https://example.com/image.jpg' } }
];

We can then render this article body using the BlocksRenderer function:

import { BlocksRenderer } from './BlocksRenderer';

const renderedBlocks = BlocksRenderer(articleBody);

The renderedBlocks array now contains HTML strings representing each block of the article body, ready to be rendered in a web page or application.

Rendered Output

Assuming the article body contains various types of content, the rendered output might look like this:

<h1>Sample Heading</h1>
<p>This is a sample paragraph.</p>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>
<q>This is a sample quote.</q>
<img src="https://example.com/image.jpg" width="100%" height="auto"/>