From bdd9e63fdfef14f981e07a5329041d98258bb29b Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sat, 5 Apr 2025 17:57:15 -0400 Subject: [PATCH] working on path normalizer --- internal/builder/build_page.go | 7 ++++++- internal/util/path.go | 25 +++++++++++++++++++++++++ test/posts/in.md | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/builder/build_page.go b/internal/builder/build_page.go index e0efc28..3a45dfe 100644 --- a/internal/builder/build_page.go +++ b/internal/builder/build_page.go @@ -59,7 +59,12 @@ func buildPageData(m Metadata, in string, out string, settings *Settings) *PageD p.Title = util.PathToTitle(in) } if icon, ok := m["icon"].(string); ok { - p.Icon = icon + i, err := util.NormalizePath(icon) + if err != nil { + p.Icon = settings.IconName + } else { + p.Icon = i + } } else { p.Icon = settings.IconName } diff --git a/internal/util/path.go b/internal/util/path.go index 1d9a740..da85642 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -3,6 +3,7 @@ package util import ( "errors" + "fmt" "os" "path/filepath" "strings" @@ -108,3 +109,27 @@ func StripTopDir(path string) string { } return filepath.Join(components[1:]...) } + +// we want to preserve a valid web-style path +// and convert relative path to web-style +// so we need to see +// TODO; use Rel function to get abs path between +// the file being analyzed's path, and what lil bro +// is pointing to +func NormalizePath(path string) (string, error) { + // empty path is root + if path == "" { + return "/", nil + } + if path[0] == '.' { + fmt.Println("Local path detected...") + abs, err := filepath.Abs(path) + fmt.Printf("abs: %s\n", abs) + if err != nil { + return "", fmt.Errorf("Couldn't normalize path: %w", err) + } + return ReplaceRoot(abs, "/"), nil + } else { + return path, nil + } +} diff --git a/test/posts/in.md b/test/posts/in.md index 57171ba..fc679ea 100644 --- a/test/posts/in.md +++ b/test/posts/in.md @@ -1,5 +1,6 @@ --- title: tiltetest +icon: ../assets/pic.png --- # My amazing markdown file!