.Summary vs. code highlighting

Hi there, my name is Karen, Who do I speak to about this issue here?

I just ran into an understandable, but annoying issue. I have a post that has code highlighting within the (let’s call it) summary section.

Meaning:

The post:

---
title: "Hugo Shortcode: Taglist"
description: ""
date: 2022-03-01T21:18:21+07:00
publishDate: 2022-03-01T21:18:21+07:00
lastmod: 2022-03-01T21:18:21+07:00
resources:
  - title: "Photo by [Joanna Kosinska](https://unsplash.com/@joannakosinska) via [Unsplash](https://unsplash.com/)"
    src: "joanna-kosinska-1_CMoFsPfso-unsplash.jpg"
tags:
  - gohugo
  - shortcode
---

I needed a Hugo shortcode to show a list of posts that belonged to a [certain](/tags/laboratory/) tag, which is quite easy. Add the following to `layouts/shortcodes/taglist.html` or whatever name you like to use for the shortcode.

```gohtml
{{- $title := $.Params.title -}}
{{- $tag := $.Params.tag -}}
{{- $limit := int ($.Params.limit) -}}
{{- $tags := (index site.Taxonomies.tags $tag) -}}

{{- if (eq $limit -1) -}}
  {{- $limit = len site.Taxonomies.tags -}}
{{- end -}}

<h3>{{- $title| default "Related Posts" -}}</h3>
<ul>
{{- range (first $limit $tags.Pages) -}}
  <li>
    <a href="{{- .Permalink -}}">{{- .Title -}}</a>
  </li>
{{- end -}}
</ul>
```

And the .Summary creates this:

I needed a Hugo shortcode to show a list of posts that belonged to a certain tag, which is quite easy. Add the following to layouts/shortcodes/taglist.html or whatever name you like to use for the shortcode. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {{- $title := $.

Problems with this:

  • it hacks off in the middle of a code block line
  • it shows the line numbers in a list, due to how the highlighting is done. (linenos=true)

I would expect some form of filter for the summary that either ignores the code block or maybe limits the summary to full lines of code and works around the line numbers.

I know I could just add a summary to my frontmatter (and I will), but let’s just assume this is part of a theme that normal users use. What would be the best approach to a “bulletproof summary” for any theme built from the content without specific front matter?

Is there any way to have some form of AST structure of the content that a theme developer could use to parse content somehow and remove unwanted block-level elements? That is probably a bigger feature, just asking because that would be the way to approach the issue in an HTML, JS, XML, PHP, DOM way. I wonder if that type of information is available somewhere deep inside Hugo and could be brought into the page variables.

1 Like

<!--more--> :sunglasses:


If we mean: left to the templates only, no “user interaction” (content edits), I’ve seen over the years folks create conditionals for their summaries, and I believe one of them even used findRE | Hugo to check if certain strings were present, which seems like something you might check for depending on how the code block is formatted (I’d probably check for <pre> and <code>).

:thinking: