From 88e0459e680b8e2d921b45e1e2c65a75514b7b7d Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sun, 5 May 2019 14:13:49 +0100 Subject: [PATCH] fix: Hugo 0.54+ breaks links to user profiles with non-ASCII usernames * check if the Hugo generated author profile URL is valid * works around the Hugo bug, preventing invalid links * rename `author` folder to `authors` * no need to GetPage authors/x and then GetPage author/x to check if user account was created * can just rename the folder and use `if .File` instead * more aligned with how Hugo's taxonomy system was designed * refactor all author code BREAKING CHANGE: rename `author` folder to `authors` Fix #1064 --- .../{author => authors}/admin/_index.md | 0 .../{author => authors}/admin/avatar.jpg | Bin exampleSite/content/home/about.md | 3 +- layouts/{author => authors}/list.html | 20 ++-- layouts/{author => authors}/terms.html | 14 +-- layouts/index.json | 4 +- layouts/partials/header.html | 2 +- layouts/partials/page_author.html | 89 +++++++++--------- layouts/partials/page_metadata_authors.html | 30 +++--- layouts/partials/widgets/about.html | 6 +- 10 files changed, 80 insertions(+), 88 deletions(-) rename exampleSite/content/{author => authors}/admin/_index.md (100%) rename exampleSite/content/{author => authors}/admin/avatar.jpg (100%) rename layouts/{author => authors}/list.html (57%) rename layouts/{author => authors}/terms.html (53%) diff --git a/exampleSite/content/author/admin/_index.md b/exampleSite/content/authors/admin/_index.md similarity index 100% rename from exampleSite/content/author/admin/_index.md rename to exampleSite/content/authors/admin/_index.md diff --git a/exampleSite/content/author/admin/avatar.jpg b/exampleSite/content/authors/admin/avatar.jpg similarity index 100% rename from exampleSite/content/author/admin/avatar.jpg rename to exampleSite/content/authors/admin/avatar.jpg diff --git a/exampleSite/content/home/about.md b/exampleSite/content/home/about.md index 47a8d49b..13d45394 100644 --- a/exampleSite/content/home/about.md +++ b/exampleSite/content/home/about.md @@ -8,6 +8,7 @@ weight = 20 # Order that this section will appear in. title = "Biography" # Choose the user profile to display -# This should be the username of a profile in your `content/author/` folder. +# This should be the username of a profile in your `content/authors/` folder. +# See https://sourcethemes.com/academic/docs/get-started/#introduce-yourself author = "admin" +++ diff --git a/layouts/author/list.html b/layouts/authors/list.html similarity index 57% rename from layouts/author/list.html rename to layouts/authors/list.html index 6c2ad112..12a4a118 100644 --- a/layouts/author/list.html +++ b/layouts/authors/list.html @@ -1,21 +1,24 @@ +{{/* Author profile page. */}} + {{ partial "header.html" . }} {{ partial "navbar.html" . }} -{{/* Just display author name as title if user profile unavailable. */}} -{{ if not (site.GetPage (printf "/author/%s" (urlize .Title))) }} +{{/* If an account has not been created for this user, just display their name as the title. */}} +{{ if not .File }}

{{ .Title }}

{{ end }} -
+
- {{/* Only show About widget if a profile has been configured for this user. */}} - {{ $widget := "widgets/about.html" }} - {{ $params := dict "root" $ "page" . "author" .Title }} - {{ with site.GetPage (printf "/author/%s" (urlize .Title)) }} + {{/* Show the About widget if an account exists for this user. */}} + {{ if .File }} + {{ $widget := "widgets/about.html" }} + {{ $slug := delimit (last 1 (split (trim .File.Dir "/") "/")) "" }}{{/* Alternatively, use `index .Params.authors 0` */}} + {{ $params := dict "root" $ "page" . "author" $slug }} {{ partial $widget $params }} - {{ end }} + {{end}} {{ $query := where .Pages ".IsNode" false }} {{ $count := len $query }} @@ -35,6 +38,5 @@ {{ end }}
- {{ partial "footer_container.html" . }} {{ partial "footer.html" . }} diff --git a/layouts/author/terms.html b/layouts/authors/terms.html similarity index 53% rename from layouts/author/terms.html rename to layouts/authors/terms.html index 6d9add9d..42dc07b1 100644 --- a/layouts/author/terms.html +++ b/layouts/authors/terms.html @@ -1,3 +1,5 @@ +{{/* List of all authors. */}} + {{ partial "header.html" . }} {{ partial "navbar.html" . }} @@ -11,16 +13,8 @@ diff --git a/layouts/index.json b/layouts/index.json index a97f11f2..4854ae48 100644 --- a/layouts/index.json +++ b/layouts/index.json @@ -3,8 +3,8 @@ {{- $pages := site.RegularPages -}} {{- /* Add the index page of docs separately since it's not in RegularPages above. */ -}} {{- $pages := $pages | union (where (where site.Pages "Kind" "section") "Type" "docs") -}} -{{- /* Add author pages to index so their bios can be searched. Hide empty `/author/` node. */ -}} -{{- $pages := $pages | union (where (where site.Pages "Section" "author") "Params.name" "!=" nil) -}} +{{- /* Add author pages to index so their bios can be searched. Hide empty `/authors/` node. */ -}} +{{- $pages := $pages | union (where (where site.Pages "Section" "authors") "Params.name" "!=" nil) -}} {{- range $pages -}} {{- /* Do not index drafts or private pages. */ -}} diff --git a/layouts/partials/header.html b/layouts/partials/header.html index 1d91cfe0..040493f9 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -13,7 +13,7 @@ {{ $superuser_name := "" }} {{ $superuser_username := "" }} {{ $superuser_role := "" }} - {{ range first 1 (where (where site.Pages "Section" "author") "Params.superuser" true) }} + {{ range first 1 (where (where site.Pages "Section" "authors") "Params.superuser" true) }} {{ $superuser_name = .Params.name }} {{ $superuser_username = delimit (last 1 (split (substr .File.Dir 0 -1) "/")) "" }} {{ $superuser_role = .Params.role }} diff --git a/layouts/partials/page_author.html b/layouts/partials/page_author.html index f2e0df90..bdb9c8da 100644 --- a/layouts/partials/page_author.html +++ b/layouts/partials/page_author.html @@ -1,59 +1,60 @@ +{{/* Author profile box */}} + {{ if not (or (eq site.Params.profile false) (eq .Params.profile false)) }} {{/* Display superuser if superuser exists and page authors are not explicitly specified. */}} {{/* Otherwise, display first author if a profile for them exists. */}} -{{ $author_urlize := "" }} +{{ $author_urlized := "" }} {{ if and (not .Params.authors) (.Scratch.Get "superuser_username") }} - {{ $author_urlize = (.Scratch.Get "superuser_username") }} + {{ $author_urlized = (.Scratch.Get "superuser_username") }} {{ else }} {{ $first_author := "" }} {{ if .Params.authors }} {{ $first_author = index .Params.authors 0 }} {{ end }} - {{ $author_urlize = urlize $first_author }} + {{ $author_urlized = urlize $first_author }} {{ end }} -{{ $person_page_path := (printf "/author/%s" $author_urlize) }} -{{ $person_page := site.GetPage $person_page_path }} -{{ with $person_page }} -{{ $link := printf "/authors/%s" $author_urlize | relURL }} -{{ $avatar := ($person_page.Resources.ByType "image").GetMatch "*avatar*" }} -
- {{ if and site.Params.gravatar .Params.email }} - Avatar - {{ else if $avatar }} - {{ $avatar_image := $avatar.Fill "250x250 Center" }} - Avatar - {{ end }} +{{ $taxonomy := "authors" }} +{{ $profile_page := site.GetPage (printf "/%s/%s" $taxonomy $author_urlized) }} +{{ with $profile_page }} + {{ $avatar := (.Resources.ByType "image").GetMatch "*avatar*" }} +
+ {{ if and site.Params.gravatar .Params.email }} + Avatar + {{ else if $avatar }} + {{ $avatar_image := $avatar.Fill "250x250 Center" }} + Avatar + {{ end }} -
-
{{ .Params.name }}
- {{ with .Params.role }}
{{.}}
{{end}} - {{ with .Params.bio }}

{{. | markdownify | emojify}}

{{end}} - +
+
{{.Params.name}}
+ {{ with .Params.role }}
{{. | markdownify | emojify}}
{{end}} + {{ with .Params.bio }}

{{. | markdownify | emojify}}

{{end}} + +
-
-{{end}} -{{end}} +{{end}}{{/* Profile page block */}} +{{end}}{{/* Show profile block */}} diff --git a/layouts/partials/page_metadata_authors.html b/layouts/partials/page_metadata_authors.html index e8081b67..acf3789e 100644 --- a/layouts/partials/page_metadata_authors.html +++ b/layouts/partials/page_metadata_authors.html @@ -1,23 +1,17 @@ -{{/* If `authors` is set and is not empty. */}} -{{ if $.Params.authors }} -{{ $authorLen := len $.Params.authors }} -{{ if gt $authorLen 0 }} - {{ range $k, $v := $.Params.authors -}} -