handle lists properly

This commit is contained in:
dre 2021-07-06 01:01:29 +08:00
parent ee363e0c91
commit 0944f4dbd5
2 changed files with 36 additions and 4 deletions

View file

@ -20,6 +20,11 @@ comment -->
> this is
a quote
- this
- is
* an unordered
* list
This is
a paragraph with a link to the [gemini protocol](https://en.wikipedia.org/wiki/Gemini_(protocol)).
@ -38,6 +43,12 @@ and
> this is a quote
- this
- is
- an unordered
- list
This is a paragraph with a link to the [gemini protocol](https://en.wikipedia.org/wiki/Gemini_(protocol)).
` + "```" + `
@ -57,6 +68,12 @@ this is code too
> this is a quote
- this
- is
- an unordered
- list
This is a paragraph with a link to the gemini protocol[1].
=> https://en.wikipedia.org/wiki/Gemini_(protocol) 1: gemini protocol

View file

@ -81,7 +81,7 @@ func isTerminated(data []byte) bool {
}
func handleList(data []byte) ([]byte, bool) {
re := regexp.MustCompile(`^([ ]*[-*^]{1,1})[^*-]`)
re := regexp.MustCompile(`^([ \t]*[-*^]{1,1})[^*-]`)
sub := re.FindSubmatch(data)
// if lists, collapse to single level
if len(sub) > 1 {
@ -100,14 +100,15 @@ func needsFence(data []byte) bool {
}
func normal(m *fsm, data []byte) stateFn {
if len(data) == 0 {
if len(bytes.TrimSpace(data)) == 0 {
return normal
}
if data, isList := handleList(data); isList {
m.blockBuffer = append(data, '\n')
//m.blockBuffer = append(data, '\n')
m.blockBuffer = append(m.blockBuffer, data...)
m.blockFlush()
return normal
return list
}
if isFence(data) {
@ -137,6 +138,20 @@ func normal(m *fsm, data []byte) stateFn {
return normal
}
func list(m *fsm, data []byte) stateFn {
if data, isList := handleList(data); isList {
m.blockBuffer = append(m.blockBuffer, data...)
m.blockFlush()
return list
}
m.blockBuffer = append(m.blockBuffer, append(data, '\n')...)
m.blockFlush()
return normal
}
func fence(m *fsm, data []byte) stateFn {
m.blockBuffer = append(m.blockBuffer, append(data, '\n')...)
// second fence returns to normal