Big renaming pass, this is about more than just wikilinks

main v0.1.0
Nick Dumas 2 years ago
parent be0d629280
commit 5e12c32fc5

@ -40,7 +40,7 @@
# Parameters
PKG = code.ndumas.com/ndumas/wikilink-parser
PKG = code.ndumas.com/ndumas/obsidian-markdown
NAME = parse-wikilinks
DOC = README.md LICENSE
@ -193,13 +193,13 @@ docker: docker-image docker-push
.PHONY: docker-push
docker-push:
$(DOCKER_CMD) tag code.ndumas.com/ndumas/wikilink-parser:$(VERSION) code.ndumas.com/ndumas/wikilink-parser:latest
$(DOCKER_CMD) push code.ndumas.com/ndumas/wikilink-parser:latest
$(DOCKER_CMD) push code.ndumas.com/ndumas/wikilink-parser:$(VERSION)
$(DOCKER_CMD) tag $(PKG):$(VERSION) $(PKG):latest
$(DOCKER_CMD) push $(PKG):latest
$(DOCKER_CMD) push $(PKG):$(VERSION)
.PHONY: docker-image
docker-image:
$(DOCKER_CMD) build --build-arg VERSION=$(VERSION) -t code.ndumas.com/ndumas/wikilink-parser:$(VERSION) .
$(DOCKER_CMD) build --build-arg VERSION=$(VERSION) -t $(PKG):$(VERSION) .
.PHONY: build-alpine
build-alpine:

@ -5,11 +5,11 @@ import (
"go.uber.org/zap/zapcore"
"code.ndumas.com/ndumas/wikilink-parser"
"code.ndumas.com/ndumas/obsidian-markdown"
)
func main() {
l := wikilink.Lex("debugLexer", `this is a [[wikilink]]`, zapcore.InfoLevel)
l := markdown.Lex("debugLexer", `this is a [[wikilink]]`, zapcore.InfoLevel)
for _, item := range l.Items {
item := item
log.Printf("%#+v\n", item)

@ -1,4 +1,4 @@
module code.ndumas.com/ndumas/wikilink-parser
module code.ndumas.com/ndumas/obsidian-markdown
go 1.19

@ -1,6 +1,6 @@
// Code generated by "stringer -type=LexemeType"; DO NOT EDIT.
package wikilink
package markdown
import "strconv"

@ -1,5 +1,5 @@
//go:generate stringer -type=LexemeType
package wikilink
package markdown
import (
"fmt"

@ -1,4 +1,4 @@
package wikilink_test
package markdown_test
import (
"testing"
@ -6,193 +6,193 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"code.ndumas.com/ndumas/wikilink-parser"
"code.ndumas.com/ndumas/obsidian-markdown"
)
var SingleWikilink = []tc{
{
name: "wikilink",
in: "[[wikilink]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink|display name",
in: "[[wikilink|display name]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink|display name|second pipe",
in: "[[wikilink|display name|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink with numeric alias|420|second pipe",
in: "[[wikilink|420|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "420"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "420"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink with spaces in filename",
in: "[[wikilink spaces]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink spaces"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink spaces"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "#heading",
in: "[[#heading]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: ""},
{Typ: wikilink.LexHeading, Val: "#"},
{Typ: wikilink.LexIdent, Val: "heading"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: ""},
{Typ: markdown.LexHeading, Val: "#"},
{Typ: markdown.LexIdent, Val: "heading"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#heading",
in: "[[wikilink#heading]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexHeading, Val: "#"},
{Typ: wikilink.LexIdent, Val: "heading"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexHeading, Val: "#"},
{Typ: markdown.LexIdent, Val: "heading"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#heading|display name",
in: "[[wikilink#heading|display name]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexHeading, Val: "#"},
{Typ: wikilink.LexIdent, Val: "heading"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexHeading, Val: "#"},
{Typ: markdown.LexIdent, Val: "heading"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#heading|display name|second pipe",
in: "[[wikilink#heading|display name|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexHeading, Val: "#"},
{Typ: wikilink.LexIdent, Val: "heading"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexHeading, Val: "#"},
{Typ: markdown.LexIdent, Val: "heading"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink with numeric aliases#heading|420|display name",
in: "[[wikilink#heading|420|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexHeading, Val: "#"},
{Typ: wikilink.LexIdent, Val: "heading"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "420"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexHeading, Val: "#"},
{Typ: markdown.LexIdent, Val: "heading"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "420"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "#^blockRef",
in: "[[#^blockRef]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: ""},
{Typ: wikilink.LexBlockRef, Val: "#^"},
{Typ: wikilink.LexIdent, Val: "blockRef"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: ""},
{Typ: markdown.LexBlockRef, Val: "#^"},
{Typ: markdown.LexIdent, Val: "blockRef"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#^blockRef",
in: "[[wikilink#^blockRef]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexBlockRef, Val: "#^"},
{Typ: wikilink.LexIdent, Val: "blockRef"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexBlockRef, Val: "#^"},
{Typ: markdown.LexIdent, Val: "blockRef"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#^blockRef|display name",
in: "[[wikilink#^blockRef|display name]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexBlockRef, Val: "#^"},
{Typ: wikilink.LexIdent, Val: "blockRef"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexBlockRef, Val: "#^"},
{Typ: markdown.LexIdent, Val: "blockRef"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink#^blockRef|display name|second pipe",
in: "[[wikilink#^blockRef|display name|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexBlockRef, Val: "#^"},
{Typ: wikilink.LexIdent, Val: "blockRef"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "display name"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexBlockRef, Val: "#^"},
{Typ: markdown.LexIdent, Val: "blockRef"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "display name"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
{
name: "wikilink with numeric aliases#^blockRef|420|second pipe",
in: "[[wikilink#^blockRef|420|second pipe]]",
expected: []wikilink.Lexeme{
{Typ: wikilink.LexOpenLink, Val: "[["},
{Typ: wikilink.LexIdent, Val: "wikilink"},
{Typ: wikilink.LexBlockRef, Val: "#^"},
{Typ: wikilink.LexIdent, Val: "blockRef"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "420"},
{Typ: wikilink.LexAlias, Val: "|"},
{Typ: wikilink.LexIdent, Val: "second pipe"},
{Typ: wikilink.LexCloseLink, Val: "]]"},
expected: []markdown.Lexeme{
{Typ: markdown.LexOpenLink, Val: "[["},
{Typ: markdown.LexIdent, Val: "wikilink"},
{Typ: markdown.LexBlockRef, Val: "#^"},
{Typ: markdown.LexIdent, Val: "blockRef"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "420"},
{Typ: markdown.LexAlias, Val: "|"},
{Typ: markdown.LexIdent, Val: "second pipe"},
{Typ: markdown.LexCloseLink, Val: "]]"},
},
},
}
@ -204,14 +204,14 @@ func Test_ObsidianWikilinks_LinksEndOfMultiLineInput(t *testing.T) {
" test data please ignore.\nbling blonk more lines\nbling blong\nthis is a",
"",
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: " test data please ignore.\n"},
{Typ: wikilink.LexText, Val: "bling blonk more lines\n"},
{Typ: wikilink.LexText, Val: "bling blong\n"},
{Typ: wikilink.LexText, Val: "this is a"},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: " test data please ignore.\n"},
{Typ: markdown.LexText, Val: "bling blonk more lines\n"},
{Typ: markdown.LexText, Val: "bling blong\n"},
{Typ: markdown.LexText, Val: "this is a"},
},
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
)
t.Run(mut.name, test)
@ -225,14 +225,14 @@ func Test_ObsidianWikilinks_LinksStartOfMultiLineInput(t *testing.T) {
"",
" test data please ignore.\nbling blonk more lines\nbling blong\nthis is a",
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: " test data please ignore.\n"},
{Typ: wikilink.LexText, Val: "bling blonk more lines\n"},
{Typ: wikilink.LexText, Val: "bling blong\n"},
{Typ: wikilink.LexText, Val: "this is a"},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: " test data please ignore.\n"},
{Typ: markdown.LexText, Val: "bling blonk more lines\n"},
{Typ: markdown.LexText, Val: "bling blong\n"},
{Typ: markdown.LexText, Val: "this is a"},
},
)
t.Run(mut.name, test)
@ -246,11 +246,11 @@ func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) {
"",
" test data please ignore",
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: " test data please ignore"},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: " test data please ignore"},
},
)
t.Run(mut.name, test)
@ -263,11 +263,11 @@ func Test_ObsidianWikilinks_LinksEndOfInput(t *testing.T) {
tc,
"this is a ",
"",
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: "this is a "},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: "this is a "},
},
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
)
t.Run(mut.name, test)
@ -281,11 +281,11 @@ func Test_ObsidianWikilinks_Basic(t *testing.T) {
tc,
"",
"",
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
[]wikilink.Lexeme{
{Typ: wikilink.LexText, Val: ""},
[]markdown.Lexeme{
{Typ: markdown.LexText, Val: ""},
},
)
t.Run(mut.name, test)
@ -295,10 +295,10 @@ func Test_ObsidianWikilinks_Basic(t *testing.T) {
type tc struct {
name string
in string
expected []wikilink.Lexeme
expected []markdown.Lexeme
}
func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix []wikilink.Lexeme) (tc, func(t *testing.T)) {
func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix []markdown.Lexeme) (tc, func(t *testing.T)) {
tc.in = prefix + tc.in
tc.in = tc.in + suffix
if expectedPrefix != nil {
@ -309,7 +309,7 @@ func mutateTestCase(tc tc, prefix, suffix string, expectedPrefix, expectedSuffix
}
return tc, func(t *testing.T) {
l := wikilink.Lex("testLexer", tc.in, zapcore.WarnLevel)
l := markdown.Lex("testLexer", tc.in, zapcore.WarnLevel)
defer l.L.Sync()
assert.Equal(t, tc.expected, l.Items, "token stream mismatch")
}

@ -1,4 +1,4 @@
package wikilink
package markdown
import (
"strings"

@ -1,4 +1,4 @@
package wikilink
package markdown
import (
// "log"

@ -1,9 +1,9 @@
package wikilink_test
package markdown_test
import (
"testing"
"code.ndumas.com/ndumas/wikilink-parser"
"code.ndumas.com/ndumas/obsidian-markdown"
)
func _Test_Wikilink_Parsing(t *testing.T) {
@ -36,7 +36,7 @@ func _Test_Wikilink_Parsing(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out := wikilink.Extract(tc.in)
out := markdown.Extract(tc.in)
if out.Link != tc.link {
t.Logf("got %#v\n", out)
t.Fail()
@ -50,7 +50,7 @@ func _Test_Wikilink_Parsing(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out := wikilink.Extract(tc.in)
out := markdown.Extract(tc.in)
if out.Alias != tc.alias {
t.Logf("got %#v\n", out)
t.Fail()
@ -64,7 +64,7 @@ func _Test_Wikilink_Parsing(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out := wikilink.Extract(tc.in)
out := markdown.Extract(tc.in)
if out.Fragment != tc.fragment {
t.Logf("got %#v\n", out)
t.Fail()

Loading…
Cancel
Save