Git Product home page Git Product logo

node-quill-converter's Introduction

node-quill-converter NPM version

Convert HTML to a Quill Delta or a Quill Delta to HTML

The purpose of this package is to assist in migrating to or from the Quill editor.

Installation

# Via NPM
npm install node-quill-converter --save

# Via Yarn
yarn add node-quill-converter

Getting Started

Convert a plain text string to a Quill delta:

const { convertTextToDelta } = require('node-quill-converter');

let text = 'hello, world';
let delta = convertTextToDelta(text);

console.log(JSON.stringify(delta)); // {"ops":[{"insert":"hello, world\n"}]}

Convert a HTML string to a Quill delta:

const { convertHtmlToDelta } = require('node-quill-converter');

let htmlString = '<p>hello, <strong>world</strong></p>';
let delta = convertHtmlToDelta(htmlString);

console.log(JSON.stringify(delta); // {"ops":[{"insert":"hello, "},{"insert":"world","attributes":{"bold":true}}]}

Convert a Quill delta to an HTML string:

const { convertDeltaToHtml } = require('node-quill-converter');

let html = convertDeltaToHtml(delta);

console.log(html) ; // '<p>hello, <strong>world</strong></p>'

License

MIT License Copyright (c) 2018 Joel Colucci

node-quill-converter's People

Stargazers

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

Watchers

 avatar  avatar

node-quill-converter's Issues

No work with Vue

When I install this package and add to Import section: import { convertHtmlToDelta } from 'node-quill-converter';
I got this errors
Module not found: Error: Can't resolve 'child_process' in 'node_modules\jsdom\lib\jsdom\living'
@ ./node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js 4:22-46
@ ./node_modules/jsdom/lib/jsdom/browser/Window.js
@ ./node_modules/jsdom/lib/api.js
@ ./node_modules/node-quill-converter/lib/index.js

TypeScript typings

Hello,

Thanks for the project!
Could the npm package include TypeScript typings for the public API? This would benefit TypeScript users

convertHtmlToDelta does not create the exact delta that quill works with

Given I paste <h2>Hello</h2><p>World</p> into a quill instance running in a real browser. getContents will return

{
  ops: [
    { insert: "Hello" },
    { attributes: { header: 2 }, insert: "\n" },
    { insert: "World\n" }
  ]
}

while convertHtmlToDelta returns

{
  ops: [
    { attributes: { header: 2 }, insert: "Hello\n" },
    { insert: "World\n" }
  ]
}

This cause problems with dirty-checks when the contents of quill are used in a <form>.

Will submit a PR.

Style attribute not being returned

The Quill editor on my page saves both the delta and the HTML content to the DB. Since I don't want to load a Quill instance to display the HTML in view-only pages, i use the saved HTML(from my understanding having the HTML is better from an SEO perspective). This HTML page will then have an Edit button that would then open the editor in a modal where i use the saved delta from the DB to populate the editor. This works fine (with a fixed caveat, please see comments and code snippet for the fix at the end).

I also have a requirement to translate the content in the editor to a different language by tapping on an appropriate button. I use the Google translate API(on the server) and send it the HTML (innerHTML) from the editor and i get back proper translated HTML. Still on the server, just after the translation, i call convertHtmlToDelta with this translated HTML and send back the returned delta to the client. I then update the value of the editor with this new delta information and my editor shows the translated content. Works great and the whole operation seems almost seamless. But there seems to be just one issue: the styles attribute doesn't seem to be present in the delta returned from convertHtmlToDelta. Everything else remains intact thus far.

So assuming this is the original delta for an image that has a float style:

ops: [
....
{
attributes:{
link: "https://quilljs.com",
style: "display: inline; margin: 0px 1em 1em 0px; float: left;",
width: "181",
},
insert{
image: ""https://source.unsplash.com/featured?art"
}
...
]

This gets rendered to:

<img src="https://source.unsplash.com/featured?art" style="display: inline; margin: 0px 1em 1em 0px; float: left;" width="181" />
After convertHtmlToDelta the delta becomes

ops: [
....
{
attributes:{
link: "https://quilljs.com",
width: "181",
},
insert{
image: ""https://source.unsplash.com/featured?art"
}
...
]

I originally had the problem with the Quill editor itself when i was preloading it with delta from the DB and from some net research i found that i had to add width,alt, height and style to the formats object. Additionally i had to add the following snippet as well:

const ImageFormatAttributesList = [
    'alt',
    'height',
    'width',
    'style'
];

class ImageFormat extends BaseImageFormat {
  static formats(domNode) {
    return ImageFormatAttributesList.reduce(function(formats, attribute) {
      if (domNode.hasAttribute(attribute)) {
        formats[attribute] = domNode.getAttribute(attribute);
      }
      return formats;
    }, {});
  }
  format(name, value) {
    if (ImageFormatAttributesList.indexOf(name) > -1) {
      if (value) {
        this.domNode.setAttribute(name, value);
      } else {
        this.domNode.removeAttribute(name);
      }
    } else {
      super.format(name, value);
    }
  }
}

Quill.register(ImageFormat, true);

Is there some way i could get the styles attribute as well in the returned delta? What are the possibilities of any other attribute getting stripped similarly?

Round-tripped HTML sometimes leaves the Quill component in an unusable state.

const { convertHtmlToDelta, convertDeltaToHtml } = require('node-quill-converter');

const sampleData = [
  `<pre>Hola</pre>`,
  `<h3>Hello</h3>`,
];

sampleData.forEach((sample) => {
  const delta = convertHtmlToDelta(sample);
  const html = convertDeltaToHtml(delta);
  console.log('html', html);
});

Output:

html <pre class="ql-syntax" spellcheck="false">Hola
</pre>
about:blank:7
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Quill=e():t.Quill=e()} // SNIPPED MASSIVE JS DUMP
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

TypeError: Cannot read property 'length' of undefined
    at s.insert (about:blank:7:10439)
    at e.value (about:blank:7:158285)
    at e.value (about:blank:7:158101)
    at exports.convertHtmlToDelta (/Users/drarok/Development/project_name/node_modules/node-quill-converter/lib/index.js:53:37)
    at /Users/drarok/Development/project_name/wat.js:9:17
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/drarok/Development/project_name/wat.js:8:12)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)

TypeError: fs.readFileSync is not a function

I've just saved this package in my react project and imported like this:

import { convertHtmlToDelta, convertDeltaToHtml } from 'node-quill-converter';

But got this error:

Uncaught TypeError: fs.readFileSync is not a function at Object.<anonymous> (:4200/static/js/0.chunk.js:490006) at Object../node_modules/node-quill-converter/lib/index.js (:4200/static/js/0.chunk.js:490032) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/components/Editor.js (:4200/static/js/main.chunk.js:2326) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/components/admin/ModalityTemplateCRUD/ModalityTemplateForm.js (:4200/static/js/main.chunk.js:7473) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/components/admin/ModalityTemplateCRUD/index.js (:4200/static/js/main.chunk.js:7810) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/pages/AdminPage.js (:4200/static/js/main.chunk.js:22831) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/App.js (:4200/static/js/main.chunk.js:69) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Module../src/index.js (:4200/static/js/main.chunk.js:22779) at __webpack_require__ (:4200/static/js/bundle.js:782) at fn (:4200/static/js/bundle.js:150) at Object.0 (:4200/static/js/main.chunk.js:33635) at __webpack_require__ (:4200/static/js/bundle.js:782) at checkDeferredModules (:4200/static/js/bundle.js:46) at Array.webpackJsonpCallback [as push] (:4200/static/js/bundle.js:33) at :4200/static/js/main.chunk.js:1

Version:
0.3.2

Quill customizations

Hi,
I need to make some customizations to Quill and therefore I need to access Quill.

I can do it this way:

let SizeClass = DOM.window.Quill.import('attributors/class/size');
SizeClass.blotName = 'blockFontSize';
SizeClass.scope = Scope.BLOCK;

In order to run it I've modified your library doing:

exports.DOM = DOM;

to have access to Quill.import

Maybe a solution like this is useful for more people and you can include it.

Thanks!

Can't import js library 'node-quill-converter'

I 'm trying to install this library in a react-typescript app for quill node-quill-converte but i can't import it it exists in my node_modules folder after successful installation but while trying

import {convertTextToDelta} from 'node-quill-converter-improved'

It gets me an error that this path to library doesn't exist i 've also tried

const { convertTextToDelta } = require('node-quill-converter-improved');

it shows me this error

TypeError: fs.readFileSync is not a function

(anonymous function)
node_modules/node-quill-converter-improved/lib/index.js:10
7 | let quillFilePath = require.resolve("quill");
8 | let quillMinFilePath = quillFilePath.replace("quill.js", "quill.min.js");
9 |

10 | let quillLibrary = fs.readFileSync(quillMinFilePath);
11 | let mutationObserverPolyfill = fs.readFileSync(
12 | path.join(__dirname, "polyfill.js")
13 | );

Memory leak just including the module

node.js 10.10.0
npm 6.4.1
express: 4.17.1

Memory usage went from 60MB (normal) to 450MB over 9 hours by simply including the module:

const quillConverter = require('node-quill-converter');

Screen Shot 2019-07-17 at 3 23 55 PM

Can't save it as a dev dependency

Trying npm install -D @types/node-quill-converter gets me this error
`npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2Fnode-quill-converter - Not found
npm ERR! 404
npm ERR! 404 '@types/node-quill-converter@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
`

Only work with node

This library only works with nodejs, not pure JS. Could be nice to have it works with JS :)

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.