Custom Front Matter

Oh. my. goodness. I have been at this for hours, but I think I’ve finally got it.

Your code didn’t work - it kept sticking in weird extra quote marks and <p> elements and was just generally being disagreeable.

I kept digging through docs, figuring I might need to build the whole string before sticking it in the “content_html” line.

String concatenation in Hugo, is, uh, pretty arcane, as best as I can see. So this is the final result. It lives in the index.json template, and for truncated posts (those with a <!--more--> tag), it shows the summary/excerpt and then a link to read more. For other posts, it follows the standard rules.

{{- if .Truncated -}}
  {{- $tt := .Title -}}
  {{- $sm := .Summary -}}
  {{- $lk := .Permalink -}}
  {{- $lkb := " Read more: <a href='" -}}
  {{- $lke := "' >" -}}
  {{- $lkc := "</a> " -}}
  {{- $tp := printf "%s" $lkc | printf "%s%s" $tt | printf "%s%s" $lke | printf "%s%s" $lk | printf "%s%s" $lkb | printf "%s%s" $sm | printf "%s" -}}
  {{- $tp := $tp | jsonify -}}
  {{- $tp := replace $tp "\\u003c" "<" -}}
  {{- $tp := replace $tp "\\u003e" ">" -}}
  {{- $tp := replace $tp "\\u0026" "&" }}
  "content_html": {{ $tp }},
{{- else -}}
  {{- $tp := .Content -}}
  {{- $tp := $tp | jsonify -}}
  {{- $tp := replace $tp "\\u003c" "<" -}}
  {{- $tp := replace $tp "\\u003e" ">" -}}
  {{- $tp := replace $tp "\\u0026" "&" }}
  "content_html": {{ $tp }},
{{- end -}}

I’m sure there’s a more elegant way to do this, but outside of the crazy printf incantation, it’s at least fairly clear what it’s doing.

1 Like

Awesome work! Sorry about my code snippet; I should have tested it before posting. :sweat_smile: And, yep, Hugo certainly has its quirks. In my world, there’s no right or wrong way to express the templates. If it works, it works.

That said, here’s an alternative take that should be functionally equal to your template. And actually tested this time around. :blush: Instead of the if/else statement, this snippet uses Hugo’s cond. That first line says, “If .Truncated is true, evaluate this printf stuff and put the result into $s. If not, just put .Content into $s.”

{{- $s := cond .Truncated (printf "%s Read more: <a href=\"%s\">%s</a>" .Summary  .Permalink .Title) .Content -}}
{{- $s := $s | jsonify -}}
{{- $s := replace $s "\\u003c" "<" -}}
{{- $s := replace $s "\\u003e" ">" -}}
{{- $s := replace $s "\\u0026" "&" }}
"content_html": {{ $s }},
2 Likes

@laura May I steal this and test on my blog?

Have at it! :sunglasses:

I’ve had to change the code I posted earlier for displaying the post without the summary in the article.html template. Try this:

<div class="post-content">
{{ if in .Content "--excerpt--" }}
	{{ $body := replace .Content .Summary "" }}
	{{ $body | safeHTML }}
{{ else }}
	{{ .Content }}
{{ end }}
</div>

And when you write the summary, you should end it with <!--excerpt--><!--more-->.

This seems to be working - that is, it shows just the body without the summary if you’ve included the <!--excerpt--> tag, and if it’s a longer post without the <!--excerpt--> tag, it shows the whole post.

I’m using @sod’s code above in the index.json file, and so far, it seems to be working. Just remember to remove the {{ .Title }} part of the index.json file as well as the replacing the {{ .Content }} part with the code.

Good luck!

1 Like

Thanks so much for this - much cleaner, and I’ve learned a new way to do conditionals in Hugo. Win win!

1 Like

Just wanted to add one comment here on excerpts… For some reason Hugo strips out the <!--more--> tag when the post gets back into the XML / JSON Feed. It doesn’t strip other HTML comments. This matters because we use the feed to populate the timeline, so without the text cut marker, Micro.blog doesn’t know this post has an excerpt.

There are work-arounds, of course. We can pull up the original Markdown for the post and use that to determine the excerpt.

This comes up enough that I would like to solve it. Personally, I don’t think the timeline should be full of summaries, but automatic truncates aren’t good either.

Thanks y’all.

1 Like

Automatic truncates are worse than summaries. But if there are no excerpts, you incentivize people to have click-bait titles.

3 Likes

@pratik @manton Second this, and I’m still of the opinion that a summary serves as an excellent synopsis of a post—a tl;dr if you will. It allows the reader to determine if the post would interest them or not. Obviously this is dependent upon an accurate summary being included, but when done properly they provide valuable context.

Yup, I should clarify that we shouldn’t force people to add a summary. For those who don’t, it can be automatic truncates.

1 Like

Oh yes, definitely not required. :+1:t2:

Why is this important?

BTW, I tried your code. The summary didn’t display on the blog, but on the timeline, the summary (< 300 chars) was followed by the first few words of the rest of the post.

It’s important because of this:


from this page: ℹ️ Timeline display rules

If you don’t remove the title element from the json file, micro.blog will display the post this way (title plus link, no summary).

As for why my code isn’t working for you, I can’t say. Maybe play with it a bit? Maybe you’re writing longer summaries than I am, and something needs to be tweaked? I don’t really know - I guess just check whatever you’re doing against the timeline display rules above to make sure your summaries aren’t getting caught in one of those conditions unintentionally.

Oh yes. That must be it (the title rule). Or it may be the thing that Manton mentioned. BTW you mention to edit the .json file. Is that the feed file? I was simply using your code in my index.html file

Woo hoo! After some trial-n-error, it worked! I realized :man_facepalming:t4: I had to change article.html andindex.json but I had to remove {{ .Title }} only in the index.json file.

BTW I had to also replace {{ .Content }} in the single.html otherwise the excerpt would show up there. I struggle a bit there coz I’ve two versions of the single.html file (a separate one for the ‘Now’ page and I was changing it only the Now version LOL.

Thanks to @sod too.

@pratik @laura Wouldn’t removing the title from the json file also remove it from the RSS feed, or is that something different?

That’s something I’m tracking now. Also for some reason that post didn’t cross-post to Mastodon. Another one that I wrote later (< 300 chars and no title) cross-posted just fine. I plan to make another macro post with a custom summary later tomorrow to confirm the cross-posting issue and also how it looks in a feed reader (Micro.blog timeline is effectively a feed reader, no?)

I tested your feed a short time ago by adding it to my Feedbin account, and the title displayed on your test post. I can check again when you add another one though.

1 Like

There are two different templates, one for JSON, one for RSS. I’ve edited the json one because that’s what micro.blog is pulling from to display my posts in the timeline.

But yes, I have taken the title field out - though I have added it back in the body of post - and if anyone subscribes to the json version, they won’t have a separate title field. But if they subscribe to the RSS version, they will have a title field, because I left that template alone.

I should probably just write up all the working parts to this scheme and put it in a separate post, so hopefully it makes more sense than this long and winding thread.

3 Likes

I have been using this for a couple of weeks now and have used it on a number of macro posts. It works wonderfully. However, one tiny quibble. I can no longer use the <!--more--> in the middle of the post so as to limit showing the entire post on my blog’s front page. Am I doing anything wrong?

@laura This customizing is working great for most part but Micro.blog allows posts with a blockquote that are under 600 characters to be displayed in full on the timeline. However, I guess me editing my feed to allow for this <!--excerpt--><!--more--> usage is not allowing me that exception. Any way I can have best of both worlds?