theme: edge cases for multilang portable links #377

This commit is contained in:
Sören Weber 2023-10-26 22:47:15 +02:00
parent 087c77dff0
commit 1ff5026bec
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
2 changed files with 14 additions and 13 deletions

View file

@ -58,9 +58,11 @@ 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](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 |

View file

@ -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 }}