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/ast_modifier.go | |
| parent | f0899429d56849f8cdf58f53a8232e4433f6725e (diff) | |
fix(markdown): not supporting triple same modifier in a row
Diffstat (limited to 'mardown/ast_modifier.go')
| -rw-r--r-- | mardown/ast_modifier.go | 61 |
1 files changed, 45 insertions, 16 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 } |
