✨ Created urls template funcs namespace
This commit is contained in:
parent
d677254e32
commit
70853bc665
|
@ -0,0 +1,42 @@
|
||||||
|
package urls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
|
||||||
|
"source.toby3d.me/toby3d/home/internal/site"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Namespace struct {
|
||||||
|
siter site.UseCase
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrAbsURL error = errors.New("unsupported input type for AbsURL")
|
||||||
|
|
||||||
|
func New(siter site.UseCase) *Namespace {
|
||||||
|
return &Namespace{
|
||||||
|
siter: siter,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ns *Namespace) AbsURL(p string) (string, error) {
|
||||||
|
site, err := ns.siter.Do(context.Background(), language.Und)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("cannot fetch site root for AbsURL processing: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return site.BaseURL.JoinPath(p).String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ns *Namespace) RelURL(p string) string {
|
||||||
|
return path.Clean("/" + p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ns *Namespace) Parse(rawUrl string) (*url.URL, error) {
|
||||||
|
return url.Parse(rawUrl)
|
||||||
|
}
|
Loading…
Reference in New Issue