Add labels to code blocks?

Is there a way to add a title/label to code blocks? Similarly, is there a way to reference a code block in text.

For example, I would like a piece of code to have a label that says “Listing 1: Example code” and then in the text I can refer to “Listing 1” when talking about it. Ideally, the numbering of code blocks would be automatically set by Zola, not manually by me.
If you are familiar with LaTeX, this is a supported feature of the listings` package.

Thanks in advance for your help!

This unmerged (but approved) PR adds that feature: Add path annotation for codeblock by izissise · Pull Request #2610 · getzola/zola · GitHub

I don’t think there’s a native way to achieve this.

Thanks! I also had the same question for images/figures. Is there a built-in way to add a label/caption to them?

If not, I will open issue(s) on GitHub and take a stab at it sometime…

Here’s three ways:

  1. Write the HTML manually (Mozilla docs).

  2. See this reply for a simple shortcode to add figures with caption.

  3. A quick search led me to this shortcode to insert a figure + citation within your markdown. I have not tested it.