Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support forced links to bypass language settings #1341

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assets/js/critical/languageSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@

// Event listener for language selection
document.addEventListener('DOMContentLoaded', () => {
// override stored language when query string contains force is true
let params = new URLSearchParams(document.location.search)
let force = params.get('force')
if (force !== null && force.toLowerCase() == 'true') {
setLanguage(document.documentElement.lang)
return
}

// continue with regular code
const storedLanguage = getLanguage()
const languageItems = document.querySelectorAll('#language-selector .dropdown-item')

Expand Down
9 changes: 9 additions & 0 deletions data/structures/link.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ arguments:
default: false
comment: >-
Flag to indicate if a link that contains baseURL host should be forced as external.
force:
type: bool
optional: true
default: false
comment: >-
Flag to indicate a link should bypass any language redirection. Only applicable when
the site param `enableLanguageSelectionStorage` is set to true. When `force` is true,
the link to a local page is kept as is.
release: v0.27.20
class:
type: string
optional: true
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/content/en/blog/bootstrap-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ As an example, the following shortcodes render links in different formats.
- {{</* link url="../projects/another-project" case=false /*/>}}
- {{</* link "about" /*/>}}
- {{</* link "/fr/a-propos/" /*/>}}
- {{</* link "/fr/a-propos" >}}About (French){{< /link */>}}
- {{</* link href="/fr/a-propos" force=true >}}About (French){{< /link */>}}
- {{</* link "#image" /*/>}}
- {{</* link "components/#map" /*/>}}

Expand Down
2 changes: 1 addition & 1 deletion exampleSite/content/fr/blog/bootstrap-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ Utilisez le shortcode `file` pour afficher et mettre en évidence le contenu com
- {{</* link url="../projects/another-project" case=false /*/>}}
- {{</* link "about" /*/>}}
- {{</* link "/en/about/" /*/>}}
- {{</* link "/en/about" >}}About (Anglais){{< /link */>}}
- {{</* link url="/en/about/" force=true >}}About (Anglais){{< /link */>}}
- {{</* link "#image" /*/>}}
- {{</* link "components/#map" /*/>}}

Expand Down
9 changes: 9 additions & 0 deletions layouts/partials/assets/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{{- $rel := "" -}}
{{- $case := .case | default true }}
{{- $external := .external | default false }}
{{- $force := .force | default false }}
{{- $cue := .cue | default site.Params.main.externalLinks.cue -}}
{{- $tab := .tab | default site.Params.main.externalLinks.tab -}}
{{- $pretty := site.Params.main.internalLinks.pretty | default false }}
Expand Down Expand Up @@ -104,6 +105,14 @@
{{ if and $pretty (not (hasSuffix $destination "/" )) (not (strings.Contains $destination "#")) }}
{{ $destination = printf "%s/" $destination }}
{{ end }}

{{ if $force }}
{{ if strings.Contains $destination "?" }}
{{ $destination = printf "%s&force=true" $destination }}
{{ else }}
{{ $destination = printf "%s?force=true" $destination }}
{{ end }}
{{ end }}
{{- end -}}

{{ if not $error -}}
Expand Down
4 changes: 3 additions & 1 deletion layouts/shortcodes/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{{ $class := "" }}
{{ $case := true }}
{{ $external := false }}
{{ $force := false }}
{{ $cue := site.Params.main.externalLinks.cue }}
{{ $tab := site.Params.main.externalLinks.tab }}
{{ $text := trim .Inner " \r\n" | .Page.RenderString | safeHTML }}
Expand All @@ -33,6 +34,7 @@
{{- $case = .Get "case" | default true -}}
{{- $external = .Get "external" | default false -}}
{{- $class = .Get "class" | default "" -}}
{{- $force = .Get "force" | default false -}}
{{ else }}
{{ $href = .Get 0 }}
{{ end }}
Expand Down Expand Up @@ -99,5 +101,5 @@

<!-- Main code -->
{{- if not $error -}}
{{ partial "assets/link.html" (dict "destination" $url "text" $text "cue" $cue "tab" $tab "case" $case "external" $external "class" $class "page" .Page) }}
{{ partial "assets/link.html" (dict "destination" $url "text" $text "cue" $cue "tab" $tab "case" $case "external" $external "force" $force "class" $class "page" .Page) }}
{{- end -}}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gethinode/hinode",
"version": "0.27.19",
"version": "0.27.20",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [
"hugo",
Expand Down
Loading