theme: make page be servable with uglyURLs=false from the FS by default

This commit is contained in:
Sören Weber 2022-11-06 00:34:33 +01:00
parent f2523234bd
commit ea266f26fd
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 28 additions and 20 deletions

View file

@ -1,15 +1,20 @@
# this is a required setting for this theme to appear on https://themes.gohugo.io/ # this is a required setting for this theme to appear on https://themes.gohugo.io/
# change this to a value appropriate for you # change this to a value appropriate for you; if your site is served from a subdirectory
baseURL = "https://example.com" # set it like "https://example.com/mysite/"
baseURL = "https://example.com/"
# as the official Hugo documentation recommends, we turn this off for this # canonicalization will only be used for the sitemap.xml and index.xml files;
# showcase; this allows the published site to be served by both http and https # if set to false, a site served from a subdirectory will generate wrong links
canonifyURLs = false # true -> all relative URLs would instead be canonicalized using baseURL # inside of the above mentioned files; if you serve the page from the servers root
# you are free to set the value to false as recommended by the official Hugo documentation
# required for this documentation to be served from subfolder canonifyURLs = true # true -> all relative URLs would instead be canonicalized using baseURL
relativeURLs = false # true -> rewrite all relative URLs to be relative to the current content # required value to serve this page from a webserver AND the file system;
# if you don't want to serve your page from the file system, you can also set this value
# if you set uglyURLs to true, don't forget to set it also in the [params] section # to false
relativeURLs = true # true -> rewrite all relative URLs to be relative to the current content
# if you set uglyURLs to false, this theme will append 'index.html' to any branch bundle link
# so your page can be also served from the file system; if you don't want that,
# set disableExplicitIndexURLs=true in the [params] section
uglyURLs = false # true -> basic/index.html -> basic.html uglyURLs = false # true -> basic/index.html -> basic.html
# the directory where Hugo reads the themes from; this is specific to your # the directory where Hugo reads the themes from; this is specific to your
@ -32,7 +37,6 @@ title = "Hugo Relearn Documentation"
# settings specific to this theme's features; choose to your likings and # settings specific to this theme's features; choose to your likings and
# consult this documentation for explaination # consult this documentation for explaination
[params] [params]
uglyURLs = false # see uglyURLs setting above
editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/" editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/"
description = "Documentation for Hugo Relearn Theme" description = "Documentation for Hugo Relearn Theme"
author = "Sören Weber" author = "Sören Weber"

View file

@ -80,17 +80,19 @@ Note that some of these parameters are explained in details in other sections of
collapsibleMenu = false collapsibleMenu = false
# If a single page can contain content in multiple languages, add those here # If a single page can contain content in multiple languages, add those here
additionalContentLanguage = [ "en" ] additionalContentLanguage = [ "en" ]
# Must be set to the same value as the global uglyURLs setting; this adds 'index.html' to all home links if set to true # If set to true, no index.html will be appended to prettyURLs; this will cause pages not
uglyURLs = false # to be servable from the file system
disableExplicitIndexURLs = false
``` ```
## Serving your page from a subfolder ## Serving your page from a subfolder
The theme runs best if your site is installed in the root of your webserver. If your site is served from a subfolder, eg. `https://example.com/mysite/`, you have to set the following lines to your `config.toml` If your site is served from a subfolder, eg. `https://example.com/mysite/`, you have to set the following lines to your `config.toml`
````toml ````toml
baseURL = "https://example.com/mysite/" baseURL = "https://example.com/mysite/"
canonifyURLs = true canonifyURLs = true
relativeURLs = true
```` ````
Without `canonifyURLs=true` URLs in sublemental pages (like `sitemap.xml`, `rss.xml`) will be generated falsly while your HTML files will still work. See https://github.com/gohugoio/hugo/issues/5226. Without `canonifyURLs=true` URLs in sublemental pages (like `sitemap.xml`, `rss.xml`) will be generated falsly while your HTML files will still work. See https://github.com/gohugoio/hugo/issues/5226.
@ -101,15 +103,17 @@ If you want your page served from the filesystem by using URLs starting with `fi
````toml ````toml
relativeURLs = true relativeURLs = true
uglyURLs = true
[params]
uglyURLs = true
```` ````
Because the global `uglyURLs` setting is not queryable in the theme's code, you'll have to set this option also in the `[params]` section of your `config.toml`. The theme will append an additional `index.html` to all branch bundle links by default to make the page be servable from the file system. If you don't care about the file system and only serve your page via a webserver you can also generate the links without this change by adding this to your `config.toml`
````toml
[params]
disableExplicitIndexURLs = true
````
{{% notice note %}} {{% notice note %}}
If you want to use the search feature with an existing installation make sure to change your outputformat for the homepage from the now deprecated `JSON` to `SEARCH` [as seen below](#activate-search). If you want to use the search feature from the file system using an older installation of the theme make sure to change your outputformat for the homepage from the now deprecated `JSON` to `SEARCH` [as seen below](#activate-search).
{{% /notice %}} {{% /notice %}}
## Activate search ## Activate search

View file

@ -1,7 +1,7 @@
{{- $page := .page }} {{- $page := .page }}
{{- $to := .to }} {{- $to := .to }}
{{- $link := $to.RelPermalink }} {{- $link := $to.RelPermalink }}
{{- if and .page.Site.Params.uglyURLs (eq (substr $link -1) "/") }} {{- if and (ne .page.Site.Params.disableExplicitIndexURLs true) (eq (substr $link -1) "/") }}
{{- $link = printf "%sindex.html" $link }} {{- $link = printf "%sindex.html" $link }}
{{- end }} {{- end }}
{{- $link }} {{- $link }}