quill

package module
v0.0.0-...-0827c70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 8 Imported by: 0

README

go-render-quill

Build Status FOSSA Status

Package quill takes a Quill-based Delta (https://github.com/quilljs/delta) as a JSON array of insert operations and renders the defined HTML document.

Complete documentation at GoDoc: https://godoc.org/github.com/dchenk/go-render-quill

Usage

import "github.com/dchenk/go-render-quill"

var delta = `[{"insert":"This "},{"attributes":{"italic":true},"insert":"is"},
    {"insert":" "},{"attributes":{"bold":true},"insert":"great!"},{"insert":"\n"}]`

html, err := quill.Render(delta)
if err != nil {
	panic(err)
}
fmt.Println(string(html))
// Output: <p>This <em>is</em> <strong>great!</strong></p>

Supported Formats

Inline
  • Background color
  • Bold
  • Text color
  • Italic
  • Link
  • Size
  • Strikethrough
  • Superscript/Subscript
  • Underline
Block
  • Blockquote
  • Header
  • Indent
  • List (ul and ol, including nested lists)
  • Text alignment
  • Code block
Embeds
  • Image (an inline format)

Extending

The simple Formatter interface is all you need to implement for most block and inline formats. Instead of Render use RenderExtended and provide a function that returns a Formatter for inserts that have the format you need.

For more control, you can also implement FormatWriter or FormatWrapper.

Documentation

Overview

Package quill takes a Quill-based Delta (https://github.com/quilljs/delta) as a JSON array of `insert` operations and renders the defined HTML document.

This library is designed to be easily extendable. Simply call RenderExtended with a function that may provide its own formats for certain kinds of ops and attributes.

Example
package main

import (
	"fmt"

	"github.com/dchenk/go-render-quill"
)

var ops = []byte(`
[
	{
		"insert": "Heading1"
	},
	{
		"attributes": {
			"header": 1
		},
		"insert": "\n"
	},
	{
		"insert": "Hello, this is text.\nAnd "
	},
	{
		"attributes": {
			"italic": true
		},
		"insert": "here is italic "
	},
	{
		"insert": "(and not).\nAnd "
	},
	{
		"attributes": {
			"bold": true
		},
		"insert": "here is bold"
	},
	{
		"insert": "\n"
	}
]`)

func main() {
	html, err := quill.Render(ops)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(html))
}
Output:

<h1>Heading1</h1><p>Hello, this is text.</p><p>And <em>here is italic </em>(and not).</p><p>And <strong>here is bold</strong></p>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(ops []byte) ([]byte, error)

Render takes a Delta array of insert operations and returns the rendered HTML using the built-in settings. If an error occurs while rendering, any HTML already rendered is returned.

func RenderExtended

func RenderExtended(ops []byte, customFormats func(string, *Op) Formatter) ([]byte, error)

RenderExtended takes a Delta array of insert operations and, optionally, a function that may provide a Formatter to customize the way certain kinds of inserts are rendered, and returns the rendered HTML. If the given Formatter is nil, then the default one that is built in is used. If an error occurs while rendering, any HTML already rendered is returned.

Types

type Format

type Format struct {
	Val   string      // the value to print
	Place FormatPlace // where this format is placed in the text
	Block bool        // indicate whether this is a block-level format (not printed until a "\n" is reached)
	// contains filtered or unexported fields
}

A Format specifies how styling to text is applied. The Val string is what is printed in the place given by Place. Block indicates if this is a block-level format.

type FormatPlace

type FormatPlace uint8

A FormatPlace is either an HTML tag name, a CSS class, or a style attribute value.

const (
	Tag FormatPlace = iota
	Class
	Style
)

type FormatWrapper

type FormatWrapper interface {
	Formatter
	Wrap() (pre, post string)        // Say what opening and closing wraps will be written.
	Open([]*Format, *Op) bool        // Given the open formats and current Op, say if to write the pre string.
	Close([]*Format, *Op, bool) bool // Given the open formats, current Op, and if the Op closes a block, say if to write the post string.
}

A FormatWrapper wraps text with additional text of any kind (such as "<ul>" for lists).

type FormatWriter

type FormatWriter interface {
	Formatter
	Write(io.Writer) // Write the entire body of the element.
}

A FormatWriter can write the body of an Op in a custom way (useful for embeds).

type Formatter

type Formatter interface {
	Fmt() *Format       // Format gives the string to write and where to place it.
	HasFormat(*Op) bool // Say if the Op has the Format that Fmt returns.
}

A Formatter is able to give a Format and say whether a given Op should have that Format applied.

type Op

type Op struct {
	Data    string // the text to insert or the value of the embed object (http://quilljs.com/docs/delta/#embeds)
	DataMap map[string]string
	Type    string            // the type of the op (typically "text", but any other type can be registered)
	Attrs   map[string]string // key is attribute name; value is either the attribute value or "y" (meaning true)
}

An Op is a Delta insert operations (https://github.com/quilljs/delta#insert) that has been converted into this format for usability with the type safety in Go.

func (*Op) HasAttr

func (o *Op) HasAttr(attr string) bool

HasAttr says if the Op is not nil and has the attribute set to a non-blank value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL