Code Monkey home page Code Monkey logo

quill-render's Introduction

quill-render

Build Status

Renders a sequence of quill insert-only deltas (operational transforms) into HTML with no browser dependencies.

This is accomplished by using cheerio to build a DOM from the delta sequence.

One way to get a document's insert-only deltas is by calling Quill#getContents() in the browser.

Example

var render = require('quill-render');
render([
    {
        "attributes": {
            "bold": true
        },
        "insert": "Hi mom"
    }
]);
// => '<b>Hi mom</b>'

If you want the cheerio object instead of the HTML, use render.asDOM().

var $ = render.asDOM([...]);
// ... DOM surgery ...

Extending

quill-render exposes the functions it uses to produce markup so that you can tweak it to fit your needs. You can change the behavior of the builtin formats or create your own.

Each function is passed $ (the root cheerio object) and the value of the format.

For inline styles, return a new element. The contents of the insert will be appended as children of the element.

render.format.inline.bold = function($/*, formatValue */) {
    return $('<b>').addClass('super-important');
};
// above example would now return '<b class="super-important">Hi mom</b>'

For 'block' styles (those deltas where insert === 1 -- ie images), this will be the block container (a <p>). Append elements to it.

render.format.block.image = function($, src) {
    var img = $('<img>');
    img.attr('src', src);
    this.append(img);
};

For line styles (those deltas where insert == '\n' and the format is meant to apply to the entire line), this will be the line's container (a p.)

render.format.lineify.h1 = function(/* $, formatValue */) {
    this[0].name = 'h1'; // change the line container tag to a h1
};

You can also specify a grouping tag. When multiple lines with the same format appear is sequence, the entire group is wrapped in the group element. (ie lists)

render.format.lineify.bullet = {
    group: function($) {
        return $('<ul>'); // return the element that will be the parent of all group members
    },
    line: function() {
        this[0].name = 'li'; // make each group member a li
    }
};

quill-render's People

Contributors

daguej avatar

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

Watchers

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

quill-render's Issues

Links on images crash quill-render

In casetext/cases-on-fire#1483, we encountered an issue with an unpublishable draft. The stack trace was:

Aug 03 10:54:16 casetext app/worker.1:  [571637 2015-08-03T17:54:16.372Z] ERROR publish  Duration: 122 ms  Memory Estimate*: - 
Aug 03 10:54:16 casetext app/worker.1:  [571637 2015-08-03T17:54:16.372Z] TypeError: Cannot call method 'call' of undefined 
Aug 03 10:54:16 casetext app/worker.1:      at Function.convert [as asDOM] (/app/node_modules/quill-render/index.js:77:21) 
Aug 03 10:54:16 casetext app/worker.1:      at buildDocumentDom (/app/tasks/functions/publish/publish.js:114:25) 
Aug 03 10:54:16 casetext app/worker.1:      at _fulfilled (/app/node_modules/q/q.js:834:54) 
Aug 03 10:54:16 casetext app/worker.1:      at self.promiseDispatch.done (/app/node_modules/q/q.js:863:30) 
Aug 03 10:54:16 casetext app/worker.1:      at Promise.promise.promiseDispatch (/app/node_modules/q/q.js:796:13) 
Aug 03 10:54:16 casetext app/worker.1:      at /app/node_modules/q/q.js:604:44 
Aug 03 10:54:16 casetext app/worker.1:      at runSingle (/app/node_modules/q/q.js:137:13) 
Aug 03 10:54:16 casetext app/worker.1:      at flush (/app/node_modules/q/q.js:125:13) 
Aug 03 10:54:16 casetext app/worker.1:      at process._tickDomainCallback (node.js:492:13) 

The draft had an image with a link attribute set on it because it was copy-pasted in. When we removed the link attribute from the image, the draft published OK.

Lists sometimes combined

IE:


  1. foo
  2. bar
  3. baz

normal text

  1. another list
  2. foo
  3. bar

is rendered as


  1. foo
  2. bar
  3. baz
  4. another list
  5. foo
  6. bar

normal text


Using quill-render for html mails

I really like the simplicity of your module and the fact that it uses cheerio. I cant seem to get rendering of span tags with font style inside the p tags working. Any hints?

Ideally

[{
    "attributes": {
        "font": "Georgia, serif"
    },
    "insert": "Hi mom"
}]

would render

<p><span style="font-family:Georgia, serif">Hi mom</span></p>

Align is not supported?

Looks like the render function is ignoring completely alignment attributes. Is it possible to maintain this?

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.