Skip to content

Suggestion for adding target=_blank for menu items #1706

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

Open
markbeachill opened this issue Mar 28, 2025 · 1 comment
Open

Suggestion for adding target=_blank for menu items #1706

markbeachill opened this issue Mar 28, 2025 · 1 comment

Comments

@markbeachill
Copy link

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 #1705

@nicolas-goudry
Copy link

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>```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants