feat: parse markdown to base syntax guide
parent
2c9a60c42f
commit
b09a0eae9a
|
@ -23,7 +23,7 @@ type header struct {
|
||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *header) ToString() string {
|
func (h header) ToString() string {
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
|
|
||||||
for i := 0; i < h.level; i++ {
|
for i := 0; i < h.level; i++ {
|
||||||
|
@ -42,7 +42,7 @@ type listItem struct {
|
||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *listItem) ToString() string {
|
func (i listItem) ToString() string {
|
||||||
text := strings.TrimSpace(i.text)
|
text := strings.TrimSpace(i.text)
|
||||||
|
|
||||||
return string(defaultListToken) + string(spaceToken) + text
|
return string(defaultListToken) + string(spaceToken) + text
|
||||||
|
|
|
@ -15,6 +15,14 @@ func Parse(lines []string) []Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(line, string(headerToken)) {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,47 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestParseHeader(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in New Issue