diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-01 09:43:06 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-01 09:43:06 +0200 |
| commit | c49daa8fe6fdcf1c7fab08ee32f1ff994979c401 (patch) | |
| tree | 6ac743ddd35563893f92f2fcba73907657d36191 /mardown | |
| parent | b07dd267fc10812f7e94bf6d9be08667537dd331 (diff) | |
fix(markdown): paragraph parse always skip one char
Diffstat (limited to 'mardown')
| -rw-r--r-- | mardown/ast_header.go | 3 | ||||
| -rw-r--r-- | mardown/ast_paragraph.go | 11 | ||||
| -rw-r--r-- | mardown/ast_quote.go | 5 | ||||
| -rw-r--r-- | mardown/ast_test.go | 4 | ||||
| -rw-r--r-- | mardown/lexer.go | 5 |
5 files changed, 23 insertions, 5 deletions
diff --git a/mardown/ast_header.go b/mardown/ast_header.go index 28a0a25..64e47ad 100644 --- a/mardown/ast_header.go +++ b/mardown/ast_header.go @@ -27,6 +27,9 @@ func (a *astHeader) Eval() (template.HTML, error) { func header(lxs *lexers) (*astHeader, error) { b := &astHeader{level: uint(len(lxs.Current().Value))} + if !lxs.Next() { + return nil, ErrInvalidHeader + } var err error b.content, err = paragraph(lxs, true) if err != nil { diff --git a/mardown/ast_paragraph.go b/mardown/ast_paragraph.go index e9855a7..d728183 100644 --- a/mardown/ast_paragraph.go +++ b/mardown/ast_paragraph.go @@ -43,7 +43,13 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, error) { switch lxs.Current().Type { case lexerBreak: n++ - case lexerLiteral, lexerHeader, lexerQuote: + case lexerQuote: + if n > 0 { + lxs.Before() + return tree, nil + } + tree.content = append(tree.content, astLiteral(lxs.Current().Value)) + case lexerLiteral, lexerHeader: n = 0 tree.content = append(tree.content, astLiteral(lxs.Current().Value)) case lexerModifier: @@ -68,6 +74,9 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, error) { return tree, nil } } + if !lxs.Finished() { + lxs.Before() // because we never handle the last item + } return tree, nil } diff --git a/mardown/ast_quote.go b/mardown/ast_quote.go index 9647500..11c7c13 100644 --- a/mardown/ast_quote.go +++ b/mardown/ast_quote.go @@ -48,7 +48,7 @@ func quote(lxs *lexers) (*astQuote, error) { n = 0 if source { // because the code did not use it - lxs.current-- + lxs.Before() return tree, nil } quoteContinue = true @@ -58,7 +58,6 @@ func quote(lxs *lexers) (*astQuote, error) { source = true } p, err := paragraph(lxs, true) - lxs.current-- // because we call the next twice here if err != nil { return nil, err } @@ -72,7 +71,7 @@ func quote(lxs *lexers) (*astQuote, error) { quoteContinue = false default: // because the code did not use it - lxs.current-- + lxs.Before() return tree, nil } } diff --git a/mardown/ast_test.go b/mardown/ast_test.go index 6215ced..030656b 100644 --- a/mardown/ast_test.go +++ b/mardown/ast_test.go @@ -35,7 +35,9 @@ func TestAst(t *testing.T) { if err != nil { t.Fatal(err) } - if string(res) != strings.ReplaceAll(parsed, "\n", "") { + wanted := strings.ReplaceAll(parsed, "\n", "") + if string(res) != wanted { t.Errorf("invalid string, got %s", res) + t.Logf("wanted %s", wanted) } } diff --git a/mardown/lexer.go b/mardown/lexer.go index 4eaaf16..f87e61c 100644 --- a/mardown/lexer.go +++ b/mardown/lexer.go @@ -45,6 +45,11 @@ func (l *lexers) Finished() bool { return l.current >= len(l.lexers) } +func (l *lexers) Before() bool { + l.current-- + return l.current >= 0 && !l.Finished() +} + func (l *lexers) String() string { s := "Lexers[" for _, l := range l.lexers { |
