From a3d7c4a3a93df535685570baec26dba4632bd765 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Mon, 20 Mar 2023 05:27:43 -0700 Subject: [PATCH] Improve urls.Parse function (#2012) Closes #1977 --- content/en/functions/urls.Parse.md | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/content/en/functions/urls.Parse.md b/content/en/functions/urls.Parse.md index 988ecb3ac..9ed4f55d3 100644 --- a/content/en/functions/urls.Parse.md +++ b/content/en/functions/urls.Parse.md @@ -1,31 +1,33 @@ --- title: urls.Parse -description: Parse parses a given URL, which may be relative or absolute, into a URL structure. -date: 2017-09-25 -publishdate: 2017-09-25 -lastmod: 2017-09-25 +description: Parses a URL into a URL structure. categories: [functions] menu: docs: parent: "functions" keywords: [urls] signature: ["urls.Parse URL"] -workson: [] -hugoversion: -deprecated: false aliases: [] --- -`urls.Parse` takes a url as input +The `urls.Parse` function parses a URL into a [URL structure](https://godoc.org/net/url#URL). The URL may be relative (a path, without a host) or absolute (starting with a [scheme]). Hugo throws an error when parsing an invalid URL. + +[scheme]: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml#uri-schemes-1 ```go-html-template -{{ $url := urls.Parse "http://www.gohugo.io" }} -``` +{{ $url := "https://example.org:123/foo?a=6&b=7#bar" }} +{{ $u := urls.Parse $url}} -and returns a [URL](https://godoc.org/net/url#URL) structure. The struct fields are accessed via the `.` notation: - -```go-html-template -{{ $url.Scheme }} → "http" -{{ $url.Host }} → "www.gohugo.io" +{{ $u.IsAbs }} → true +{{ $u.Scheme }} → https +{{ $u.Host }} → example.org:123 +{{ $u.Hostname }} → example.org +{{ $u.RequestURI }} → /foo?a=6&b=7 +{{ $u.Path }} → /foo +{{ $u.Query }} → map[a:[6] b:[7]] +{{ $u.Query.a }} → [6] +{{ $u.Query.Get "a" }} → 6 +{{ $u.Query.Has "b" }} → true +{{ $u.Fragment }} → bar ```