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

@kentico-ericd/kontent-richtext-validation

v0.0.13

Published

Provides a function to validate the content of a Rich Text element to ensure it's suitable for updating via the Kontent Management API.

Downloads

18

Readme

Kontent Rich Text Validator

This package provides a function for validating the content of Rich Text elements. It can be used in conjunction with the Kontent Management JS SDK or our REST API to ensure the text is in the correct format before the data is sent to Kontent.

This can speed up the development process (e.g. when writing webhooks) and prevent errors while updating content, as errors can be resolved without the need for submitting repeated API calls, and could be integrated into testing scenarios.

Installation

Install via NPM:

npm i @kentico-ericd/kontent-richtext-validation --save

Usage

// Typescript + ES
import { KontentRichText } from '@kentico-ericd/kontent-richtext-validation'
// CommonJS
const { KontentRichText } = require('@kentico-ericd/kontent-richtext-validation');

The KontentRichText class provides a validate() function, which returns the following object:

{
  success: <boolean>,
  message: <string>
}

In the event that there is an error parsing the input, success will be false and message will contain the error and (in most cases) the exact position of the faulty text. On success, the message should contain the exact text that was passed to the function.

For example:

KontentRichText.validate('<p><a hrf="https://kontent.ai">Kentico Kontent</a></p>')
{
  success: false,
  message: "The A element must have a 'href' attribute or a 'data-email-address' attribute or a data attribute that identifies the link 
target ('data-item-id', 'data-item-external-id', 'data-asset-id' or 'data-asset-external-id'). (1, 30)"
}

We can see that the error occurred on line 1, col 30 which is the closing double-quote of the href attribute.

Example

This package could be integrated into the automatic translation webhook of the sample ExpressJS application. Perhaps we want to always return a 200 (success) response to Kontent when the webhook triggers, so that the webhook doesn't receive errors and stop processing. Or, we could validate the text and if there is some error, we can skip that item and continue with the rest of them.

Specifically, it could be added to this line where we get translated text from an external service. Maybe the external service accidentally changed some of the HTML? Let's check:

if (match.length > 0) {
  e.value = text.replace(/<br>/g, '<br/>');
  const validationResult = KontentRichText.validate(e.value);
  if(!validationResult.success) {
    const error = `Validation failed for item ${updatedVariant.data.item.id},`
      + ` lang ${targetLangCode}, element ${e.id}: ${validationResult.message}`;

    // We can log the error
    console.log(error);
    // We could write to some physical log file too
    writeToLogFile(error);
    // Send an email to devs?
    sendEmail('[email protected]', 'Webhook error', error);
    // Skip this variant but continue with others
    stopProcessing = true;
    // or, maybe throw an error
    throw new Error(error);
  }
}