Giter Site home page Giter Site logo

kontent-ai / kontent-delivery-node-parser Goto Github PK

View Code? Open in Web Editor NEW
0.0 18.0 2.0 586 KB

Rich text element parser for node.js & javascript delivery SDK.

License: MIT License

JavaScript 49.41% TypeScript 50.59%
kontent kontent-delivery rich-text-element kontent-parser kontent-ai kontent-ai-tool

kontent-delivery-node-parser's Introduction

Node.js parser for Rich text elements

This library is an optional addon for the javascript delivery SDK that can be used to resolve rich text elements in node.js environment.

This is an alternative to built-in browserParser that comes native with the SDK and works in browsers.

Usage

import { createRichTextHtmlResolver, createAsyncRichTextHtmlResolver } from '@kontent-ai/delivery-sdk';
import { nodeParser, asyncNodeParser } from '@kontent-ai/delivery-node-parser';

// transform rich text html into json
const json = createRichTextHtmlResolver(nodeParser).resolveRichText(data);

// or
const html = await createAsyncRichTextHtmlResolver(asyncNodeParser).resolveRichText(data);

kontent-delivery-node-parser's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kontent-delivery-node-parser's Issues

nodeParser for richtext

Brief bug description

Using nodeParser, it doesn't resolve rendering empty object references. The HTML element that I want to use remains wrapped in an object type. I use nodeParser in createRichTextHtmlResolver(nodeParser) to resolve rich text with components and items.

<object type="application/kenticocloud" data-type="item" data-rel="link" data-codename="image__test" data-sdk-resolved="1"><img src="https://miro.medium.com/v2/resize:fit:720/format:webp/1*xMuIOwjliGUPjkzukeWKfw.jpeg" alt="undefined">
</object>

Using nodeParser I expected to get rid of object type but it doen't happen.

Screenshot 2024-02-05 155801
image

Empty text

Brief bug description

When I want to parse an HTML string like this one <p>one <strong>two</strong></p>, the first TextNode child (#text) of the P tag does not contain the value one .

The issue comes from the text node which is serialized with parse5 while parse5 computes the text value from the childNodes, which a TextNode has not.

If you replace the parentNode instead of adding it to the childNodes, it works better:
https://github.com/kontent-ai/kontent-delivery-node-parser/blob/master/lib/parser/implementation/shared.ts#L160

-        text: striptags(serialize(node)),
+        text:  tagName === "#text" ? node.value : striptags(serialize(node)),

Test environment

  • Node

Incorrect main/module/exports specified in its package.json

Brief bug description

When using this package in an Astro project, Astro throws this error
error Failed to resolve entry for package "@kontent-ai/delivery-node-parser". The package may have incorrect main/module/exports specified in its package.json.

It looks like

"module": "./dist/esm5/index.js",
"es2015": "./dist/esm/index.js",

in the package.json should read

"module": "./dist/es6/index.js",
"es2015": "./dist/es6/index.js",

as there is no esm5 or esm folder in the package /dist folder, only 'cjs, es6, es2020, esnext'.

Repro steps

There's quite a few steps to reproduce in our demo repo but I believe if you try and add this package to any Vite project, it should throw the same error (as Astro uses Vite under the hood).

If you need more details @Enngage I'm more than happy to record a quick demo video or jump on a Zoom call / Hangout.

Expected behavior

The module references in package.json point to the correct output folder.

Additional context

In our demo Astro project we resolve rich text info from the Delivery API into an object, so that we can create a custom resolver component for outputting the HTML we need.

import { nodeParser } from "@kontent-ai/delivery-node-parser";
import { createRichTextObjectResolver, Elements, linkedItemsHelper } from "@kontent-ai/delivery-sdk";

export default async function resolveRichText(richTextElement: Elements.RichTextElement) {
    const resolvedObject = createRichTextObjectResolver(nodeParser).resolveRichText({
        element: richTextElement,
        linkedItems: linkedItemsHelper.convertLinkedItemsToArray(richTextElement.linkedItems),
    });

    return resolvedObject;
}

but while our editor doesn't throw an error (it resolves the dist/cjs files), the Vite dev server Astro runs under the hood throws an error
Screen Shot 2023-01-11 at 15 27 14

Going into node_modules/@kontent-ai/delivery-node-parser/package.json and changing module and es2015 to the following resolves the issues

"module": "./dist/es6/index.js",
"es2015": "./dist/es6/index.js",

preserveResolvedObjectTags set to false preserve object tags

Brief bug description

When I use the node example with the option preserveResolvedObjectTags: false, the HTML output still contains the A or OBJECT tags.

If you replace the parentNode instead of adding it to the childNodes, it works better:
https://github.com/kontent-ai/kontent-delivery-node-parser/blob/master/lib/parser/implementation/shared.ts#L155

-    element.childNodes = [rootNode];
+    Object.entries(rootNode).forEach(([attributeName, attributeValue]) => {
+        element[attributeName] = attributeValue
+    })

Expected behavior

No duplicated tags.

Test environment

  • Node

Additional context

Add any other context about the problem here.

Screenshots

Add links to screenshots, if possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.