HUGO

A Jekyll to Hugo migration — some questions

I started converting my personal website from Jekyll after spending way too much time in dependency hell. After a day of acquainting myself with Hugo I only look forward to playing more with it.

Conversion and building the theme is mostly working out fine, but I thought I’d gather some questions that have arisen during the process in one thread. I hope that’s fine.

The now function {{ now.Format "2006-01-02"}} doesn’t seem to work in a content page (not a template). How can I use the now function in the body text of a content page? The page is HTML if that matters.

Can I somehow access the slug from my filename in the content dir? Jekyll creates the rendered post slug from the post filename. In Jekyll, Front Matter title: for a given post may be “Carrots are very tasty”, but if filename is 2006-01-02-tasty-carrots.html the rendered filename for :title will be tasty-carrots. I’d like to access tasty-carrots in Hugo. Is it possible? Also, with Jekyll I can also do :title:output_ext to get tasty-carrots.html instead of making it a directory with an index file inside. Is that possible? Which leads me to…

content/about.html too gets rendered as /about/index.html. How can I render it as /about.html?

I have some trouble interpreting the documentation regarding GDPR and “Disable All Services”. It says “An example Privacy Config that disables all the relevant services in Hugo. With this configuration, the other settings will not matter.”

Does the below quoted text mean that all privacy features for Twitter are “enabled”?

[privacy.twitter]
  disable = true

I.e., is it the same as below quoted text?

[privacy.twitter]
  enableDNT = true
  simple = true

My blog images gets rendered to /images/blog/2006/01/02/FILENAME.jpg. Can I somehow call them from my HTML files that I write in content/blog/ without entering the entire path? Would it be possible to create a shortcode that I could call from my blog content pages (say {{< logimg shoe.jpg >}}). As shown above the image/images for a post will be in a path that matches the page publish date that I’m calling it from. I made an attempt but ended up with errors.

Thanks for a great piece of software!

You can create your own shortcode, eg {{< now >}} that returns the now function

Have a look at Page Variables and File Variables.

uglyURLs

You may want to ask this question separately, unless you get others’ responses here. I am inclined to interpret it similarly, however I am neither a Hugo dev nor do I use twitter on my sites.

Should be doable. Easier to help you if you show what you’ve tried.

2 Likes

I created layouts/shortcodes/now.html and put {{ now.Format "2006"}} in it. I used {{< now >}} on content/apage.html and {{ now.Format "2006"}} got printed to the rendered page, unparsed.

Read through them both. Closest I can find is .File.TranslationBaseName, but I guess that would include the date (2006-01-02-tasty-carrots, not tasty-carrots)? Problem is I can’t use .File.TranslationBaseName to format my URLs under [permalinks] in config.toml?

Great, thanks!

OK, I’ll do.

This is somewhat uncomfortable. :blush: In layouts/shortcodes/logimg.html there’s <img src="/images/blog/{{ .PublishDate.Format "2006" }}/{{ .PublishDate.Format "02" }}/{{ .PublishDate.Format "01" }}/{{ .Get 0 }}">.

Error is:

execute of template failed: template: shortcodes/logimg.html:1:38: executing "shortcodes/logimg.html" at <.PublishDate.Format>: can't evaluate field PublishDate in type *hugolib.ShortcodeWithPage

Guess .PublishDate.Format isn’t welcome there.

Odd, this works for me. Can you share your site code so I can reproduce?

:thinking: You could try to regex your way out? Or if it is always in the form YYYY-MM-DD-lorem-ipsum you could slicestr the date away?

You need to get the page’s context first: try .Page.PublishDate.Format ...

(note: you may also need to pipe to safeHTMLAttrs or its other safeX friends)

For the posts in the blog section, it’s always the same. But how can I do slicestr in config.toml?

Sorry, works! I had forgotten <> in {{< now >}}.

Well, that is just awesome. Thank you! Seemed to work without safeHTMLAttrs . What would be the point of safeX in this context?

Ok, so I’m realising I probably didn’t understand the question:

  1. your content files’ filenames are in the form 2006-01-02-tasty-carrots.html
  2. and you wanted to use tasty-carrots in your templates (?)

was my current/initial understanding of the question.

On re-read:

#2 is: you want content/blog/2006-01-02-tasty-carrots.html to be rendered at yoursite.com/blog/tasty-carrots/ (?)

If so, AFAIK, Hugo treats the whole 2006-01-02-tasty-carrots as the whole slug, so will use it as is.

If you are able to, you could define the slug yourself in your content frontmatter:

slug: tasty-carrots

and Hugo will then use that value to generate your page at the url.


As for safeX-, sometimes Hugo sanitises urls and other attrs. Just keep it in mind if you ever encounter ZgotmplZ, this is where you start looking.

Sorry, I was not clear. I said I wanted to “access” tasty-carrots, but actually I wanted to fetch it for use in my URLs so that I could replicate what I had in Jekyll and not break the URLs.

Yes, correct. content/blog/2006-01-02-tasty-carrots.html

No, in my rendered post URL. Like so: domain.tld/blog/2016/01/02/tasty-carrots/ or domain.tld/blog/2016/01/02/tasty-carrots.html

From this point on, I guess I am. But for the posts that already exist I’d have to go in and edit Front Matter slug: manually. It’d be awesome if hugo import jekyll created a slug: in Front Matter from tasty-carrots in the Jekyll post filename. If I could dream a
slugFromFilename option in config.toml :filenameslug Permalink Configuration Value would be awesome :slight_smile: That way of creating slugs seems quite sane.

Great, thanks again for all the help!