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.
101 lines
2.2 KiB
Go
101 lines
2.2 KiB
Go
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{}
|