You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
obsidian-markdown/states.go

131 lines
2.1 KiB
Go

package markdown
import (
"strings"
"go.uber.org/zap"
)
func isOpenLink(s string) bool {
return strings.HasPrefix(s, OpenLink)
}
func isCloseLink(s string) bool {
return strings.HasPrefix(s, CloseLink)
}
func isAlias(s string) bool {
return strings.HasPrefix(s, Alias)
}
func isHeading(s string) bool {
return strings.HasPrefix(s, Heading)
}
func isBlockRef(s string) bool {
return strings.HasPrefix(s, BlockRef)
}
func isEscape(s string) bool {
return strings.HasPrefix(s, EscapeChar)
}
func lexIdent(l *Lexer) stateFn {
L := l.L.Named("lexIdent")
for {
s := l.input[l.GetPos():]
L.Debug("stepping through lexIdent")
switch {
case isEscape(s):
l.next()
l.next()
continue
case isCloseLink(s):
L.Debug("found CloseLink")
l.emit(LexIdent)
return lexCloseLink
case isBlockRef(s):
L.Debug("found BlockRef")
l.emit(LexIdent)
return lexBlockRef
case isAlias(s):
L.Debug("found Alias")
l.emit(LexIdent)
return lexAlias
case isHeading(s):
L.Debug("found Heading")
l.emit(LexIdent)
return lexHeading
}
r := l.next()
L = l.L.With(
zap.String("rune", string(r)),
)
}
return l.errorf("malformed link")
}
func lexHeading(l *Lexer) stateFn {
l.SetPos(l.GetPos() + len(Heading))
l.emit(LexHeading)
return lexIdent
}
func lexBlockRef(l *Lexer) stateFn {
l.SetPos(l.GetPos() + len(BlockRef))
l.emit(LexBlockRef)
return lexIdent
}
func lexAlias(l *Lexer) stateFn {
l.SetPos(l.GetPos() + len(Alias))
l.emit(LexAlias)
return lexIdent
}
func lexText(l *Lexer) stateFn {
L := l.L.Named("lexText")
for {
s := l.input[l.GetPos():]
L.Debug("stepping through lexText")
switch {
case isEscape(s):
l.next()
l.next()
continue
case isOpenLink(s):
L.Debug("found openLink")
l.emit(LexText)
return lexOpenLink
}
r := l.next()
switch {
case r == EOF:
l.emit(LexText)
return nil
case r == '\n':
l.emit(LexText)
return lexText
}
}
}
func lexOpenLink(l *Lexer) stateFn {
l.SetPos(l.GetPos() + len(OpenLink))
l.emit(LexOpenLink)
return lexIdent
}
func lexCloseLink(l *Lexer) stateFn {
l.SetPos(l.GetPos() + len(CloseLink))
l.emit(LexCloseLink)
return lexText
}