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

@brightspot/marked-text

v4.5.9-rc.2

Published

Library to traverse marked text received from Brightspot's GraphQL API

Downloads

6

Readme

Brightspot - Marked Text

Marked Text is a JSON representation of a Brightspot rich text editor (RTE) field containing all the metadata for rich-text objects. Its structure is flattened in such a way that is conducive to the GraphQL ecosystem.

This utility provides a flexible framework for iterating over a Marked Text object and transforming it in any way required, without limitations. Most commonly in the form of HTML or React components.

Installing the API

Install with npm:

npm i  @brightspot/marked-text

Install with yarn:

yarn add @brightspot/marked-text

The exported markedTextTraversal function will transform the Marked Text representation of a Brightspot RTE field into the content required for your application.

Importing the function into your application:

import { markedTextTraversal } from '@brightspot/marked-text'

Transforming Marked Text

The markedTextTraversal function is passed the following two arguments.

  1. data: Marked Text JSON object (see example below).
  2. visitor: Object containing two properties: visitText with a function value; visitMark with a function value.

Below is a simple usage that converts Marked Text into an HTML representation with all of the expected parent-child nesting.

const result = markedTextTraversal(data, {
  visitText: (text) => {
    return text
  },
  visitMark: (mark, children) => {
    let markup = `<${mark.data.name}>`
    children.forEach((child) => (markup += child))
    markup += `</${mark.data.name}>`
    return markup
  },
})

Your visitText function is passed a single string argument. In this simple example nothing is needed beyond returning its value right back, but if a business case required it it could be transformed here.

The visitMark function is doing the bulk of the work, creating our HTML markup in this example. This function receives two arguments, a mark object and an array.

The mark object will always contain the properties descendants, end, start and data. The data property is also an object containing the property __typename. In the example below, it is of type RteHtmlElement which also contains the properties name and attributes but these extra properties vary depending on the type of rich text element returned.

{
  "descendants": 2,
  "end": 23,
  "start": 0,
  "data": {
    "__typename": "RteHtmlElement",
    "name": "p",
    "attributes": []
  }
}

The children array is the array of children within the mark that has been processed with the visitor object. As such, the type of elements in the array vary depending on the user's implementation.

Example Input-Output

Below is taken directly from the Brightspot GraphQL Explorer output, in our example above, data of Marked Text object lies within the property richText:

{
  "data": {
    "Article": {
      "headline": "Hello, world",
      "body": {
        "__typename": "RichTextBody",
        "richText": {
          "text": "Hello, rich text world!",
          "marks": [
            {
              "descendants": 2,
              "end": 23,
              "start": 0,
              "data": {
                "__typename": "RteHtmlElement",
                "name": "p",
                "attributes": []
              }
            },
            {
              "descendants": 1,
              "end": 16,
              "start": 7,
              "data": {
                "__typename": "RteHtmlElement",
                "name": "b",
                "attributes": []
              }
            },
            {
              "descendants": 0,
              "end": 16,
              "start": 12,
              "data": {
                "__typename": "RteHtmlElement",
                "name": "u",
                "attributes": []
              }
            }
          ]
        }
      }
    }
  }
}

Produces the following HTML:

<p>Hello, <b>rich <u>text</u></b> world!</p>