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. -->
{{ if and (index .AllTranslations 0).File (index .AllTranslations 1).File }}
{{ $file0 := readFile (index .AllTranslations 0).File.Filename }}
{{ $file1 := readFile (index .AllTranslations 1).File.Filename }}
{{ if eq (sha256 $file0) (sha256 $file1) }}
{{ .Store.Set "en_article_in_fr_site" true }}
{{ 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 }}
{{- /*
-- Detects the english articles published as-is in the french site and
-- sets "en_article_in_fr_site" in the page store.
--
-- This partial must not output anything since it is also called from
-- inline contexts (internalLink shortcode, article headlines).
*/ -}}
{{- if and .IsPage .IsTranslated (eq .Lang "fr") -}}
{{- if and (index .AllTranslations 0).File (index .AllTranslations 1).File -}}
{{- /* Try to compare files by SHA256 fingerprint. */ -}}
{{- $file0 := readFile (index .AllTranslations 0).File.Filename -}}
{{- $file1 := readFile (index .AllTranslations 1).File.Filename -}}
{{- if eq (sha256 $file0) (sha256 $file1) -}}
{{- .Store.Set "en_article_in_fr_site" true -}}
{{- 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.
-- Currently, this shortcode can only handle Hugo sites with two languages. -->
{{ $path := .Get "path" }}
{{ $currentLang := .Page.Lang }}
{{ $title := .Get "title" }}
{{ $sites := hugo.Sites }}
{{ if ne $currentLang (index hugo.Sites 0).Language.Lang }}
{{ $sites = collections.Reverse hugo.Sites }}
{{ end }}
{{ range $sites }}
{{ with .GetPage $path }}
<a href="{{ .RelPermalink }}">
{{ if ne $title "" }}
{{ $title }}
{{ else }}
{{ .Title }}
{{ end }}
</a>
{{ if and (eq $currentLang "en") (eq .Lang "fr") }}
{{- partial "icons/fr.svg" . -}}
{{ end }}
{{ if (eq $currentLang "fr") }}
-- Currently, this shortcode can only handle Hugo sites with two languages.
--
-- The output must not contain any extra whitespace since the link is
-- usually embedded in a sentence.
*/ -}}
{{- $path := .Get "path" -}}
{{- $currentLang := .Page.Lang -}}
{{- $title := .Get "title" -}}
{{- $sites := hugo.Sites -}}
{{- if ne $currentLang (index hugo.Sites 0).Language.Lang -}}
{{- $sites = collections.Reverse hugo.Sites -}}
{{- end -}}
{{- range $sites -}}
{{- with .GetPage $path -}}
<a href="{{ .RelPermalink }}">{{ or $title .Title }}</a>
{{- if and (eq $currentLang "en") (eq .Lang "fr") }} {{ partial "icons/fr.svg" . | strings.TrimSpace | safeHTML }}{{ end -}}
{{- if eq $currentLang "fr" -}}
{{- partial "lang-detection.html" . -}}
{{ if .Store.Get "en_article_in_fr_site" }}
{{- partial "icons/en.svg" . -}}
{{ end }}
{{ end }}
{{ break }}
{{ end }}
{{ end }}
{{- if .Store.Get "en_article_in_fr_site" }} {{ partial "icons/en.svg" . | strings.TrimSpace | safeHTML }}{{ end -}}
{{- end -}}
{{- break -}}
{{- end -}}
{{- end -}}

Loading…
Cancel
Save