Browse Source

remove the stray whitespace around internal links

The internalLink shortcode and the lang-detection partial it calls were
emitting newlines around the link, which rendered as a space before the
following punctuation ("Red Hat Summit Connect France 2024 ,"), in the
pages as well as in the summaries, meta descriptions and RSS feeds.

Refs #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YNHsWYTXPThURz6TbVS1j3
master
Nicolas Massé 1 month ago
parent
commit
8e5c14917e
  1. 37
      layouts/_partials/lang-detection.html
  2. 53
      layouts/_shortcodes/internalLink.html

37
layouts/_partials/lang-detection.html

@ -1,15 +1,22 @@
{{ if and .IsPage .IsTranslated (eq .Lang "fr") }} {{- /*
<!-- Try to compare files by SH256 fingerprint. --> -- Detects the english articles published as-is in the french site and
{{ if and (index .AllTranslations 0).File (index .AllTranslations 1).File }} -- sets "en_article_in_fr_site" in the page store.
{{ $file0 := readFile (index .AllTranslations 0).File.Filename }} --
{{ $file1 := readFile (index .AllTranslations 1).File.Filename }} -- This partial must not output anything since it is also called from
{{ if eq (sha256 $file0) (sha256 $file1) }} -- inline contexts (internalLink shortcode, article headlines).
{{ .Store.Set "en_article_in_fr_site" true }} */ -}}
{{ end }} {{- if and .IsPage .IsTranslated (eq .Lang "fr") -}}
{{ else }} {{- if and (index .AllTranslations 0).File (index .AllTranslations 1).File -}}
<!-- Sometimes there is no file though. In that case, compare the content. --> {{- /* Try to compare files by SHA256 fingerprint. */ -}}
{{ if eq (index .AllTranslations 0).Plain (index .AllTranslations 1).Plain }} {{- $file0 := readFile (index .AllTranslations 0).File.Filename -}}
{{ .Store.Set "en_article_in_fr_site" true }} {{- $file1 := readFile (index .AllTranslations 1).File.Filename -}}
{{ end }} {{- if eq (sha256 $file0) (sha256 $file1) -}}
{{ end }} {{- .Store.Set "en_article_in_fr_site" true -}}
{{ end }} {{- end -}}
{{- else -}}
{{- /* Sometimes there is no file though. In that case, compare the content. */ -}}
{{- if eq (index .AllTranslations 0).Plain (index .AllTranslations 1).Plain -}}
{{- .Store.Set "en_article_in_fr_site" true -}}
{{- end -}}
{{- end -}}
{{- end -}}

53
layouts/_shortcodes/internalLink.html

@ -1,31 +1,26 @@
<!-- This shortcode generates the HTML link to an internal resource, which can be translated or not. {{- /*
-- This shortcode generates the HTML link to an internal resource, which can be translated or not.
-- It displays a country flag when the target page is in a different language than the current page. -- It displays a country flag when the target page is in a different language than the current page.
-- Currently, this shortcode can only handle Hugo sites with two languages. --> -- Currently, this shortcode can only handle Hugo sites with two languages.
{{ $path := .Get "path" }} --
{{ $currentLang := .Page.Lang }} -- The output must not contain any extra whitespace since the link is
{{ $title := .Get "title" }} -- usually embedded in a sentence.
{{ $sites := hugo.Sites }} */ -}}
{{ if ne $currentLang (index hugo.Sites 0).Language.Lang }} {{- $path := .Get "path" -}}
{{ $sites = collections.Reverse hugo.Sites }} {{- $currentLang := .Page.Lang -}}
{{ end }} {{- $title := .Get "title" -}}
{{ range $sites }} {{- $sites := hugo.Sites -}}
{{ with .GetPage $path }} {{- if ne $currentLang (index hugo.Sites 0).Language.Lang -}}
<a href="{{ .RelPermalink }}"> {{- $sites = collections.Reverse hugo.Sites -}}
{{ if ne $title "" }} {{- end -}}
{{ $title }} {{- range $sites -}}
{{ else }} {{- with .GetPage $path -}}
{{ .Title }} <a href="{{ .RelPermalink }}">{{ or $title .Title }}</a>
{{ end }} {{- if and (eq $currentLang "en") (eq .Lang "fr") }} {{ partial "icons/fr.svg" . | strings.TrimSpace | safeHTML }}{{ end -}}
</a> {{- if eq $currentLang "fr" -}}
{{ if and (eq $currentLang "en") (eq .Lang "fr") }}
{{- partial "icons/fr.svg" . -}}
{{ end }}
{{ if (eq $currentLang "fr") }}
{{- partial "lang-detection.html" . -}} {{- partial "lang-detection.html" . -}}
{{ if .Store.Get "en_article_in_fr_site" }} {{- if .Store.Get "en_article_in_fr_site" }} {{ partial "icons/en.svg" . | strings.TrimSpace | safeHTML }}{{ end -}}
{{- partial "icons/en.svg" . -}} {{- end -}}
{{ end }} {{- break -}}
{{ end }} {{- end -}}
{{ break }} {{- end -}}
{{ end }}
{{ end }}

Loading…
Cancel
Save