diff --git a/README.md b/README.md index 62900f1..4c65a88 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,9 @@ coverage: 100.0% of statements ## Benchmarks ``` -BenchmarkSmall-12 5933677 190.8 ns/op 32 B/op 1 allocs/op -BenchmarkMedium-12 971929 1085 ns/op 512 B/op 1 allocs/op -BenchmarkLarge-12 277028 4075 ns/op 2560 B/op 2 allocs/op +BenchmarkSmall-12 5836533 205.3 ns/op 32 B/op 1 allocs/op +BenchmarkMedium-12 967740 1103 ns/op 512 B/op 1 allocs/op +BenchmarkLarge-12 277908 4099 ns/op 2560 B/op 2 allocs/op ``` ## License diff --git a/Render.go b/Render.go index 148bf9a..eeae870 100644 --- a/Render.go +++ b/Render.go @@ -17,6 +17,7 @@ type renderer struct { paragraphLevel int quoteLevel int listLevel int + olistLevel int tableLevel int codeLines int 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("
    ") + r.olistLevel++ + } + + r.WriteString("
  1. ") + r.writeText(line) + r.WriteString("
  2. ") + return + } + } + if r.paragraphLevel == 0 { r.WriteString("

    ") r.paragraphLevel++ @@ -233,7 +254,12 @@ func (r *renderer) closeLists() { r.WriteString("") } + for range r.olistLevel { + r.WriteString("

") + } + r.listLevel = 0 + r.olistLevel = 0 } // closeTables closes open tables. diff --git a/Render_test.go b/Render_test.go index 9e96cc0..db81fd4 100644 --- a/Render_test.go +++ b/Render_test.go @@ -55,10 +55,19 @@ func TestLink(t *testing.T) { } func TestList(t *testing.T) { + assert.Equal(t, markdown.Render("-"), "

-

") + assert.Equal(t, markdown.Render("- "), "") assert.Equal(t, markdown.Render("- Entry"), "") assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2"), "") - assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2\n- Entry 3"), "") - assert.Equal(t, markdown.Render("-"), "

-

") +} + +func TestOrderedList(t *testing.T) { + assert.Equal(t, markdown.Render("1"), "

1

") + assert.Equal(t, markdown.Render("1."), "
") + assert.Equal(t, markdown.Render("1. "), "
") + assert.Equal(t, markdown.Render("1. Entry"), "
  1. Entry
") + assert.Equal(t, markdown.Render("999) Entry"), "
  1. Entry
") + assert.Equal(t, markdown.Render("1. Entry\n2. Entry"), "
  1. Entry
  2. Entry
") } func TestTables(t *testing.T) {