mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
prefix + suffix trimming
This commit is contained in:
parent
68f688bbfe
commit
b1343bc63d
17
main.go
17
main.go
@ -6,6 +6,7 @@ import (
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
@ -14,7 +15,12 @@ type Link struct {
|
||||
Text string
|
||||
}
|
||||
|
||||
func parse(dir string) []Link {
|
||||
func trim(source, prefix, suffix string) string {
|
||||
return strings.TrimPrefix(strings.TrimSuffix(source, suffix), prefix)
|
||||
}
|
||||
|
||||
// parse single file for links
|
||||
func parse(dir, pathPrefix string) []Link {
|
||||
// read file
|
||||
bytes, err := ioutil.ReadFile(dir)
|
||||
if err != nil {
|
||||
@ -23,11 +29,11 @@ func parse(dir string) []Link {
|
||||
|
||||
// parse md
|
||||
var links []Link
|
||||
fmt.Printf("in %s \n", dir)
|
||||
fmt.Printf("%s \n", trim(dir, pathPrefix, ".md"))
|
||||
for text, target := range md.GetAllLinks(string(bytes)) {
|
||||
fmt.Printf("found link: %s -> %s \n", text, target)
|
||||
fmt.Printf(" %s -> %s \n", text, target)
|
||||
links = append(links, Link{
|
||||
Source: dir,
|
||||
Source: trim(dir, pathPrefix, ".md"),
|
||||
Target: target,
|
||||
Text: text,
|
||||
})
|
||||
@ -35,13 +41,14 @@ func parse(dir string) []Link {
|
||||
return links
|
||||
}
|
||||
|
||||
// recursively walk directory and return all files with given extension
|
||||
func find(root, ext string) {
|
||||
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if filepath.Ext(d.Name()) == ext {
|
||||
parse(s)
|
||||
parse(s, root)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user