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

Remove canonifyURLs support #4733

Open
bep opened this issue May 18, 2018 · 11 comments
Open

Remove canonifyURLs support #4733

bep opened this issue May 18, 2018 · 11 comments

Comments

@bep
Copy link
Member

bep commented May 18, 2018

See gohugoio/hugoDocs#469

This is also related to #4574 -- and we should make sure to do this and that in the same release.

We currently have a fair amount of (some of it slowish) code to support canonifyURLs, and there are several issues with it. Issues that are very hard to fix in a simple way. I don't use canonifyURLs, and I'm not willing to spend time fixing/maintaining it.

So until someone steps up to the plate with some magic, we are removing it.

/cc @spf13

@kaushalmodi
Copy link
Contributor

kaushalmodi commented May 18, 2018

Currently sites that have baseURLs with subdirs have to use canonifyURLs = true as a crutch to make links like ![](/images/gopher.png) work.

See this test page (best viewed in widescreen so that the nested frame can be seen side-by-side).

The said example image link works with canonifyURLs = true

image

The said example image link fails with canonifyURLs = false (default)

image

@bep
Copy link
Member Author

bep commented May 18, 2018

@kaushalmodi #4457 should solve the Markdown part.

But why do you have to use canonifyURLs = true to get your example working? Note the boldness in my "have". There is an obvious workaround when you say that an URL in Markdown with a leading slash is relative to the host. You have a similar challenge with languages.

So, /subpath/images/gopher.png isn't very portable, but it works. And you can create yourelf a shortcode if you really want.

#4457 solves the Markdown part for both sub-paths and languages in a much cleaner and effective way.

So what you lose by this is the canonification of the URLs inside the templates. And that, my friend, is work for absURL.

(#4457 will only work for BF and possibly MMark).

@kaushalmodi
Copy link
Contributor

So, /subpath/images/gopher.png isn't very portable

Exactly! I am aware of that workaround.. but portability is a big reason I am not going that route. Tomorrow if a site moves from subdir-domain to regular domain, the needed refactoring, while possible, is still an ugly job.

And you can create yourelf a shortcode if you really want.

I prefer to use regular Markdown links as much as possible.

#4457 solves the Markdown part for both sub-paths and languages in a much cleaner and effective way.

I'll subscribe to that issue (You obviously meant #4574 :)). I'll definitely try it out once you have a PR for that.

So what you lose by this is the canonification of the URLs inside the templates. And that, my friend, is work for absURL.

Yes, I am fine with that. The real-world usage is mainly linking to figures in content Markdown files, which look like the example I posted above.

@Jos512
Copy link

Jos512 commented May 18, 2018

I use canonifyURLs on my websites to translate relative URLs to absolute ones. But I understand that this feature will be possible with #4574 too? In that case I don't care about removing canonifyURLs, since the same functionality that I currently use will be present in the future.

@bep bep modified the milestones: v0.42, v0.43 Jun 5, 2018
@bep bep modified the milestones: v0.43, v0.44 Jun 30, 2018
@bep bep modified the milestones: v0.44, v0.45, v0.46 Jul 10, 2018
@bep bep modified the milestones: v0.46, v0.47, v0.48 Aug 3, 2018
@bep bep modified the milestones: v0.48, v0.49 Aug 22, 2018
@bep bep modified the milestones: v0.49, v0.50 Sep 13, 2018
@bep bep modified the milestones: v0.50, v0.51 Oct 6, 2018
@bep bep modified the milestones: v0.51, v0.53 Nov 8, 2018
@bep bep modified the milestones: v0.53, v0.54 Dec 6, 2018
@XhstormR
Copy link

XhstormR commented Apr 17, 2024

If i use markdown syntax for images, but we removed canonifyURLs support, What's the best alternative solution for the baseURL with sub directory?

yaml config:

baseURL: https://www.example.com/dir0/

content.md

![](/dir1/dir2/image.png)

output html:

// 404 error https://www.example.com/dir1/dir2/image.png
// should be https://www.example.com/dir0/dir1/dir2/image.png  
<img src="/dir1/dir2/image.png"> // 404 error 

Updated:
Right now I switch to markdown render hooks features https://gohugo.io/render-hooks/images/.

image render hooks:

<img src="{{ site.BaseURL }}{{ .Destination | safeURL }}"
  {{- with .Text }} alt="{{ . }}"{{ end -}}
  {{- with .Title }} title="{{ . }}"{{ end -}}
>

@jmooring
Copy link
Member

You can enable Hugo's embedded link and image render hooks:

Note that these are enabled by default with multilingual single-host sites.

There is never a good reason to use {{ site.BaseURL }} in a template. At some point I hope we deprecate this method as well.

@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
@ksgolding
Copy link

Would like to thank XhstormR for providing an actual solution to the problem! I have to admit, that how Hugo manages paths for images, well and apparently links is strangely fustrating and obtuse, especially for something that should really just work. I expect that the "static" folder is likely causing more problems then it solves.

Here is a subtly alternate to XhstormR solution, not using the apparently bad BaseURL

<img src='{{ urls.RelURL "" }}{{ .Destination | safeURL }}'
  {{- with .Text }} alt="{{ . }}"{{ end -}}
  {{- with .Title }} title="{{ . }}"{{ end -}}
>
{{- /* chomp trailing newline */ -}}

@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@jmooring
Copy link
Member

@ksgolding

especially for something that should really just work

I've written about this many times, with a detailed description here. The short answer is to use link and image render hooks that resolve markdown destinations regardless of site configuration and front matter (e.g., slug and url) values.

The easiest way to handle this is to enable Hugo's embedded link and image render hooks:

You can examine the source code here:

@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants