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 ## Features
- Code blocks - Code blocks
- Formatting (bold, italic, monospace) - Formatting (bold, italic, monospace and strikethrough)
- Links - Links
- Lists - Lists
- Headers - Headers
@ -34,6 +34,7 @@ PASS: TestParagraph
PASS: TestHeader PASS: TestHeader
PASS: TestItalic PASS: TestItalic
PASS: TestBold PASS: TestBold
PASS: TestStrike
PASS: TestLink PASS: TestLink
PASS: TestList PASS: TestList
PASS: TestTables PASS: TestTables
@ -48,9 +49,9 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkSmall-12 5986922 187.5 ns/op 32 B/op 1 allocs/op BenchmarkSmall-12 6202569 190.9 ns/op 32 B/op 1 allocs/op
BenchmarkMedium-12 1000000 1077 ns/op 512 B/op 1 allocs/op BenchmarkMedium-12 1000000 1080 ns/op 512 B/op 1 allocs/op
BenchmarkLarge-12 255178 4055 ns/op 2560 B/op 2 allocs/op BenchmarkLarge-12 271048 4115 ns/op 2560 B/op 2 allocs/op
``` ```
## License ## License

View File

@ -257,11 +257,12 @@ func (r *renderer) writeText(markdown string) {
codeStart = -1 codeStart = -1
emStart = -1 emStart = -1
strongStart = -1 strongStart = -1
strikeStart = -1
parentheses = 0 parentheses = 0
) )
for { for {
i := strings.IndexAny(markdown[searchStart:], "[]()`*_") i := strings.IndexAny(markdown[searchStart:], "[]()`*_~")
if i == -1 { if i == -1 {
r.WriteString(html.EscapeString(markdown[tokenStart:])) r.WriteString(html.EscapeString(markdown[tokenStart:]))
@ -342,6 +343,23 @@ func (r *renderer) writeText(markdown string) {
tokenStart = i tokenStart = i
emStart = i + 1 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>") 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) { 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><a href=\"https://example.com/\">text</a></p>")
assert.Equal(t, markdown.Render("[text](https://example.com/"), "<p>[text](https://example.com/</p>") assert.Equal(t, markdown.Render("[text](https://example.com/"), "<p>[text](https://example.com/</p>")