Implemented ordered lists

This commit is contained in:
Eduard Urbach 2024-04-03 09:23:52 +02:00
parent 1c7f9f2f7d
commit 463c8b4a85
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 40 additions and 5 deletions

View File

@ -51,9 +51,9 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkSmall-12 5933677 190.8 ns/op 32 B/op 1 allocs/op BenchmarkSmall-12 5836533 205.3 ns/op 32 B/op 1 allocs/op
BenchmarkMedium-12 971929 1085 ns/op 512 B/op 1 allocs/op BenchmarkMedium-12 967740 1103 ns/op 512 B/op 1 allocs/op
BenchmarkLarge-12 277028 4075 ns/op 2560 B/op 2 allocs/op BenchmarkLarge-12 277908 4099 ns/op 2560 B/op 2 allocs/op
``` ```
## License ## License

View File

@ -17,6 +17,7 @@ type renderer struct {
paragraphLevel int paragraphLevel int
quoteLevel int quoteLevel int
listLevel int listLevel int
olistLevel int
tableLevel int tableLevel int
codeLines int codeLines int
tableHeaderWritten bool tableHeaderWritten bool
@ -200,6 +201,26 @@ func (r *renderer) processLine(line string) {
} }
} }
pos := 0
for pos < len(line) && line[pos] >= '0' && line[pos] <= '9' {
pos++
if pos < len(line) && (line[pos] == '.' || line[pos] == ')') {
line = strings.TrimSpace(line[pos+1:])
if r.olistLevel == 0 {
r.WriteString("<ol>")
r.olistLevel++
}
r.WriteString("<li>")
r.writeText(line)
r.WriteString("</li>")
return
}
}
if r.paragraphLevel == 0 { if r.paragraphLevel == 0 {
r.WriteString("<p>") r.WriteString("<p>")
r.paragraphLevel++ r.paragraphLevel++
@ -233,7 +254,12 @@ func (r *renderer) closeLists() {
r.WriteString("</ul>") r.WriteString("</ul>")
} }
for range r.olistLevel {
r.WriteString("</ol>")
}
r.listLevel = 0 r.listLevel = 0
r.olistLevel = 0
} }
// closeTables closes open tables. // closeTables closes open tables.

View File

@ -55,10 +55,19 @@ func TestLink(t *testing.T) {
} }
func TestList(t *testing.T) { func TestList(t *testing.T) {
assert.Equal(t, markdown.Render("-"), "<p>-</p>")
assert.Equal(t, markdown.Render("- "), "<ul><li></li></ul>")
assert.Equal(t, markdown.Render("- Entry"), "<ul><li>Entry</li></ul>") assert.Equal(t, markdown.Render("- Entry"), "<ul><li>Entry</li></ul>")
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2"), "<ul><li>Entry 1</li><li>Entry 2</li></ul>") assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2"), "<ul><li>Entry 1</li><li>Entry 2</li></ul>")
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2\n- Entry 3"), "<ul><li>Entry 1</li><li>Entry 2</li><li>Entry 3</li></ul>") }
assert.Equal(t, markdown.Render("-"), "<p>-</p>")
func TestOrderedList(t *testing.T) {
assert.Equal(t, markdown.Render("1"), "<p>1</p>")
assert.Equal(t, markdown.Render("1."), "<ol><li></li></ol>")
assert.Equal(t, markdown.Render("1. "), "<ol><li></li></ol>")
assert.Equal(t, markdown.Render("1. Entry"), "<ol><li>Entry</li></ol>")
assert.Equal(t, markdown.Render("999) Entry"), "<ol><li>Entry</li></ol>")
assert.Equal(t, markdown.Render("1. Entry\n2. Entry"), "<ol><li>Entry</li><li>Entry</li></ol>")
} }
func TestTables(t *testing.T) { func TestTables(t *testing.T) {