Rendering code blocks properly from .md files

This is a bug or its a feature?

When having a code block after a list, no matter how many new lines, the code block is rendered inside the list. Even if you have a list, after that a title, after that a code block. In this last case, both, title and code block are rendered inside the list.

I expect the example 1, the code to be rendered in the list, but not example 2 and example 3. Any other render works fine except what hugo uses.

The only way to break this circle is to put a text like that is going to be rendered as p between the list and the next stuff.

I can replicate example 1 and 2 but not 3 when using Blackfriday. I believe this is a bug, https://github.com/russross/blackfriday/issues.

I also tested with Mmark (also built in to Hugo) and then all three examples works as they should. So my suggestion is to switch to Mmark, I believe it’s better maintained.

Is there a way to set mmark as default render in hugo without having to set markup = "mmark" in front matter or changing the .md files to .mmark?

You could run this script from the root of your content dir. It will recursively rename all *.md files to *.mmark.

If you don’t have your site in a git repo, then definitely take a backup before doing this.

#!/bin/bash

ext_old="md"
ext_new="mmark"

files="$(find . -type f -name "*.$ext_old")"

for file in $files; do
  basename="$(basename "$file")"
  filename="${basename%.*}"
  dirname="$(dirname "$file")"
  echo mv "$file" "$dirname"/"$filename"."$ext_new"
  mv "$file" "$dirname"/"$filename"."$ext_new"
done
1 Like

@freethebit I cannot recreate the issue you are seeing.

Actually let me take that back…

I see the issue with:

-   list item 1
-   list item 2

```
echo "hello"
```

-   another list item 1
-   another list item 2

But not with:

-   list item 1
-   list item 2

```text
echo "hello"
```

-   another list item 1
-   another list item 2

Notice the ```text :smile:.

Thanks for the reply, but this is not a option for me. I’m looking for a static site generator that use something that properly render markdown .md files and default syntax and the content files a wide compatible with other tools. Since there is no global setup for this, I’m moving on my actual project to another static site generator.

Speed is good as long it properly render the content.

And here’s another workaround:

-   list item 1
-   list item 2

<!--list-end-->

```
echo "hello"
```

(I happen to know these shortcomings of Blackfriday as I have written an Emacs Org mode to Blackfriday compatible Markdown generator :smile:.)

1 Like

My opinion on this: its ugly, I don’t use shortcodes for a reason, mix stuff that is not content to content. If I need to use shortcode, I do not use.

I am in the same camp.

I’m going to play with 11ty today and see how it goes. The sad part, is that I only notice it after developed a theme for hugo and made the setup CI / CD on gitlab.

Good luck!

Here’s what’s keeping me with Hugo:

  • While the Blackfriday dependency sucks, the workarounds are not bad (there’s a glimmer of hope that Hugo will switch to the commonmark Go library at some point).
  • Speed!
  • Static binary (I don’t need Ruby, node.js, etc. dependencies)
  • Apart from Markdown parsing, there are many things that make up a static site generator and Hugo is great in all those areas… few I can think of: the bundle system, resources, data files, theme inheritance, etc.
  • COMMUNITY! (this forum right here)
2 Likes

No if statement to check for the MD file extension in the event the OP is including images or other files inside the content dir?

Hi @rdwatters. If I’m understanding your question correctly, then this line from the above only returns the files with the relevant extension.

files="$(find . -type f -name "*.$ext_old")"
2 Likes

Could you elaborate a bit on this? Or provide a link to discussion on this topic?
I am very curious about the reasoning for not using shortcodes. Thanks!

Thanks for creating a separate thread.

Oops. You abstracted that part too, which is awesome. Sorry for the sloppy feedback, @zwbetz, and thank you for sharing; I’ll likely be using this sooner rather than later😊

1 Like

After testing everything that I could the only option acceptable is what you showed:

```text
echo "hello"
```

Specifying the code language in every single block.

Is this a case to have in the documentation a explicit note about the need to specify the programming language when using code block?

It helps in one way but mess up the hole thing,

- Item 1

- Item 2

    ```zzz
    code
    ```

- Item 3

It make ‘item 3’ as sub level of ‘item 2’. The expected result was the code to be inside ‘item 2’.

Without the ‘zzz’ the thing keep it well formatted.

1 Like

You could try the option pygmentsCodeFencesGuessSyntax: Configure Hugo | Hugo