trying to handle escape characters now

main
Nick Dumas 1 year ago
parent 5e12c32fc5
commit b9049f340e

@ -36,6 +36,7 @@ const (
Alias = "|" Alias = "|"
Heading = "#" Heading = "#"
BlockRef = "#^" BlockRef = "#^"
EscapeChar = `\`
) )
func Lex(name, input string, level zapcore.Level) *Lexer { func Lex(name, input string, level zapcore.Level) *Lexer {

@ -9,7 +9,19 @@ import (
"code.ndumas.com/ndumas/obsidian-markdown" "code.ndumas.com/ndumas/obsidian-markdown"
) )
var SingleWikilink = []tc{ var wikilinkWithEscapeCharacters = []tc{
{
name: "wikilink",
in: `[[wiki\]link]]`,
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: `wiki\]link`},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
}
var singleWikilink = []tc{
{ {
name: "wikilink", name: "wikilink",
in: "[[wikilink]]", in: "[[wikilink]]",
@ -197,8 +209,25 @@ var SingleWikilink = []tc{
}, },
} }
func Test_ObsidianWikilinks_LinksWithEscapeCharacters(t *testing.T) {
for _, tc := range wikilinkWithEscapeCharacters {
mut, test := mutateTestCase(
tc,
"",
"",
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
)
t.Run(mut.name, test)
}
}
func Test_ObsidianWikilinks_LinksEndOfMultiLineInput(t *testing.T) { func Test_ObsidianWikilinks_LinksEndOfMultiLineInput(t *testing.T) {
for _, tc := range SingleWikilink { for _, tc := range singleWikilink {
mut, test := mutateTestCase( mut, test := mutateTestCase(
tc, tc,
" test data please ignore.\nbling blonk more lines\nbling blong\nthis is a", " test data please ignore.\nbling blonk more lines\nbling blong\nthis is a",
@ -219,7 +248,7 @@ func Test_ObsidianWikilinks_LinksEndOfMultiLineInput(t *testing.T) {
} }
func Test_ObsidianWikilinks_LinksStartOfMultiLineInput(t *testing.T) { func Test_ObsidianWikilinks_LinksStartOfMultiLineInput(t *testing.T) {
for _, tc := range SingleWikilink { for _, tc := range singleWikilink {
mut, test := mutateTestCase( mut, test := mutateTestCase(
tc, tc,
"", "",
@ -240,7 +269,7 @@ func Test_ObsidianWikilinks_LinksStartOfMultiLineInput(t *testing.T) {
} }
func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) { func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) {
for _, tc := range SingleWikilink { for _, tc := range singleWikilink {
mut, test := mutateTestCase( mut, test := mutateTestCase(
tc, tc,
"", "",
@ -258,7 +287,7 @@ func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) {
} }
func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) { func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) {
for _, tc := range SingleWikilink { for _, tc := range singleWikilink {
mut, test := mutateTestCase( mut, test := mutateTestCase(
tc, tc,
"this is a ", "this is a ",
@ -276,7 +305,7 @@ func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) {
func Test_ObsidianWikilinks_Basic(t *testing.T) { func Test_ObsidianWikilinks_Basic(t *testing.T) {
for _, tc := range SingleWikilink { for _, tc := range singleWikilink {
mut, test := mutateTestCase( mut, test := mutateTestCase(
tc, tc,
"", "",

@ -26,15 +26,19 @@ func isBlockRef(s string) bool {
return strings.HasPrefix(s, BlockRef) return strings.HasPrefix(s, BlockRef)
} }
func isEscape(s string) bool {
return strings.HasPrefix(s, EscapeChar)
}
func lexIdent(l *Lexer) stateFn { func lexIdent(l *Lexer) stateFn {
for {
L := l.L.Named("lexIdent") L := l.L.Named("lexIdent")
for {
s := l.input[l.GetPos():] s := l.input[l.GetPos():]
L.Debug("stepping through lexIdent") L.Debug("stepping through lexIdent")
if s[0] == '\\' { // i think this will handle escape characters?
break
}
switch { switch {
case isEscape(s):
l.next()
continue
case isCloseLink(s): case isCloseLink(s):
L.Debug("found CloseLink") L.Debug("found CloseLink")
l.emit(LexIdent) l.emit(LexIdent)
@ -85,10 +89,17 @@ func lexAlias(l *Lexer) stateFn {
func lexText(l *Lexer) stateFn { func lexText(l *Lexer) stateFn {
L := l.L.Named("lexText") L := l.L.Named("lexText")
for { for {
if isOpenLink(l.input[l.GetPos():]) { s := l.input[l.GetPos():]
L.Debug("stepping through lexText")
switch {
case isEscape(s):
l.next()
continue
case isOpenLink(s):
L.Debug("found openLink") L.Debug("found openLink")
l.emit(LexText) l.emit(LexText)
return lexOpenLink return lexOpenLink
} }
r := l.next() r := l.next()
switch { switch {

Loading…
Cancel
Save