Skip to content

Commit 7cb33b5

Browse files
committed
Added Superscript and Subscript formats
1 parent 37c8d31 commit 7cb33b5

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fmt.Println(string(html))
3030
- Italic
3131
- Link
3232
- Strikethrough
33+
- Superscript/Subscript
3334
- Underline
3435

3536
### Block

inline_formats.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,20 @@ func (bf *bkgFormat) Fmt() *Format {
139139
func (*bkgFormat) HasFormat(o *Op) bool {
140140
return o.HasAttr("background")
141141
}
142+
143+
// script (sup and sub)
144+
145+
type scriptFormat struct {
146+
t string
147+
}
148+
149+
func (sf *scriptFormat) Fmt() *Format {
150+
return &Format{
151+
Val: sf.t,
152+
Place: Tag,
153+
}
154+
}
155+
156+
func (*scriptFormat) HasFormat(o *Op) bool {
157+
return o.HasAttr("script")
158+
}

render.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ func (o *Op) getFormatter(keyword string, customFormats func(string, *Op) Format
360360
return &bkgFormat{
361361
c: o.Attrs["background"],
362362
}
363+
case "script":
364+
sf := new(scriptFormat)
365+
if o.Attrs["script"] == "super" {
366+
sf.t = "sup"
367+
} else {
368+
sf.t = "sub"
369+
}
370+
return sf
363371
}
364372

365373
return nil

render_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ func TestSimple(t *testing.T) {
2121
`[{"insert":"text "},{"insert":{"image":"source-url"}},{"insert":" more text\n"}]`, // image
2222
`[{"insert":"abc "},{"attributes":{"background":"#66a3e0"},"insert":"colored"},{"insert":" plain\n"}]`, // background
2323
`[{"attributes":{"underline":true},"insert":"underlined"},{"insert":"\n"}]`, // underlined
24+
`[{"insert":"plain"},{"attributes":{"script":"super"},"insert":"super"},{"insert":"\n"}]`, // superscript
25+
`[{"insert":"plain"},{"attributes":{"script":"sub"},"insert":"sub"},{"insert":"\n"}]`, // subscript
2426
}
2527

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

4044
for i := range cases {

0 commit comments

Comments
 (0)