Skip to content
/ hugo Public
forked from gohugoio/hugo

Commit

Permalink
tpl/urls: Return strings from URL functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Sep 29, 2023
1 parent 3af8bde commit fdf1b6f
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions tpl/urls/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package urls
import (
"errors"
"fmt"
"html/template"
"net/url"

"github.com/gohugoio/hugo/common/urls"
Expand All @@ -40,35 +39,35 @@ type Namespace struct {
}

// AbsURL takes the string s and converts it to an absolute URL.
func (ns *Namespace) AbsURL(s any) (template.HTML, error) {
func (ns *Namespace) AbsURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", nil
}

return template.HTML(ns.deps.PathSpec.AbsURL(ss, false)), nil
return ns.deps.PathSpec.AbsURL(ss, false), nil
}

// Parse parses rawurl into a URL structure. The rawurl may be relative or
// absolute.
func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
s, err := cast.ToStringE(rawurl)
if err != nil {
return nil, fmt.Errorf("Error in Parse: %w", err)
return nil, fmt.Errorf("error in Parse: %w", err)
}

return url.Parse(s)
}

// RelURL takes the string s and prepends the relative path according to a
// page's position in the project directory structure.
func (ns *Namespace) RelURL(s any) (template.HTML, error) {
func (ns *Namespace) RelURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", nil
}

return template.HTML(ns.deps.PathSpec.RelURL(ss, false)), nil
return ns.deps.PathSpec.RelURL(ss, false), nil
}

// URLize returns the the strings s formatted as an URL.
Expand All @@ -91,7 +90,7 @@ func (ns *Namespace) Anchorize(s any) (string, error) {
}

// Ref returns the absolute URL path to a given content item from Page p.
func (ns *Namespace) Ref(p any, args any) (template.HTML, error) {
func (ns *Namespace) Ref(p any, args any) (string, error) {
pp, ok := p.(urls.RefLinker)
if !ok {
return "", errors.New("invalid Page received in Ref")
Expand All @@ -101,11 +100,11 @@ func (ns *Namespace) Ref(p any, args any) (template.HTML, error) {
return "", err
}
s, err := pp.Ref(argsm)
return template.HTML(s), err
return s, err
}

// RelRef returns the relative URL path to a given content item from Page p.
func (ns *Namespace) RelRef(p any, args any) (template.HTML, error) {
func (ns *Namespace) RelRef(p any, args any) (string, error) {
pp, ok := p.(urls.RefLinker)
if !ok {
return "", errors.New("invalid Page received in RelRef")
Expand All @@ -116,7 +115,7 @@ func (ns *Namespace) RelRef(p any, args any) (template.HTML, error) {
}

s, err := pp.RelRef(argsm)
return template.HTML(s), err
return s, err
}

func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) {
Expand All @@ -143,7 +142,7 @@ func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) {
if len(v) == 0 || len(v) > 2 {
return nil, fmt.Errorf("invalid number of arguments to ref")
}
// These where the options before we introduced the map type:
// These were the options before we introduced the map type:
s = v[0]
if len(v) == 2 {
of = v[1]
Expand All @@ -165,25 +164,25 @@ func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) {

// RelLangURL takes the string s and prepends the relative path according to a
// page's position in the project directory structure and the current language.
func (ns *Namespace) RelLangURL(s any) (template.HTML, error) {
func (ns *Namespace) RelLangURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}

return template.HTML(ns.deps.PathSpec.RelURL(ss, !ns.multihost)), nil
return ns.deps.PathSpec.RelURL(ss, !ns.multihost), nil
}

// AbsLangURL the string s and converts it to an absolute URL according
// to a page's position in the project directory structure and the current
// language.
func (ns *Namespace) AbsLangURL(s any) (template.HTML, error) {
func (ns *Namespace) AbsLangURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}

return template.HTML(ns.deps.PathSpec.AbsURL(ss, !ns.multihost)), nil
return ns.deps.PathSpec.AbsURL(ss, !ns.multihost), nil
}

// JoinPath joins the provided elements into a URL string and cleans the result
Expand Down

0 comments on commit fdf1b6f

Please sign in to comment.