From b1343bc63dd745b8e91500f04a2ffd13d88f1c2a Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Fri, 16 Jul 2021 23:32:47 -0400 Subject: [PATCH] prefix + suffix trimming --- main.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 92fcfea..107b755 100644 --- a/main.go +++ b/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 })