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

quill-delta-from-html

v1.0.5

Published

Convert easily HTML inputs content to Quill Js Delta format

Downloads

2,482

Readme

Quill Delta from HTML

This is a package that converts HTML input into Quill Delta format, which is used in the Quill Js package.

This package supports the conversion of a wide range of HTML tags and attributes into their corresponding Delta operations, ensuring that your HTML content is accurately represented in the Quill editor.

Supported tags

    <!--Text Formatting-->
        <b>, <strong>: Bold text
        <i>, <em>: Italic text
        <u>, <ins>: Underlined text
        <s>, <del>: Strikethrough text
        <sup>: Superscript text
        <sub>: Subscript text

    <!--Headings-->
        <h1> to <h6>: Headings of various levels

    <!--Lists and nested ones-->
        <ul>: Unordered lists
        <ol>: Ordered lists
        <li>: List items
        <li data-checked="true">: Check lists
        <input type="checkbox">: Another alternative to make a check lists

    <!--Links-->
        <a>: Hyperlinks with support for the href attribute

    <!--Images-->
        <img>: Images with support for the src, align, and styles

    <!--div-->
        <div>: HTML tag containers

    <!--Videos -->
        <video>: Videos with support for the src

    <!--Blockquotes-->
        <blockquote>: Block quotations

    <!--Code Blocks-->
        <pre>, <code>: Code blocks

    <!--Text Alignment, inline text align and direction-->
        <p style="text-align:left|center|right|justify">: Paragraph style alignment
        <p align="left|center|right|justify">: Paragraph alignment
        <p dir="rtl">: Paragraph direction

    <!--Text attributes-->
        <p style="padding: 10px;font-size: 12px;font-family: Times New Roman;color:#ffffff">: Inline attributes

    <!--Custom Blocks-->
        <pullquote data-author="john">: Custom html

Quick Example

import { HtmlToDelta } from 'quill-delta-from-html';

const htmlContent: string = '<p>Hello, <b>world</b>!</p>';
const delta = new HtmlToDelta(<your_custom_blocks>, <YourHtmlToOperationsImpl>, <your_black_nodes_list>).convert(htmlContent);

/*
   { "insert": "hello, " },
   { "insert": "world", "attributes": {"bold": true} },
   { "insert": "!\n" }
*/

Creating your own CustomHtmlPart

What is a CustomHtmlPart?

CustomHtmlPartis a special class that let us convert custom HTML elements in Operation to Quill Delta.

First you need to define your own CustomHtmlPart

import { CustomHtmlPart } from 'quill-delta-from-html';
import { HTMLElement } from 'node-html-parser';
import Delta, { AttributeMap, Op } from 'quill-delta';

/// Custom block handler for <pullquote> elements.
class PullquoteBlock extends CustomHtmlPart {
  matches(element: HTMLElement): boolean {
    // you can put here the validation that you want
    // To detect a <p>, you just need to do something like:
    // element.rawTagName === 'p'
    return element.rawTagName === 'pullquote';
  }

  convert(element: HTMLElement, currentAttributes: AttributeMap): Op[] {
    const delta: Delta = new Delta();
    const attributes = { ...currentAttributes };

    // Extract custom attributes from the <pullquote> element
    // The attributes represent the data into a html tag
    // at this point, <pullquote> should have these attributes
    //
    // <pullquote data-author="John Doe" data-style="italic">
    // These attributes can be optional, so ensure not to use "!" to avoid any null conflict
    const author = element.attribs['data-author'];
    const style = element.attribs['data-style'];

    // Apply custom attributes to the Delta operations
    if (author) {
      delta.insert(
        `Pullquote: "${element.children[0].data}" by ${author}`,
        attributes,
      );
    } else {
      delta.insert(`Pullquote: "${element.children[0].data}"`, attributes);
    }

    if (style && style.toLowerCase() === 'italic') {
      attributes['italic'] = true;
    }
    return delta;
  }
}

After, put your PullquoteBlock to HtmlToDelta using the param customBlocks

import { HtmlToDelta } from 'quill-delta-from-html';

const htmlText: string = `
  <html>
    <body>
      <p>Regular paragraph before the custom block</p>
      <pullquote data-author="John Doe" data-style="italic">This is a custom pullquote</pullquote>
      <p>Regular paragraph after the custom block</p>
    </body>
  </html>
`;

// Registering the custom block
const customBlocks = [new PullquoteBlock()];

// Initialize HtmlToDelta with the HTML text and custom blocks
const converter = new HtmlToDelta(customBlocks);

// Convert HTML to Delta operations
const delta = converter.convert(htmlText);

/*
This should be resulting delta
  {"insert": "Regular paragraph before the custom block"},
  {"insert": "Pullquote: \"This is a custom pullquote\" by John Doe", "attributes": {"italic": true}},
  {"insert": "\n"},
  {"insert": "Regular paragraph after the custom block\n"}
*/

HtmlOperations

The HtmlOperations class is designed to streamline the conversion process from HTML to Delta operations, accommodating a wide range of HTML structures and attributes commonly used in web content.

To utilize HtmlOperations, extend this class and implement the methods necessary to handle specific HTML elements. Each method corresponds to a different HTML tag or element type and converts it into Delta operations suitable for use with Quill JS.

abstract class HtmlOperations {
  // customBlocks are passed internally by HtmlToDelta
  protected customBlocks?: CustomHtmlPart[];

  // You don't need to override this method
  // as it simply calls the other methods
  // to detect the type of HTML tag
  resolveCurrentElement(
    element: dom.Element,
    indentLevel?: number,
  ): Operation[];

  abstract brToOp(element: dom.Element): Operation[];
  abstract headerToOp(element: dom.Element): Operation[];
  abstract listToOp(element: dom.Element, indentLevel?: number): Operation[];
  abstract paragraphToOp(element: dom.Element): Operation[];
  abstract linkToOp(element: dom.Element): Operation[];
  abstract spanToOp(element: dom.Element): Operation[];
  abstract imgToOp(element: dom.Element): Operation[];
  abstract videoToOp(element: dom.Element): Operation[];
  abstract codeblockToOp(element: dom.Element): Operation[];
  abstract blockquoteToOp(element: dom.Element): Operation[];
}

Passing a custom HtmlOperations implementation

import { HtmlToDelta } from 'quill-delta-from-html';

const converter = new HtmlToDelta(...your_custom_blocks, <YourHtmlToOperationsImpl>);

Contributions

If you find a bug or want to add a new feature, please open an issue or submit a pull request on the GitHub repository.

This project is licensed under the MIT License - see the LICENSE file for details.