hugo-PaperMod icon indicating copy to clipboard operation
hugo-PaperMod copied to clipboard

Suggestion for adding target=_blank for menu items

Open markbeachill opened this issue 9 months ago • 1 comments

Suggestion for adding target=_bkank for menu items

in the config: - identifier: text-editor name: Text editor url: https://onlinenotepad.org/app params: target: _blank

In the header.html partial template code (at the end). It adss a little bit of logic that creates and populates a $targetBlank string Ot then outputs this string in the <a href part

   <ul id="menu">
        {{- range site.Menus.main }}
        {{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL) ) | absLangURL }}
        {{- $page_url:= $currentPage.Permalink | absLangURL }}
        {{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
	{ {- $targetBlank := "" }}
	{{- with .Params.target }}
		{{- if eq . "_blank" }}
		{{- $targetBlank = ` target="_blank" rel="noopener"` }}
		{{- end }}
	{{- end }}
        <li>
            <a href="{{ .URL | absLangURL }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
            {{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }} {{ $targetBlank | safeHTMLAttr }}>

Originally posted by @markbeachill in https://github.com/adityatelange/hugo-PaperMod/discussions/1705

markbeachill avatar Mar 28 '25 10:03 markbeachill

I was about to suggest more or less the same change.

However, I think that this option should not explicitly expect _blank, but rather something like isExternal. It makes more sense to me since in your example the only valid value for Params.target is _blank. Also, it somehow abstract the implementation details (ie. the target option value being _blank).

It would give something like this:

        <ul id="menu">
            {{- range site.Menus.main }}
            {{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL) ) | absLangURL }}
            {{- $page_url:= $currentPage.Permalink | absLangURL }}
            {{- $is_search := eq (site.GetPage .KeyName).Layout `search` }}
            <li>
                <a href="{{ .URL | absLangURL }}" title="{{ .Title | default .Name }} {{- cond $is_search (" (Alt + /)" | safeHTMLAttr) ("" | safeHTMLAttr ) }}"
                {{- cond $is_search (" accesskey=/" | safeHTMLAttr) ("" | safeHTMLAttr ) }}
                {{- with .Params.isExternal -}}
                target="_blank" rel="noopener"
                {{- end }}>
                    <span {{- if eq $menu_item_url $page_url }} class="active" {{- end }}>
                        {{- .Pre }}
                        {{- .Name -}}
                        {{ .Post -}}
                    </span>
                    {{- if (findRE "://" .URL) }}&nbsp;
                    <svg fill="none" shape-rendering="geometricPrecision" stroke="currentColor" stroke-linecap="round"
                        stroke-linejoin="round" stroke-width="2.5" viewBox="0 0 24 24" height="12" width="12">
                        <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
                        <path d="M15 3h6v6"></path>
                        <path d="M10 14L21 3"></path>
                    </svg>
                    {{- end }}
                </a>
            </li>
            {{- end }}
        </ul>```

nicolas-goudry avatar Mar 29 '25 22:03 nicolas-goudry