Skip to content

Commit ed86dcb

Browse files
committed
Improved documentation, added strike and background formats
1 parent aaef254 commit ed86dcb

File tree

5 files changed

+106
-14
lines changed

5 files changed

+106
-14
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: go
2+
3+
go:
4+
- 1.9

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,49 @@
22

33
Package `quill` takes a Quill-based Delta (https://github.com/quilljs/delta) as a JSON array of `insert` operations
44
and renders the defined HTML document.
5+
6+
Complete documentation at GoDoc: [https://godoc.org/github.com/dchenk/go-render-quill](https://godoc.org/github.com/dchenk/go-render-quill)
7+
8+
## Usage
9+
10+
```
11+
import "github.com/dchenk/go-render-quill"
12+
13+
var delta = `[{"insert":"This "},{"attributes":{"italic":true},"insert":"is"},
14+
{"insert":" "},{"attributes":{"bold":true},"insert":"great!"},{"insert":"\n"}]`
15+
16+
html, err := quill.Render(delta)
17+
if err != nil {
18+
panic(err)
19+
}
20+
fmt.Println(string(html))
21+
// Output: <p>This <em>is</em> <strong>great!</strong></p>
22+
```
23+
24+
## Supported Formats
25+
26+
### Inline
27+
- Background color
28+
- Bold
29+
- Text color
30+
- Italic
31+
- Link
32+
- Strikethrough
33+
- Underline
34+
35+
### Block
36+
- Blockquote
37+
- Header
38+
- Indent
39+
- List (ul and ol, including nested lists)
40+
- Text alignment
41+
42+
### Embeds
43+
- Image (an inline format)
44+
45+
## Extending
46+
47+
The simple `Formatter` interface is all you need to implement for most block and inline formats. Instead of `Render` use `RenderExtended`
48+
and provide a function that returns a `Formatter` for inserts that have the format you need.
49+
50+
For more control, you can also implement `FormatWriter` or `FormatWrapper`.

inline_formats.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,33 @@ func (imf *imageFormat) Write(buf io.Writer) {
109109
buf.Write([]byte{'>'})
110110

111111
}
112+
113+
// strikethrough
114+
type strikeFormat struct{}
115+
116+
func (*strikeFormat) Fmt() *Format {
117+
return &Format{
118+
Val: "s",
119+
Place: Tag,
120+
}
121+
}
122+
123+
func (*strikeFormat) HasFormat(o *Op) bool {
124+
return o.HasAttr("strike")
125+
}
126+
127+
// background
128+
type bkgFormat struct {
129+
c string
130+
}
131+
132+
func (bf *bkgFormat) Fmt() *Format {
133+
return &Format{
134+
Val: "background-color:" + bf.c + ";",
135+
Place: Style,
136+
}
137+
}
138+
139+
func (*bkgFormat) HasFormat(o *Op) bool {
140+
return o.HasAttr("background")
141+
}

render.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package `quill` takes a Quill-based Delta (https://github.com/quilljs/delta) as a JSON array of `insert` operations
1+
// Package quill takes a Quill-based Delta (https://github.com/quilljs/delta) as a JSON array of `insert` operations
22
// and renders the defined HTML document.
33
//
44
// This library is designed to be easily extendable. Simply call RenderExtended with a function that may provide its
@@ -354,6 +354,12 @@ func (o *Op) getFormatter(keyword string, customFormats func(string, *Op) Format
354354
return &indentFormat{
355355
in: o.Attrs["indent"],
356356
}
357+
case "strike":
358+
return new(strikeFormat)
359+
case "background":
360+
return &bkgFormat{
361+
c: o.Attrs["background"],
362+
}
357363
}
358364

359365
return nil
@@ -375,7 +381,7 @@ type Formatter interface {
375381
HasFormat(*Op) bool // Say if the Op has the Format that Fmt returns.
376382
}
377383

378-
// A Formatter may also be a FormatWriter if it wishes to write the body of the Op in some custom way (useful for embeds).
384+
// A FormatWriter can write the body of an Op in a custom way (useful for embeds).
379385
type FormatWriter interface {
380386
Formatter
381387
Write(io.Writer) // Write the entire body of the element.

render_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ import (
1010
func TestSimple(t *testing.T) {
1111

1212
cases := []string{
13-
`[{"insert": "\n"}]`,
14-
`[{"insert": "line1\nline2\n"}]`,
15-
`[{"insert": "line1\n\nline3\n"}]`,
16-
`[{"insert": "bkqt"}, {"attributes": {"blockquote": true}, "insert": "\n"}]`,
17-
`[{"attributes": {"color": "#a10000"}, "insert": "colored"}, {"insert": "\n"}]`,
18-
`[{"insert":"abc "},{"attributes":{"bold":true},"insert":"bld"},{"attributes":{"list":"bullet"},"insert":"\n"}]`,
19-
`[{"insert":{"image":"source-url"}},{"insert":"\n"}]`,
20-
`[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`,
13+
`[{"insert": "\n"}]`, // blank
14+
`[{"insert": "line1\nline2\n"}]`, // two paragraphs (single op)
15+
`[{"insert": "line1\n\nline3\n"}]`, // blank line
16+
`[{"insert": "bkqt"}, {"attributes": {"blockquote": true}, "insert": "\n"}]`, // blockquote
17+
`[{"attributes": {"color": "#a10000"}, "insert": "colored"}, {"insert": "\n"}]`, // color
18+
`[{"attributes":{"strike":true},"insert":"striked"},{"insert":"\n"}]`, // strikethrough
19+
`[{"insert":"abc "},{"attributes":{"bold":true},"insert":"bld"},{"attributes":{"list":"bullet"},"insert":"\n"}]`, // list
20+
`[{"insert":{"image":"source-url"}},{"insert":"\n"}]`, // image
21+
`[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`, // image
22+
`[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"colored"},{"insert":" plain\n"}]`, // background
23+
`[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`, // underlined
2124
}
2225

2326
want := []string{
@@ -26,9 +29,12 @@ func TestSimple(t *testing.T) {
2629
"<p>line1</p><p><br></p><p>line3</p>",
2730
"<blockquote>bkqt</blockquote>",
2831
`<p><span style="color:#a10000;">colored</span></p>`,
32+
"<p><s>striked</s></p>",
2933
"<ul><li>abc <strong>bld</strong></li></ul>",
3034
`<p><img src="source-url"></p>`,
3135
`<p>text <img src="source-url"> more text</p>`,
36+
`<p>abc <span style="background-color:#66a3e0;">colored</span> plain</p>`,
37+
"<p><u>underlined</u></p>",
3238
}
3339

3440
for i := range cases {
@@ -61,18 +67,18 @@ func TestRender(t *testing.T) {
6167
func testPair(opsFile, htmlFile string) error {
6268
ops, err := ioutil.ReadFile("./tests/" + opsFile)
6369
if err != nil {
64-
return fmt.Errorf("could not read %s; %s\n", opsFile, err)
70+
return fmt.Errorf("could not read %s; %s", opsFile, err)
6571
}
6672
html, err := ioutil.ReadFile("./tests/" + htmlFile)
6773
if err != nil {
68-
return fmt.Errorf("could not read %s; %s\n", htmlFile, err)
74+
return fmt.Errorf("could not read %s; %s", htmlFile, err)
6975
}
7076
got, err := Render(ops)
7177
if err != nil {
72-
return fmt.Errorf("error rendering; %s\n", err)
78+
return fmt.Errorf("error rendering; %s", err)
7379
}
7480
if !bytes.Equal(html, got) {
75-
return fmt.Errorf("bad rendering; \nwanted: \n%s\ngot: \n%s\n", html, got)
81+
return fmt.Errorf("bad rendering; \nwanted: \n%s\ngot: \n%s", html, got)
7682
}
7783
return nil
7884
}

0 commit comments

Comments
 (0)