Hugo post directory appearing on homepage, but not posts - hugo

For my blogdown-created website (using the Xmin theme), the posts directory - from which I can view individual posts - appears on the homepage, instead of individual posts.
https://joshuamrosenberg.com/
This seemed to happen after updating to the lastest version of Hugo. The source for my website is here. I'm a bit puzzled about what to do: do you have any advice?

This is due to a breaking change in Hugo 0.57.0, and I have fixed the issue in the latest version of the XMin theme. Basically you need to replace
{{ range (where .Data.Pages "Section" "!=" "") }}
with
{{ $pages := .Pages }}
{{ if .IsHome }}{{ $pages = .Site.RegularPages }}{{ end }}
{{ range (where $pages "Section" "!=" "") }}
in the template file layouts/_default/list.html.

Related

How do I avoid nil pointer evaluating resource.Resource.Permalink with Hugo static site generator using range .Pages?

I'm trying to create a schema for events within a range of pages. This works fine for a single page but I'm trying to add on my list template so I have all the events. I'm getting the following error currently: nil pointer evaluating resource.Resource.Permalink
....{{ define "JSON-LD" }}
{{ range .Pages }}
<script type='application/ld+json'>
{
"#context": "https://www.schema.org",
"#type": "Event",
"name": "{{.Description}}",
"url": "{{.Params.eventURL}}",
{{ with .Params.images -}}
"image": [
{{- range $index, $element := . -}}
{{- if ne $index 0 -}}, {{ end }}
{{ $image := $.Resources.GetMatch $element -}}
{{ $image.Permalink -}}
{{- end }}
],
{{ end -}}....
That error is due to the Resources.GetMatch coming back nil and therefore their is a nil pointer when you try to get {{$image.Permalink}}
{{ with }} is a good option to check for success before you try to get the Permalink.
However, the error you describe is an error you probably want as it means you have a resource not found - i.e. $element is not a valid resource in that case, which you should fix in the markdown or wherever you are getting it from as then, you have no real, at build time, way to know you have an invalid resource.
I hope that helps.

Issue with double-escaped entities in the link

I am getting double-escaped entities in the link in a post. For example, a link What's the difference becomes What&rsquo;s the difference.
The problem is similar to this https://discourse.gohugo.io/t/solved-why-is-page-title-getting-double-escaped-entities/14935 however it didn't help me solve this.
Why is this happening? How do I solve it?
The code for the blog is available here - https://github.com/desecho/blog
Your renderhook:
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank"{{ end }}>{{ .Text }}
Change it to:
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank"{{ end }}>{{ .Text | safeHTML }}
That should do the trick.
Now, the reason why is, well Turtles all the way down, but, realize that Hugo is working with a Markdown engine "goldmark" and you have your config set "unsafe=true", but even beyond that you now have HUGO creating/building with your render hook back to markdown. Not clear to me how they all work together, but their is empirically a lot of back and forth...
So... The above should be fine.
Please test and let me know.
(Providing your code was key, and probably a note for your future-self, if the above resolves your problem, of that).
Bien suerte!

Where does imageConfig (HUGO) look for images when building the site?

I'm trying to get the size (width and height) of an image located inside my static folder
I wrote:
{{$imagelink := print "/static" (.Params.image | relURL) }}
{{$imgData := imageConfig $imagelink }}
<p>{{ $imgData.Height }}</p>
<p>{{ $imgData.Width }}</p>
where .Params.image is taken from the YAML metadata, e.g.: image: "images/my-image.PNG" of each post.
The previous works well if I save the file when the server is running. I can get the width and the height, but if I build my site with build_site(local = TRUE) (I'm using blogdown) HUGO can't find the images.
I tried debugging the error with {{ errorf "Specified file at %s not found." $imagelink }}, but whatever path I try it never founds the images.
I also followed this tutorial about imageConfig, where they point out the following:
but {{$imagelink := print "/static/static" (.Params.image | relURL) }} doesn't work either.
I haven't found official documentation regarding this issue. Any help is appreciated.
This issue was solved upgrading HUGO and blogdown to the latest version

Including source files from a repo in HUGO posts

I'm setting up a blog with HUGO, as a commented code repo, and I'm including source files right from the repos on the posts.
I've been able to get it to a working condition and want to improve it but I'm stuck.
What I've done
There's a .gitignore'd repos dir inside the HUGO site root, that holds the source code repos.
There's a getSourceFile.html shortcode:
{{ with .Get 0 }}
<pre><code>{{ readFile . }}</code>
<span class="source-footer">{{.}}</span>
</pre>
{{ end }}
Then, in the post I can use the shortcode like so:
#### Base/EntityTypeConfiguration.cs
Estas clases permiten manejar una clase de configuraciĆ³n por cada clase del modelo...
{{< getSourceFile "repos/EFCoreApp/src/EFCore.App/Base/EntityTypeConfiguration.cs" >}}
and I get this:
Which is pretty nice, since I don't have to copy and paste code, it's 100% up to date and I am sure it compiles.
But this is where I'm stuck!
What I would like to do
1) Setting up the repo root in the front matter so the shortcode is simpler to use, like so:
{{< getSourceFile "src/EFCore.App/Base/EntityTypeConfiguration.cs" >}}
2) Being able to pass the language as a parameter to the shortcode to use it in the highlighting feature, something like so (this does not work):
{{< getSourceFile "src/EFCore.App/Base/EntityTypeConfiguration.cs" "csharp" >}}
getSourceFile.html:
{{ with .Get 0 }}
```{{.Get 1}}
<pre><code>{{ readFile . }}</code>
<span class="source-footer">{{.}}</span>
</pre>
```
{{ end }}
Or better yet, infer it from the file extension! ;-)
I think it shouldn't be too difficult but this is my first experience with Hugo, Go and the Templates, so, Could someone help me with this, please?
Thanks in advance.
I finally got the answer in HUGO's dicussion forum, so I just wanted to post it here to finish the question.
This is the final shortcode:
{{ $file := .Get 0 }}
{{ $repoFilePath := printf "%s/%s" $.Page.Params.reponame $file }}
{{ $repoFile := printf "repos/%s" $repoFilePath }}
{{ $fileExt := replace (index (findRE "(\\.)\\w+$" $file) 0) "." "" }}
<pre><code class="language-{{ $fileExt }}">{{ readFile $repoFile }}</code>
<span class="source-footer">{{ $repoFilePath }}</span>
</pre>
And this even solves the language highlighting from the file extension.

In jinja2 on Google App Engine, how can I (easily) build a URL based on a route name with arguments?

If I construct a jinja environment as follows:
jinja_environment = jinja2.Environment (
loader=jinja2.FileSystemLoader ((os.path.dirname (__file__), 'templates')), extensions=[])
jinja_environment.globals['url_for'] = webapp2.uri_for
In my templates, I can build simple URLs from a route name when the route does not define any arguments:
{{ url_for('user_home') }}
However, when the route contains an argument as defined by a string such as /invoice/<:\d+>, I am unable to pass any arguments. Calling it in all the following ways fails, with a KeyError "Missing argument "0" to build URI.":
{{ url_for('invoice') }}
{{ url_for('invoice', args=['123']) }}
{{ url_for('invoice', kwargs={'__0__':'123'}) }}
{{ url_for('invoice',_request=None, args=['123'],kwargs={'__0__':'123'}) }}
Existing examples for this seem to be out of date--at least I haven't been able to make them work. What am I missing?
Route('/invoice/<invoice_id>/', handler=invoice_handler, invoice_id='something')
{{ url_for('invoice', invoice_id=123) }}
You can try the above, Jinja is expecting the named parameter you defined your handler.
I think just {{ url_for('invoice', 123) }} should work.

Resources