Implemented strikethrough text

This commit is contained in:
Eduard Urbach 2024-04-02 20:50:33 +02:00
parent 3b776ef8cd
commit d7fd8c74e8
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 29 additions and 5 deletions

View File

@ -5,7 +5,7 @@ A markdown renderer that supports only a subset of the CommonMark spec in order
## Features
- Code blocks
- Formatting (bold, italic, monospace)
- Formatting (bold, italic, monospace and strikethrough)
- Links
- Lists
- Headers
@ -34,6 +34,7 @@ PASS: TestParagraph
PASS: TestHeader
PASS: TestItalic
PASS: TestBold
PASS: TestStrike
PASS: TestLink
PASS: TestList
PASS: TestTables
@ -48,9 +49,9 @@ coverage: 100.0% of statements
## Benchmarks
```
BenchmarkSmall-12 5986922 187.5 ns/op 32 B/op 1 allocs/op
BenchmarkMedium-12 1000000 1077 ns/op 512 B/op 1 allocs/op
BenchmarkLarge-12 255178 4055 ns/op 2560 B/op 2 allocs/op
BenchmarkSmall-12 6202569 190.9 ns/op 32 B/op 1 allocs/op
BenchmarkMedium-12 1000000 1080 ns/op 512 B/op 1 allocs/op
BenchmarkLarge-12 271048 4115 ns/op 2560 B/op 2 allocs/op
```
## License

View File

@ -257,11 +257,12 @@ func (r *renderer) writeText(markdown string) {
codeStart = -1
emStart = -1
strongStart = -1
strikeStart = -1
parentheses = 0
)
for {
i := strings.IndexAny(markdown[searchStart:], "[]()`*_")
i := strings.IndexAny(markdown[searchStart:], "[]()`*_~")
if i == -1 {
r.WriteString(html.EscapeString(markdown[tokenStart:]))
@ -342,6 +343,23 @@ func (r *renderer) writeText(markdown string) {
tokenStart = i
emStart = i + 1
}
case '~':
if i+1 >= len(markdown) || markdown[i+1] != '~' {
continue
}
if strikeStart != -1 {
r.WriteString("<del>")
r.WriteString(html.EscapeString(markdown[strikeStart:i]))
r.WriteString("</del>")
strikeStart = -1
tokenStart = i + 2
} else {
r.WriteString(html.EscapeString(markdown[tokenStart:i]))
tokenStart = i
strikeStart = i + 2
}
}
}
}

View File

@ -39,6 +39,11 @@ func TestBold(t *testing.T) {
assert.Equal(t, markdown.Render("__bold__"), "<p><strong>bold</strong></p>")
}
func TestStrike(t *testing.T) {
assert.Equal(t, markdown.Render("~normal text~"), "<p>~normal text~</p>")
assert.Equal(t, markdown.Render("~~deleted text~~"), "<p><del>deleted text</del></p>")
}
func TestLink(t *testing.T) {
assert.Equal(t, markdown.Render("[text](https://example.com/)"), "<p><a href=\"https://example.com/\">text</a></p>")
assert.Equal(t, markdown.Render("[text](https://example.com/"), "<p>[text](https://example.com/</p>")