pavittarx / editorjs-html

A javascript library to parse editorjs clean data to html. It is adaptable and usable in all kind of projects.
https://runkit.com/pavittarx/editorjs-html-example
MIT License
331 stars 61 forks source link

List Recursor Continues to Execute on Empty Nested `item.items` Array #53

Open Luzefiru opened 8 months ago

Luzefiru commented 8 months ago

The Problem

Looking at the clean data of a list using the @editorjs/nested-list plugin, it seems that each item[*] child has an item[*].items automatically added to it, but with no items [].

The current transformers.ts code found here checks if (item.items) before calling the recursor() function. An empty array [] in JavaScript is evaluated to true, making the recursor basically parse nothing and return `<${listStyle}>${list.join("")}</${listStyle}>.

Possible Fix

Ensure that the item.items array is not empty before calling the recursor. Like so:

if (item.items.length) list = recursor(item.items, listStyle);

Current Output

Currently, when you make a single @editorjs/nested-list with text content, it creates a duplicate empty <${listStyle}></${listStyle}> pair before the closing </${listStyle}> tag.

This leads to outputted markup looking like this <${listStyle}> ${content} <${listStyle}></${listStyle}></${listStyle}>.

See this demo: image

Expected Output

Using the same input should only output <${listStyle}> ${content} </${listStyle}>. After applying the possible fix mentioned previously, this is the new output.

image