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/main.go

55 lines
1.1 KiB
Go

package main
import (
"log"
"regexp"
)
var (
alias = regexp.MustCompile(`\|(.*)`)
link = regexp.MustCompile(`^([a-zA-Z ]+)?[\|\#\^\\]?`)
subsection = regexp.MustCompile(`#([\w\s]+)`)
block = regexp.MustCompile(`#\^([a-zA-Z ]+)`)
wholeLink = regexp.MustCompile(`\[\[(.*)\]\]`)
)
type Link struct {
Dest, Alias, Subsection, Block string
}
func ExtractLink(raw string) Link {
var li Link
rawLinkMatch := wholeLink.FindAllStringSubmatch(raw, -1)
var rawLink string
if len(rawLinkMatch) > 0 {
rawLink = rawLinkMatch[0][1]
}
l := link.FindAllStringSubmatch(rawLink, -1)
if len(l) > 0 {
li.Dest = l[0][1]
}
a := alias.FindAllStringSubmatch(rawLink, -1)
if len(a) > 0 {
li.Alias = a[0][1]
}
s := subsection.FindAllStringSubmatch(rawLink, -1)
if len(s) > 0 {
li.Subsection = s[0][1]
}
b := block.FindAllStringSubmatch(rawLink, -1)
if len(b) > 0 {
li.Block = b[0][1]
}
return li
}
func main() {
log.Printf("%#v\n", ExtractLink(`Embedding a link in a bigger block of text [[Regular Link#^link to block]] shouldn't cause any problems `))
}