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

variables do not work with hasPrefix inside render-link #12929

Closed
knutov opened this issue Oct 13, 2024 · 12 comments
Closed

variables do not work with hasPrefix inside render-link #12929

knutov opened this issue Oct 13, 2024 · 12 comments

Comments

@knutov
Copy link

knutov commented Oct 13, 2024

$ hugo version # from github

hugo v0.135.0-f30603c47f5205e30ef83c70419f57d7eb7175ab+extended linux/amd64 BuildDate=2024-09-27T13:17:08Z VendorInfo=gohugoio

layouts/_default/_markup/render-link.html:

<pre>DEBUG:
	{{ hasPrefix .Destination "http" }}
	{{ eq (substr (.Destination | lower ) 0 4) "http" }}
	{{ findRE `http` .Destination }}
	{{ hasPrefix "https://my.test.link/" "http" }}

	.Destination: {{ .Destination }}
	.Title: {{ .Title }}
	.Text: {{ .Text }}
</pre>

and I have this result after render:

DEBUG:
	false <--- expected true
	false <--- expected true
	[] <--- expected to find something
	true <--- hasPrefix "https://my.test.link/" "http" works if it is a string, not variable

	.Destination: https://my.test.link/  
	.Title: 
	.Text: link text
@bep
Copy link
Member

bep commented Oct 13, 2024

I'm pretty sure hasPrefix works fine with variables and render-links, but I cannot spot the error in the snippet you posted.

@knutov
Copy link
Author

knutov commented Oct 13, 2024

It works before.
I just upgraded to the latest hugo version, did fixes for new .Summary behavior and started to check is everything else works fine.

Ans this snippet is not working as earlier.

If link inside markdown is https://my.test.link/ I do expect hasPrefix 'http' to be true - I use it to add target="_blank". With latest version it is not working.

@knutov
Copy link
Author

knutov commented Oct 13, 2024

my actual snippet (without debug code) is

<a href="{{ .Destination | safeURL }}"
	{{- with .Title}} title="{{ . }}"{{ end }}
	{{- if strings.HasPrefix .Destination "mailto" -}} target="_blank" {{end}}
	{{- if or
		( strings.HasPrefix .Destination "http" )
		( strings.HasPrefix .Destination "https" )
		( strings.HasSuffix .Destination ".pdf" )
	-}}
	target="_blank" rel="noopener"{{ end }}>{{- .Text | safeHTML -}}</a>
{{- partial "empty.html" . -}}

@jmooring
Copy link
Member

I am not able to reproduce the problem as described. Try it:

git clone --single-branch -b hugo-github-issue-12929 https://github.com/jmooring/hugo-testing hugo-github-issue-12929
cd hugo-github-issue-12929
hugo server

@knutov
Copy link
Author

knutov commented Oct 13, 2024

I am not able to reproduce the problem as described. Try it:

Indeed, this minimal example is working correct. What can go wrong in my case?

@jmooring
Copy link
Member

Does one of your Markdown links use a shortcode for the destination? For example:

[link text]({{< some-shortcode xxxx >}} "link title")

@knutov
Copy link
Author

knutov commented Oct 13, 2024

Well, I found the minimal repoducer based on your branch:

https://github.com/knutov/hugo-testing/tree/hugo-github-issue-12929

knutov/hugo-testing@85593be

@knutov
Copy link
Author

knutov commented Oct 13, 2024

Does one of your Markdown links use a shortcode for the destination? For example:

[link text]({{< some-shortcode xxxx >}} "link title")

yes, it's param

@jmooring
Copy link
Member

Use the {{% .. %}} notation instead of {{< ... >}}.

@knutov
Copy link
Author

knutov commented Oct 13, 2024

Use the {{% .. %}} notation instead of {{< ... >}}.

It works! Thanks! Is it about https://gohugo.io/content-management/shortcodes/#shortcodes-with-markdown ?

@jmooring
Copy link
Member

Yes, sort of.

Copy link

github-actions bot commented Nov 4, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants