diff --git a/README.md b/README.md index b0d3057..d724855 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Render.go b/Render.go index c8cd507..72472be 100644 --- a/Render.go +++ b/Render.go @@ -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("") + r.WriteString(html.EscapeString(markdown[strikeStart:i])) + r.WriteString("") + strikeStart = -1 + tokenStart = i + 2 + } else { + r.WriteString(html.EscapeString(markdown[tokenStart:i])) + tokenStart = i + strikeStart = i + 2 + } } } } diff --git a/Render_test.go b/Render_test.go index 33c2be7..4e98dc2 100644 --- a/Render_test.go +++ b/Render_test.go @@ -39,6 +39,11 @@ func TestBold(t *testing.T) { assert.Equal(t, markdown.Render("__bold__"), "

bold

") } +func TestStrike(t *testing.T) { + assert.Equal(t, markdown.Render("~normal text~"), "

~normal text~

") + assert.Equal(t, markdown.Render("~~deleted text~~"), "

deleted text

") +} + func TestLink(t *testing.T) { assert.Equal(t, markdown.Render("[text](https://example.com/)"), "

text

") assert.Equal(t, markdown.Render("[text](https://example.com/"), "

[text](https://example.com/

")