diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 10:58:28 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 10:58:28 +0200 |
| commit | 597e15a5cde4127105ce7a6109d52491d6976be7 (patch) | |
| tree | ba10438a8604ba4ed8a0c59a32844fbebee0c96c /mardown | |
| parent | f0899429d56849f8cdf58f53a8232e4433f6725e (diff) | |
fix(markdown): not supporting triple same modifier in a row
Diffstat (limited to 'mardown')
| -rw-r--r-- | mardown/ast_modifier.go | 61 | ||||
| -rw-r--r-- | mardown/ast_modifier_test.go | 4 | ||||
| -rw-r--r-- | mardown/lexer.go | 2 | ||||
| -rw-r--r-- | mardown/lexer_test.go | 2 |
4 files changed, 49 insertions, 20 deletions
diff --git a/mardown/ast_modifier.go b/mardown/ast_modifier.go index 0107605..13cd91d 100644 --- a/mardown/ast_modifier.go +++ b/mardown/ast_modifier.go @@ -24,6 +24,7 @@ type astModifier struct { symbols string tag modifierTag content []block + super bool } func (a *astModifier) Eval() (template.HTML, error) { @@ -35,6 +36,9 @@ func (a *astModifier) Eval() (template.HTML, error) { } content += ct } + if a.super { + return content, nil + } return template.HTML(fmt.Sprintf("<%s>%s</%s>", a.tag, content, a.tag)), nil } @@ -51,7 +55,7 @@ func (a *astModifier) String() string { content += ",\n\t" } content += "]" - return fmt.Sprintf("modifier{sym: %s, tag: %s, content: %s\n}", a.symbols, a.tag, content) + return fmt.Sprintf("modifier{sym: %s, tag: %s, super: %v, content: %s\n}", a.symbols, a.tag, a.super, content) } func modifier(lxs *lexers) (*astModifier, error) { @@ -66,19 +70,44 @@ func modifier(lxs *lexers) (*astModifier, error) { case lexerLiteral, lexerHeader, lexerList: s += lxs.Current().Value case lexerModifier: - if lxs.Current().Value == mod.symbols { - mod.content = append(mod.content, astLiteral(s)) - return mod, nil - } - if len(s) != 0 { - mod.content = append(mod.content, astLiteral(s)) + if mod.super && []rune(mod.symbols)[0] == []rune(lxs.Current().Value)[0] && + len(mod.symbols) >= len(lxs.Current().Value) { + mod.symbols = mod.symbols[len(lxs.Current().Value):] + subMod, err := modifierDetect(lxs.Current().Value) + if err != nil { + return nil, err + } + if !subMod.super { + subMod.content = append(subMod.content, astLiteral(s)) + mod, err = modifierDetect(mod.symbols) // this trick is so cool :D + if err != nil { + return nil, err + } + } else { + subMod, _ = modifierDetect("**") + subEm, _ := modifierDetect("*") + subEm.content = append(subEm.content, astLiteral(s)) + subMod.content = append(subMod.content, subEm) + } s = "" + mod.content = append(mod.content, subMod) + if len(mod.symbols) == 0 { + return mod, nil + } + } else { + if lxs.Current().Value == mod.symbols { + mod.content = append(mod.content, astLiteral(s)) + return mod, nil + } else if len(s) != 0 { + mod.content = append(mod.content, astLiteral(s)) + s = "" + } + c, err := modifier(lxs) + if err != nil { + return nil, err + } + mod.content = append(mod.content, c) } - c, err := modifier(lxs) - if err != nil { - return nil, err - } - mod.content = append(mod.content, c) case lexerBreak: lxs.Before() // because we did not use it if len(s) != 0 { @@ -101,16 +130,16 @@ func modifier(lxs *lexers) (*astModifier, error) { func modifierDetect(val string) (*astModifier, error) { mod := new(astModifier) + mod.symbols = val switch len(val) { case 1: - mod.symbols = val mod.tag = emTag - return mod, nil case 2: - mod.symbols = val mod.tag = boldTag - return mod, nil + case 3: + mod.super = true default: return nil, ErrInvalidModifier } + return mod, nil } diff --git a/mardown/ast_modifier_test.go b/mardown/ast_modifier_test.go index 1539aa4..ce0988f 100644 --- a/mardown/ast_modifier_test.go +++ b/mardown/ast_modifier_test.go @@ -4,7 +4,7 @@ import "testing" func TestModifier(t *testing.T) { content := ` -**bo*n*soir** +**bo*n*soir**, ça ***va* bien** ? ` lxs := lex(content) tree, err := ast(lxs) @@ -15,7 +15,7 @@ func TestModifier(t *testing.T) { if err != nil { t.Fatal(err) } - if c != "<p><b>bo<em>n</em>soir</b></p>" { + if c != "<p><b>bo<em>n</em>soir</b>, ça <b><em>va</em> bien</b> ?</p>" { t.Errorf("failed, got %s", c) t.Logf("lxs: %s\ntree: %s", lxs, tree) } diff --git a/mardown/lexer.go b/mardown/lexer.go index 64fa78b..52e5788 100644 --- a/mardown/lexer.go +++ b/mardown/lexer.go @@ -88,7 +88,7 @@ func lex(s string) *lexers { case '*', '_': if (currentType != lexerModifier && len(previous) > 0) || (len(previous) > 0 && []rune(previous)[0] != c) || - len(previous) > 2 { + len(previous) >= 3 { lexs = append(lexs, lexer{Type: currentType, Value: previous}) previous = "" } diff --git a/mardown/lexer_test.go b/mardown/lexer_test.go index c9ec349..8fe81cc 100644 --- a/mardown/lexer_test.go +++ b/mardown/lexer_test.go @@ -16,7 +16,7 @@ func TestLex(t *testing.T) { t.Errorf("invalid lex, got %s", lxs) } lxs = lex("***hey***, what's up?") - if lxs.String() != "Lexers[modifier(**) modifier(*) literal(hey) modifier(*) modifier(**) literal(, what's up?) ]" { + if lxs.String() != "Lexers[modifier(***) literal(hey) modifier(***) literal(, what's up?) ]" { t.Errorf("invalid lex, got %s", lxs) } lxs = lex(`Xxx\_DarkEmperor\_xxX`) |
