feat: parse markdown to base syntax guide

main
hau 2020-11-27 17:45:25 +07:00 committed by Nguyen Tran Hau
parent 2c9a60c42f
commit b09a0eae9a
3 changed files with 51 additions and 2 deletions

View File

@ -23,7 +23,7 @@ type header struct {
text string
}
func (h *header) ToString() string {
func (h header) ToString() string {
var builder strings.Builder
for i := 0; i < h.level; i++ {
@ -42,7 +42,7 @@ type listItem struct {
text string
}
func (i *listItem) ToString() string {
func (i listItem) ToString() string {
text := strings.TrimSpace(i.text)
return string(defaultListToken) + string(spaceToken) + text

View File

@ -15,6 +15,14 @@ func Parse(lines []string) []Base {
}
if strings.HasPrefix(line, string(headerToken)) {
bases = append(bases, parseHeader(line))
continue
}
if strings.HasPrefix(line, string(defaultListToken)) ||
strings.HasPrefix(line, string(alternativeListToken)) {
bases = append(bases, parseListItem(line))
continue
}
}

View File

@ -6,6 +6,47 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
tests := []struct {
name string
lines []string
want []Base
}{
{
name: "level 1",
lines: []string{
"# abc",
"- xyz",
},
want: []Base{
header{
level: 1,
text: "abc",
},
listItem{
text: "xyz",
},
},
},
{
name: "level 3 with alternative",
lines: []string{
"### xyz",
"* abc",
},
want: []Base{
header{
level: 1,
text: "xyz",
},
listItem{
text: "abc",
},
},
},
}
}
func TestParseHeader(t *testing.T) {
tests := []struct {
name string