Compare commits

...

16 Commits

@ -1 +1,27 @@
common --experimental_enable_bzlmod
# Disable lockfiles until it works properly.
# https://github.com/bazelbuild/bazel/issues/19068
common --lockfile_mode=off
###############################
# Directory structure #
###############################
# Artifacts are typically placed in a directory called "dist"
# Be aware that this setup will still create a bazel-out symlink in
# your project directory, which you must exclude from version control and your
# editor's search path.
build --symlink_prefix=dist/
###############################
# Output #
###############################
# A more useful default output mode for bazel query, which
# prints "ng_module rule //foo:bar" instead of just "//foo:bar".
query --output=label_kind
# By default, failing tests don't print any output, it's logged to a
# file instead.
test --test_output=errors

1
.gitignore vendored

@ -5,3 +5,4 @@ reports/*
lexer.log
parser.log
bazel-*
*.bazel.lock

@ -21,6 +21,7 @@ go_library(
go_test(
name = "obsidian-markdown_test",
size = "small",
srcs = [
"lexer_test.go",
"wikilink_test.go",

@ -6,7 +6,17 @@ module(
bazel_dep(name = "gazelle", version = "0.32.0")
bazel_dep(name = "rules_go", version = "0.41.0")
bazel_dep(name = "rules_oci", version = "1.2.0")
bazel_dep(name = "rules_pkg", version = "0.9.1")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps, "com_github_stretchr_testify", "org_uber_go_zap")
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
name = "distroless_base",
tag = "latest",
image = "gcr.io/distroless/base-debian11",
)
use_repo(oci, "distroless_base")

File diff suppressed because it is too large Load Diff

@ -16,3 +16,19 @@ go_binary(
embed = [":demo_lib"],
visibility = ["//visibility:public"],
)
load("@rules_pkg//:pkg.bzl", "pkg_tar")
pkg_tar(
name = "tar",
srcs = [":demo"],
)
load("@rules_oci//oci:defs.bzl", "oci_image")
oci_image(
name = "image",
base = "@distroless_base",
entrypoint = ["/demo"],
tars = [":tar"],
)

@ -0,0 +1,35 @@
package main
import (
"bytes"
// "fmt"
ofm "code.ndumas.com/ndumas/obsidian-markdown/goldmark"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
)
func main() {
m := goldmark.New(goldmark.WithExtensions(
ofm.ObsidianLinkExtension,
))
source := `
# Hello goldmark-extensions
this is a [[wikilink]] in text
[[wikilink]] starting a line
ending a line with a [[wikilink]]
`
var buf bytes.Buffer
context := parser.NewContext()
err := m.Convert([]byte(source), &buf, parser.WithContext(context))
if err != nil {
panic(err)
}
// fmt.Printf("%#+v\n", context)
}

@ -4,7 +4,9 @@ go 1.19
require (
github.com/stretchr/testify v1.8.0
github.com/yuin/goldmark v1.5.5
go.uber.org/zap v1.24.0
gopkg.in/yaml.v2 v2.4.0
)
require (

@ -11,6 +11,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.5.5 h1:IJznPe8wOzfIKETmMkd06F8nXkmlhaHqFRM9l1hAGsU=
github.com/yuin/goldmark v1.5.5/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
@ -20,6 +22,8 @@ go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -0,0 +1,100 @@
package goldmark
import (
"bytes"
"fmt"
// "errors"
"regexp"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var contextKeyMeta = parser.NewContextKey()
func isSeparator(line []byte) bool {
r := regexp.MustCompile(`^\s*-{3,}\s*$`)
return r.Match(line)
}
type obsidianLinkParser struct{}
var defaultLinkParser = &obsidianLinkParser{}
func (b *obsidianLinkParser) Trigger() []byte {
// return []byte{'!', '[', ']'}
fmt.Println("obsidianLinkParser.Trigger()")
return []byte{'[', ']'}
}
func (b *obsidianLinkParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
fmt.Println("obsidianLinkParser.Parse()")
return nil
}
func (b *obsidianLinkParser) Open(parent ast.Node, block text.Reader, pc parser.Context) (ast.Node, parser.State) {
fmt.Println("obsidianLinkParser.Open()")
linenum, _ := block.Position()
if linenum != 0 {
return nil, parser.NoChildren
}
line, _ := block.PeekLine()
if isSeparator(line) {
return ast.NewTextBlock(), parser.NoChildren
}
return nil, parser.NoChildren
}
func (b *obsidianLinkParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
line, segment := reader.PeekLine()
if isSeparator(line) {
reader.Advance(segment.Len())
return parser.Close
}
node.Lines().Append(segment)
return parser.Continue | parser.NoChildren
}
func (b *obsidianLinkParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
lines := node.Lines()
var buf bytes.Buffer
for i := 0; i < lines.Len(); i++ {
segment := lines.At(i)
buf.Write(segment.Value(reader.Source()))
}
pc.Set(contextKeyMeta, buf)
node.Parent().RemoveChild(node.Parent(), node)
}
func (b *obsidianLinkParser) CanInterruptParagraph() bool {
return false
}
func (b *obsidianLinkParser) CanAcceptIndentedLine() bool {
return false
}
type obsidianLinkExtension struct{}
func (ole *obsidianLinkExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
util.Prioritized(defaultLinkParser, 0),
),
)
}
var ObsidianLinkExtension = &obsidianLinkExtension{}
Loading…
Cancel
Save