From 11977b96f3f18e77c3317250d36cc51c79ae78a8 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Wed, 16 Nov 2022 22:58:11 -0800 Subject: [PATCH] Make relURL and related functions consistent (#1895) This addresses changes made in: https://github.com/gohugoio/hugo/pull/10002 Closes #469 --- content/en/functions/abslangurl.md | 64 +++++++++++++++++++------ content/en/functions/absurl.md | 77 +++++++++++++++++------------- content/en/functions/relLangURL.md | 67 +++++++++++++++++++------- content/en/functions/relurl.md | 76 ++++++++++++++++------------- 4 files changed, 187 insertions(+), 97 deletions(-) diff --git a/content/en/functions/abslangurl.md b/content/en/functions/abslangurl.md index d9818fcaf..11d540bde 100644 --- a/content/en/functions/abslangurl.md +++ b/content/en/functions/abslangurl.md @@ -1,27 +1,61 @@ --- title: absLangURL -description: Adds the absolute URL with correct language prefix according to site configuration for multilingual. -date: 2017-02-01 -publishdate: 2017-02-01 -lastmod: 2017-02-01 +description: Returns an absolute URL with a language prefix, if any. categories: [functions] menu: docs: - parent: "functions" -keywords: [multilingual,i18n,urls] + parent: functions +keywords: [urls, multilingual,i18n] signature: ["absLangURL INPUT"] -workson: [] -hugoversion: -relatedfuncs: [relLangURL] -deprecated: false -aliases: [] --- -Both `absLangURL` and [`relLangURL`](/functions/rellangurl/) are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl) relatives but will add the correct language prefix when the site is configured with more than one language. +Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on: -So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`: +- Whether the input begins with a slash +- The `baseURL` in site configuration +- The language prefix, if any +In examples that follow, the project is multilingual with content in both Español (`es`) and English (`en`). The default language is Español. The returned values are from the English site. + +### Input does not begin with a slash + +If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`. + +With `baseURL = https://example.org/` + +```go-html-template +{{ absLangURL "" }} → https://example.org/en/ +{{ absLangURL "articles" }} → https://example.org/en/articles +{{ absLangURL "style.css" }} → https://example.org/en/style.css ``` -{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/" -{{ "blog/" | relLangURL }} → "/hugo/en/blog/" + +With `baseURL = https://example.org/docs/` + +```go-html-template +{{ absLangURL "" }} → https://example.org/docs/en/ +{{ absLangURL "articles" }} → https://example.org/docs/en/articles +{{ absLangURL "style.css" }} → https://example.org/docs/en/style.css ``` + +### Input begins with a slash + +If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`. + +With `baseURL = https://example.org/` + +```go-html-template +{{ absLangURL "/" }} → https://example.org/en/ +{{ absLangURL "/articles" }} → https://example.org/en/articles +{{ absLangURL "/style.css" }} → https://example.org/en/style.css +``` + +With `baseURL = https://example.org/docs/` + +```go-html-template +{{ absLangURL "/" }} → https://example.org/en/ +{{ absLangURL "/articles" }} → https://example.org/en/articles +{{ absLangURL "/style.css" }} → https://example.org/en/style.css + +{{% note %}} +The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function. +{{% /note %}} diff --git a/content/en/functions/absurl.md b/content/en/functions/absurl.md index 808cc00d8..efea2df75 100644 --- a/content/en/functions/absurl.md +++ b/content/en/functions/absurl.md @@ -1,50 +1,61 @@ --- title: absURL -description: Creates an absolute URL based on the configured baseURL. -date: 2017-02-01 -publishdate: 2017-02-01 -lastmod: 2017-02-01 +description: Returns an absolute URL. categories: [functions] menu: docs: - parent: "functions" + parent: functions keywords: [urls] signature: ["absURL INPUT"] -workson: [] -hugoversion: -relatedfuncs: [relURL] -deprecated: false -aliases: [] --- -Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `https://example.com/hugo/`: +With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on: -``` -{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css" -{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css" -{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/" -{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/" +- Whether the input begins with a slash +- The `baseURL` in site configuration + +### Input does not begin with a slash + +If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`. + +With `baseURL = https://example.org/` + +```go-html-template +{{ absURL "" }} → https://example.org/ +{{ absURL "articles" }} → https://example.org/articles +{{ absURL "style.css" }} → https://example.org/style.css ``` -The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data (SEO)][jsonld], where some of your images for a piece of content may or may not be hosted locally: +With `baseURL = https://example.org/docs/` -{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}} - -{{< /code >}} +```go-html-template +{{ absURL "" }} → https://example.org/docs/ +{{ absURL "articles" }} → https://example.org/docs/articles +{{ absURL "style.css" }} → https://example.org/docs/style.css +``` -The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside ` -{{< /code >}} +```go-html-template +{{ relURL "" }} → /docs/ +{{ relURL "articles" }} → /docs/articles +{{ relURL "style.css" }} → /docs/style.css +``` -The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `