From 1ff5026beca0796b15ab95c4a5ff51d67affcddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Thu, 26 Oct 2023 22:47:15 +0200 Subject: [PATCH] theme: edge cases for multilang portable links #377 --- exampleSite/content/basics/migration/_index.en.md | 14 ++++++++------ layouts/partials/shortcodes/link.html | 13 ++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md index 71be50656f..a7d6ca0428 100644 --- a/exampleSite/content/basics/migration/_index.en.md +++ b/exampleSite/content/basics/migration/_index.en.md @@ -57,12 +57,14 @@ This document shows you what's new in the latest release. For a detailed list of The following types of linking are supported: - | link | description | - | -------------------------------- | ------------------------------- | - | `[generator](basics/generator)` | absolute from your project root | - | `[generator](/basics/generator)` | absolute from your project root | - | `[generator](./../generator)` | relative from the current page | - | `[generator](../generator)` | relative from the current page | + | link | description | + | ---------------------------------- | ------------------------------------------- | + | `[generator](en/basics/generator)` | absolute from your project root (multilang) | + | `[generator](/en/basics/generator)`| absolute from your project root (multilang) | + | `[generator](basics/generator)` | absolute from your current language root | + | `[generator](/basics/generator)` | absolute from your current language root | + | `[generator](./../generator)` | relative from the current page | + | `[generator](../generator)` | relative from the current page | --- diff --git a/layouts/partials/shortcodes/link.html b/layouts/partials/shortcodes/link.html index e3d49ca38f..29c5f2fdbe 100644 --- a/layouts/partials/shortcodes/link.html +++ b/layouts/partials/shortcodes/link.html @@ -25,19 +25,18 @@ {{- with $url.Fragment }} {{- $fragment = printf "#%s" . }} {{- end }} - {{- $path := strings.TrimSuffix ".html" $url.Path }} - {{- $path := $url.Path }} - {{- with $page.Page.GetPage $path }} + {{- $path := printf "%s/" (strings.TrimPrefix "/" (strings.TrimSuffix "/" $url.Path)) }} + {{- with $page.Page.GetPage (strings.TrimSuffix "/" $path | default "/") }} {{- $url = printf "%s%s" (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) $fragment }} {{- $found = true }} {{- else }} {{- /* is it a link into another translation? */}} {{- range $page.Site.Languages }} {{- $lang := .Lang }} - {{- $prefix := printf "/%s" $lang }} - {{- if strings.HasPrefix $prefix $path }} - {{- $path := strings.TrimPrefix $prefix $path }} - {{- with $page.Page.GetPage $path }} + {{- $prefix := printf "%s/" $lang }} + {{- if strings.HasPrefix $path $prefix }} + {{- $path = strings.TrimPrefix $prefix $path | default "/" }} + {{- with $page.Site.GetPage (strings.TrimSuffix "/" $path | default "/") }} {{- range .Page.AllTranslations }} {{- if eq $lang .Lang }} {{- $url = printf "%s%s" (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) $fragment }}