Anyone have a ref shortcode that auto-generates a link with multilingual Title support?

I am looking for a way to use ref/relref shortcode to auto-generate as a link with the correct Title automatically pulled.

(Test repo: Test repo)

Currently we have to do this manually:

[title]({{< ref "somefile.md" >}})

or in a multilingual site:

[title]({{< ref path="somefile.md" lang="ja" >}})

The more cross-referencing you have, the harder it is to track, and edit (if needed). So, I’m thinking of creating a new shortcode for just this purpose.

However, there is no title params for the ref function, there are only path, lang, and outputFormat.

I also tried:

{{ with .Site.GetPage "somefile" }}{{ .Title }}{{ end }}

But it only pulls information from the exact same language the viewed page is set to. So if the said page is in “ko”, then .Site.GetPage will only search ‘ko’ related files.

For example, this does not work:

{{ with .Site.GetPage "/ja/kb/webdev/test-internal.md" }}{{ .Title }}{{ end }}

But without the lang folder, it works but will pull the same language as the current page:

{{ with .Site.GetPage "/kb/webdev/test-internal.md" }}{{ .Title }}{{ end }}

Sample code in .md files

* {{< reftitle path="reftitle-test.md" lang="en-ph" >}}
* {{< reftitle path="reftitle-test.md" lang="ja" >}}
* {{< reftitle path="reftitle-test.md" lang="ko" >}}

I currently have the following in my reftitle.html shortcode

{{- $lang := .Get "lang" -}}
{{- $path := .Get "path" -}}
{{- $ref := relref . .Params | urlize -}}
<a href="{{ $ref }}">{{ with .Site.GetPage $ref }}{{ .Title }}{{ end }}</a>
  • The links are correct since it is using the ref.
  • However, I can not pull the appropriate link title.
    • In the example code above, it is empty because it can not find the referenced file because $ref value is /ja/kb/webdev/test-internal.md

Test repo: Test repo

Or, I’m approaching this incorrectly. ^^;;

Thank you, shalom!

Shortcode, I named it “reflink.html”

{{ with site.GetPage (.Get 0) }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}

Use it like this:

{{< reflink "somefile.md" >}}

And it will output:

<a href="/path/to/somefile/">Some files title</a>

Read more at .GetPage | Hugo.

2 Likes

I happen to have written about that exact a similar topic recently – the titleref shortcode that I describe accepts the page title, and it figures out the associated RelPermalink internally :smiley:

See if that helps you. I don’t show the lang support. But you should be able to build upon the idea I present there.

2 Likes

Ooh, found a way to do it!

I was looking into @kaushalmodi’s titleref and it made me remember .Translations

Here’s the new code for the shortcode file:

{{- $path := .Get "path" -}}
{{- $ref := relref . .Params | urlize -}}
{{ with .Site.GetPage $path }}
  {{ range .AllTranslations }}
    {{ if eq $ref .RelPermalink }}<a href="{{ .RelPermalink }}">{{ .Title }}</a>{{ break }}{{ end }}
  {{ end }}
{{ end }}

Having the following in your .md file:

* {{< reflangtitle path="reftitle-test.md" lang="en-ph" >}}
* {{< reflangtitle path="reftitle-test.md" lang="ja" >}}
* {{< reflangtitle path="reftitle-test.md" lang="ko" >}}

Will now generate the proper multilingual title and automatic linking.

I also updated the test site source code: Test source.

With this shortcode, it is now possible:

  1. use ref with an automatic link generation
  2. the generated link pulls the multilingual title regardless of the current page’s lang

Useful when referencing a post written only in one language from a page/post written in a different language.

Example: if you have a Chinese article and you want to link to another article which is only available in English, you can use the shortcode to autogenerate the link and the title (you don’t have to open that article and copy-paste the title into your new content).

By the way, thank you for the {{ break }} feature, without it there are extra lines generated, very useful.

Thank you all. Shalom!

3 Likes