HUGO

CSS Postprocess and RelPermalink issue

Hi there,

I am facing an issue getting relPermalink working correctly when using PostProcess

My template code is the following

{{ $resource := resources.Get "css/konidocs.scss" | resources.ToCSS }}
{{ $resource = $resource | resources.PostCSS (dict "config" "assets/cfg/") }}
{{ $resource = $resource | resources.Minify }}
{{ $resource = $resource | resources.PostProcess }}
<link rel="stylesheet" href="{{ $resource.RelPermalink }}">

It is located in header of all pages of my website

When I browse / url output is :
<link rel="stylesheet" href="/css/konidocs.min.css">

This is not correct there is a ‘.’ missing

When I browse: /blog/repairing-krups-ea8800-coffee-machine/ output is :
<link rel="stylesheet" href="/css/konidocs.min.css">

This is not correct…it does not takes into account relative URL.

By changing template code to

{{ $resource := resources.Get "css/konidocs.scss" | resources.ToCSS }}
{{ $resource = $resource | resources.PostCSS (dict "config" "assets/cfg/") }}
{{ $resource = $resource | resources.Minify }}
<link rel="stylesheet" href="{{ $resource.RelPermalink }}">

When I browse / url output is :
<link rel="stylesheet" href="./css/konidocs.min.css">
This is now correct

When I browse: /blog/repairing-krups-ea8800-coffee-machine/ output is :
<link rel="stylesheet" href="../../css/konidocs.min.css">
This is now correct.

Is this a normal behaviour ?

Please re-format your post.

1 Like

It’s a known limitation; you cannot currently use relURLs=true in combination with PostProcess.

Thanks for your help.

I was thinking about a hack to combine relative part of URL with the non relative URL generated by resources.Postprocess

It looks like there is some url transformation that occurs, when relative URL is enabled
Template

href={{ .Site.Home.RelPermalink }}

will render as a relative URL (

href=./

or

href=…/…/

While template

{{ .Site.Home.RelPermalink }}

will render as an absolute URL (not with …/… )

/

It looks to happen in transform/absurlremover.go file.
Can this function be applied in a template ? with something like

{{ .Site.Home.RelPermalink | magicfunction }}

With some string operation I could do a workaround.

Hi,

I finally solved my problem with this template loop which generates the relative part of the Url by counting the number of / in the current page .RelPermalink

This is a dirty workaround, but it works fine. I hope it will be usefull to others :grinning:

{{ $resource := resources.Get "css/konidocs.scss" | resources.ToCSS }}
{{ $resource = $resource | resources.PostCSS (dict "config" "assets/cfg/") }}
{{ $resource = $resource | resources.Minify }}
{{ $resource = $resource | resources.PostProcess }}

    {{ $deep := strings.Count "/" (strings.TrimPrefix .Site.BaseURL .Permalink) }}
    {{ $deep = sub $deep 1 }}
    {{ $relbase = "." }}
    {{ if gt $deep 0 }}
        {{ $relbase = "" }}
        {{ range $i, $sequence := (seq $deep) }}
            {{ $relbase = (print $relbase "../") }}
        {{ end }}
        {{ $relbase = strings.TrimSuffix "/" $relbase }}
    {{ end }}      
<link rel="stylesheet" href="{{ $relbase }}{{ $resource.RelPermalink }}">

Have a nice day !

2 Likes

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