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/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
@ -14,7 +15,12 @@ type Link struct {
|
|||||||
Text string
|
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
|
// read file
|
||||||
bytes, err := ioutil.ReadFile(dir)
|
bytes, err := ioutil.ReadFile(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -23,11 +29,11 @@ func parse(dir string) []Link {
|
|||||||
|
|
||||||
// parse md
|
// parse md
|
||||||
var links []Link
|
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)) {
|
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{
|
links = append(links, Link{
|
||||||
Source: dir,
|
Source: trim(dir, pathPrefix, ".md"),
|
||||||
Target: target,
|
Target: target,
|
||||||
Text: text,
|
Text: text,
|
||||||
})
|
})
|
||||||
@ -35,13 +41,14 @@ func parse(dir string) []Link {
|
|||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recursively walk directory and return all files with given extension
|
||||||
func find(root, ext string) {
|
func find(root, ext string) {
|
||||||
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
if filepath.Ext(d.Name()) == ext {
|
if filepath.Ext(d.Name()) == ext {
|
||||||
parse(s)
|
parse(s, root)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user