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

134 lines
2.1 KiB
Go

package wikilink
import (
"strings"
)
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 lexIdent(l *Lexer) stateFn {
for {
r := l.next()
s := l.input[l.pos:]
if r == '\\' { // i think this will handle escape characters?
break
}
switch {
case isBlockRef(s):
l.emit(ItemIdent)
return lexBlockRef
case isAlias(s):
l.emit(ItemIdent)
return lexAlias
case isCloseLink(s):
l.emit(ItemIdent)
return lexCloseLink
case isHeading(s):
l.emit(ItemIdent)
return lexHeading
}
}
panic("we shouldn't be here")
return nil // this shouldn't ever be reached
}
func lexHeading(l *Lexer) stateFn {
l.pos += len(Heading)
l.emit(ItemHeading)
return lexIdent
}
func lexBlockRef(l *Lexer) stateFn {
l.pos += len(BlockRef)
l.emit(ItemBlockRef)
return lexIdent
}
func lexAlias(l *Lexer) stateFn {
l.pos += len(Alias)
l.emit(ItemAlias)
return lexIdent
}
func lexText(l *Lexer) stateFn {
for {
if isOpenLink(l.input[l.pos:]) {
return lexOpenLink
}
r := l.next()
switch {
case r == EOF || r == '\n':
l.emit(ItemText)
return nil
}
}
}
func lexOpenLink(l *Lexer) stateFn {
l.pos += len(OpenLink)
l.emit(ItemOpenLink)
return lexIdent
}
func lexCloseLink(l *Lexer) stateFn {
l.pos += len(CloseLink)
l.emit(ItemCloseLink)
return lexText
}
/*
func lexInsideLink(l *Lexer) stateFn {
log.Println("entering lexInsideLink")
for {
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
}
r := l.next()
log.Printf("inspecting %q\n", string(r))
switch {
case r == EOF:
case r == '\n':
return l.errorf("unclosed link")
case r == '#':
l.emit(ItemText)
if l.peek() = '^' { return lexBlockRef }
return lexHeading
case r == '|':
l.emit(ItemText)
return lexAlias
}
}
}
*/