Internal link to words in the same post

I want to use the markdown markup to create internal links in the same post.
For example, I want to link the word “Hugo” to the other words “Hugo” contained in the same post.
Is it possible?

A solution to this question has been posted in the forum before.

And here is the sample shortcode for anchors in the same page: Stéphane HUC :: IT Log :: Hugo: Shortcodes

1 Like

Thank you.
The author said that it is quite complicated … :fearful:

You simply need to create a /layouts/shortcodes/anchor.html with the following contents:

{{ $txt := .Get 0 | safeHTML }}{{ $name := .Get 1 | lower | safeHTML }}{{ $anchor := anchorize $name }}
<a href="{{ printf "%s" .Page.RelPermalink }}#{{ $anchor }}" title="{{ i18n "shortcodeAnchorTitle" }}{{ $name }}">{{ $txt }}</a>

Then in your content file you use the shortcode input wherever you need an anchor:
{{< anchor "Text" "Target" >}}

Also have a look at the documentation for Shortcodes

2 Likes

Thank you.
I created the anchor.html as you said.
Regarding the Shortcode, in my case, I understand that the first word (for example Hugo) is the “Text”.
What is the “Target”?
The following word Hugo in the same post content?

The id of the word you are linking to.

For example: {{< anchor "Text" "Hugo" >}}

Would link to <span id="hugo">Hugo</span>

You would need to write HTML in your text to assign the necessary id for the link anchor to work. Also note that each id needs to be unique.

1 Like

Thank you.
Just the last question: the file anchor.html should be under themes/PaperMod/layouts/shortcodes or under /layouts/shortcodes ?
BtW, I see that under themes/PaperMod/layouts/partials/ there is the file anchored_headings.html with the following content:

{{- /* formats .Content headings by adding an anchor */ -}}
{{ . | replaceRE "(<h[1-6] id=\"([^\"]+)\".+)(</h[1-6]+>)" "${1}<a hidden class=\"anchor\" aria-hidden=\"true\" href=\"#${2}\">#</a>${3}" | safeHTML }}

I understand that it is different.

The anchor.html needs to be under /layouts/shortcodes so that the theme can be updated in the future without problems.

The anchored headings shortcode is for headings <h1> <h2>... <h6>, indeed, it has nothing to do with what you’re asking in this topic.

2 Likes

Thank you very much for your availability and kindness.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.