aboutsummaryrefslogtreecommitdiff
path: root/markdown
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-02-05 09:09:19 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2026-02-05 16:36:41 +0000
commitcd3bd5dff64fa80c7170d7f5d2d05f610fec36ee (patch)
treed9976e9bd4419cabb30797f08903d9a9a910ca0c /markdown
parentd2273b16237e1c3d731a173f01632cf4f51848dd (diff)
feat(markdown): lex callout
Diffstat (limited to 'markdown')
-rw-r--r--markdown/lexer.go19
-rw-r--r--markdown/lexer_test.go5
2 files changed, 23 insertions, 1 deletions
diff --git a/markdown/lexer.go b/markdown/lexer.go
index afdc4a0..f79b2ec 100644
--- a/markdown/lexer.go
+++ b/markdown/lexer.go
@@ -19,6 +19,7 @@ const (
lexerList lexerType = "list"
lexerExternal lexerType = "external"
+ lexerCallout lexerType = "callout"
lexerLiteral lexerType = "literal"
lexerReplace lexerType = "replace"
@@ -134,7 +135,23 @@ func lex(s string, opt *Option) *lexers {
fn(c, lexerHeading, nil)
case '>':
fn(c, lexerQuote, nil)
- case '[', ']', '(', ')', '!':
+ case '[', ']', '!':
+ if !newLine && i < len(runes)-1 {
+ next := runes[i+1]
+ runes := []rune(previous)
+ if c == '[' && next == '!' {
+ fn(c, lexerCallout, nil)
+ continue
+ } else if c == '!' && len(runes) > 0 && previous[len(previous)-1] == '[' {
+ fn(c, lexerCallout, nil)
+ continue
+ } else if c == ']' && next != '(' {
+ fn(c, lexerCallout, nil)
+ continue
+ }
+ }
+ fallthrough
+ case '(', ')':
fn(c, lexerExternal, func(c rune) bool { return validExternal(previous + string(c)) })
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
fn(c, lexerList, nil)
diff --git a/markdown/lexer_test.go b/markdown/lexer_test.go
index d8ec3ba..5736b2a 100644
--- a/markdown/lexer_test.go
+++ b/markdown/lexer_test.go
@@ -28,6 +28,11 @@ func TestLex(t *testing.T) {
if lxs.String() != `Lexers[list(*) literal( list) ]` {
t.Errorf("invalid lex, got %s", lxs)
}
+ lxs = lex(`> [!NOTE] title
+> hey`, opt)
+ if lxs.String() != `Lexers[quote(>) literal( ) callout([!) literal(NOTE) callout(]) literal( title) break({\n}) quote(>) literal( hey) ]` {
+ t.Errorf("invalid lex, got %s", lxs)
+ }
}
func TestLex_Replacer(t *testing.T) {