Is it possible to use attribute lists with render hooks?

Let’s take headings for instance.

## A Heading {.text-serif #a-head title=“Look”}

This would make a heading with attributes class text-serif, id a-head and title Look.

Is it possible to include these in the heading render hooks?

you’ll have to check on how this interfaces with render hooks

Currently no (I don’t think so), but it’s a great idea.

Could you create an issue on GitHub about it?

As I see this should not be hard to do, but we need to expand the render hook API to pass down the .Attributes.

Done

1 Like

Just a quick follow-up, I’d like to do the same thing with render hooks for images, and it looks like it should be possible, but I’m kinda bunging the syntax.

Could someone check me on if this is possible?

In the markdown:

![alt_text](VulnerabilitytoClimate.png "image_tooltip")
{.image-left}text comes here

In render-image.html:

{{ $attrs = .Attributes }}
<img {{ if isset "class" $attrs }}class="$attrs.class"{{ end }} src=....

I’m currently getting an error on $attrs = .Attributes saying .Attributes doesn’t exist, so clearly I’m not accessing it, when it probably should be an empty map even if I screwed up the syntax for the image?

execute of template failed: template: _default/_markup/render-image.html:5:16: executing “_default/_markup/render-image.html” at <.Attributes>: can’t evaluate field Attributes in type goldmark.linkContext

No special Goldmark extensions enabled in config.toml

It is not. Attributes can only be specified for block elements. Comparing the three render hooks:

Render Hook Element Type Attribute Support
render-link.html <a> inline No
render-image.html <img> inline No
render-heading.html <h1-6> block Yes

See comments:
https://github.com/gohugoio/hugo/pull/8271

The attributes would apply to the paragraph since images are wrapped in them, so you’ll need to have something like .attribute img {} in CSS.

Another way would be to add the attributes in the resource params.

@jmooring is right, and that is unfortunate.

See Transfer paragraph attributes to image when used as a block · Issue #8362 · gohugoio/hugo · GitHub

I know others have done similar things. I have some stupid, fragile render hook code that allows you to misuse the image title attribute.

This markdown:

![Alternate text](a.jpg "Title text {id='foo' class='bar'}")

![](a.jpg "{style='border: 1px solid #ff0000'}")

Produces this HTML:

<p><img src="a.jpg" class="bar" id="foo" alt="Alternate text" title="Title text"></p>
<p><img src="a.jpg" style="border: 1px solid #ff0000"></p>

I can post if you’re interested.

2 Likes

Just realized that i did, too. but haven’t been using it.

Thanks folks, that’s unfortunately but misusing title is a cute idea (if horribly nonstandard). Thanks for the snippet Henry!

There is a lot of discussion on this here (see below). Of course, there doesn’t seem to be any consensus.

I think the Markdown attribute situation in Hugo is decent. It’s unfortunate that CommonMark cannot make up its mind, but it gets a little stupid to wait 20 years for them.

3 Likes

Can we please have this table in the official documentation?

I was struggling to understand why the Attribute support that is mentions to be supported also for links was not working.

It would be fantastic all the render-hooks to behave consistently, and I don’t see what is the problem is allowing people specifying theirs.

1 Like

If you feel strongly about it, please open an issue in the docs repository.

@jmooring

I’m not sure I’m strong enough to take this onboard. I’m still struggling to wrestle with the basic, and there is a high risk to make more mess than benefit ats.