`anchorlinenos=true` attribute doesn't work when using code fences

Hello,

I am working on converting all of my code blocks to use code fences instead of the {{< highlight >}} shortcode now that it has been supporting attributes like linenos for a while.

As I was testing out this conversion, I realized that the anchorlinenos=true works in the highlight shortcode but not in the code fences.

To help quickly verify if this is an issue in my usage of this attribute or my site config, I have created a minimum working example: Anchor Line Numbers | Hugo MWE

Markdown source of that page

That page highlights the issue and also presents few git commands at the bottom so that anyone can reproduce this issue locally.

I did some snooping around in the Hugo code… It looks like this needs to include the anchorlinenos and lineanchors special attributes:


From that test page’s HTML source:


I believe I can open an issue?

Opened

There are three ways to apply syntax highlighting:

  1. Within a template or shortcode use the transform.Highlight function.
  2. Within markdown use the highlight shortcode, which calls transform.Highlight.
  3. Within markdown use code fences.

When using #1 or #2, you may specify any of these options.

When using #3, the available options are limited by the yuin/goldmark-highlighting module:

Option Values
linenos true, false, inline, table
linenostart integer
hl_lines array
hl_style string (see this list of available styles)

For example:

```bash {linenos=table linenostart=4 hl_lines=["4-6",9] hl_style=emacs}
#!/usr/bin/env bash

mul() {
  foo=$1
  bar=$2
  echo $((foo * bar))
}

mul 6 7
```

image

1 Like

@jmooring Thanks for that detailed reply!

I was reading Syntax Highlighting | Hugo and I saw

The options are the same as in the highlighting shortcode,including linenos=false , but note the slightly different Markdown attribute syntax.

So that doc should be updated?

Yes, I’ve got a low priority reminder to look at this. In addition to a limited option set when using code fences, there are also syntactical differences between the three methods which require correction and clarification.

The yuin/goldmark-highlighting module also provides a nohl option. I didn’t inlcude it in the list above because the behavior was unexpected (or because I don’t understand the intent).

Yeah, I realized that when adding hl_lines support for code fences to ox-hugo. Other than that, using comma or space to separate the attributes in code fences doesn’t seem to make a difference.

Yup.

I also experienced a self-inflicted wound when trying to use mixed-case option names as documented here.

This issue is now fixed in the master branch as of

So Hugo v0.93.0 will have this fix.

1 Like

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