HUGO

Unless content contains <h1>

Is there a code to add h1 or something similar if it is not there like the code for Jekyll

{% unless content contains '<h1>' %}
<h1>{{ page.title }}</h1>
{% endunless %}

If there is no h1, it will add an h1 containing the page title.

I … guess:

{{ if not (strings.Contains .Content "<h1>") }}
{{ end }}

do not work
I want code to reduce the CSS/JS codes, for example, if there is none
<button> ... </button>
It removes all CSS/JS button codecs

Did it not tell you if there was a h1 element in that content? Did you get an error message?

When used
{{ if not (strings.Contains .Content "<h1>") }}<h1>Experiment</h1>{{ end }}
I didn’t get an error message and adding another h1

And when used
{{ if (strings.Contains .Content "<h1>") }}<h1>Experiment</h1>{{ end }}
I didn’t get an error message and didn’t add h1 even on pages without h1

If you create this markdown:

+++
title = "Test"
date = 2021-01-01T00:00:00-00:00
draft = false
+++

# My Heading

then .Content will not contain <h1>.

Instead, it will contain <h1 id="my-heading">

Maybe search for <h1 or </h1> instead.

I tried that and it didn’t work
I wrote h1 as an example but I want something to remove unused tokens

The site will be very fast when using a code like this

{% less content contains '<button' %}
button{...}
{% endless %}

What didn’t work? Please be specific.

This method did not work

{{ if not (strings.Contains .Content "<h1>") }}...{{ end }}

It does not work and does not show an error message

We are not communicating well. Let’s try again…

This markdown:

+++
title = "Test"
date = 2021-01-01T00:00:00-00:00
draft = false
+++
# My Heading

Produces this .Content:

<h1 id="my-heading">My Heading</h1>

And this template code:

{{ if not (strings.Contains .Content "<h1>") }}
  Content does not contain &lt;h1&gt;
{{ end }}

Displays this in the browser:

Content does not contain <h1>

Which is correct.

The problem is that this code is not working for me

used

{{ if not (strings.Contains .Content "<h1>") }}
  Content does not contain &lt;h1&gt;
{{ end }}

and

{{ if not (strings.Contains .Content "<h1") }}
  Content does not contain &lt;h1&gt;
{{ end }}

On a page containing h1, it appears Content does not contain &lt;h1&gt;

hugo version 0.83.1 Host on Cloudflare Pages

You will need to share your repository so that we can see your code. Thanks.

Page containing h1

A page that does not contain h1

.Content contains the portion of the markdown files after the front matter.

None of your content pages contains a level 1 header. They contain level 2 (##) and level 3 (###).

Sorry to waste your time I thought it works with h1 found in profile.html

Is there a code that .Content contains the portion of the html files

No, there is not.

EDIT: With one exception, shortcodes. If your markdown file contains a shortcode, the rendered shortcode will be part of .Content.

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