Skip to content

Commit

Permalink
Added Superscript and Subscript formats
Browse files Browse the repository at this point in the history
  • Loading branch information
dchenk committed Jan 5, 2018
1 parent 37c8d31 commit 7cb33b5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fmt.Println(string(html))
- Italic
- Link
- Strikethrough
- Superscript/Subscript
- Underline

### Block
Expand Down
17 changes: 17 additions & 0 deletions inline_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,20 @@ func (bf *bkgFormat) Fmt() *Format {
func (*bkgFormat) HasFormat(o *Op) bool {
return o.HasAttr("background")
}

// script (sup and sub)

type scriptFormat struct {
t string
}

func (sf *scriptFormat) Fmt() *Format {
return &Format{
Val: sf.t,
Place: Tag,
}
}

func (*scriptFormat) HasFormat(o *Op) bool {
return o.HasAttr("script")
}
8 changes: 8 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ func (o *Op) getFormatter(keyword string, customFormats func(string, *Op) Format
return &bkgFormat{
c: o.Attrs["background"],
}
case "script":
sf := new(scriptFormat)
if o.Attrs["script"] == "super" {
sf.t = "sup"
} else {
sf.t = "sub"
}
return sf
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestSimple(t *testing.T) {
`[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`, // image
`[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"colored"},{"insert":" plain\n"}]`, // background
`[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`, // underlined
`[{"insert":"plain"},{"attributes":{"script":"super"},"insert":"super"},{"insert":"\n"}]`, // superscript
`[{"insert":"plain"},{"attributes":{"script":"sub"},"insert":"sub"},{"insert":"\n"}]`, // subscript
}

want := []string{
Expand All @@ -35,6 +37,8 @@ func TestSimple(t *testing.T) {
`<p>text <img src="source-url"> more text</p>`,
`<p>abc <span style="background-color:#66a3e0;">colored</span> plain</p>`,
"<p><u>underlined</u></p>",
"<p>plain<sup>super</sup></p>",
"<p>plain<sub>sub</sub></p>",
}

for i := range cases {
Expand Down

0 comments on commit 7cb33b5

Please sign in to comment.