mkdocs: Indented code block not transformed

I want to place a code block in a numbered list. Stack Overflow has the answer, but when I indent the backticks, they’re not picked up by the parser, and are simply rendered as text. If the backticks are not indented, the numbering restarts.

The following works on GitHub but not MkDocs.

  1. Foo

    def hello_world():
        pass
        more
    
  2. Bar

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

So to build on/clarify @geerlingguy’s solution (which is what worked for me with mkdocs 0.17.3):

  • Don’t use backticks (mkdocs only supports this for non-indented code blocks)
  • Your code block must have blank lines above and below the code block
  • For a 1st level list, indent your code block 8 spaces
  • For a 2nd level list, indent your code block 12 spaces
  • For N level list, indent your code block (4 + N x 4) spaces

Here’s a example (I’ve replaced spaces with periods to make it clear)

1. Level one

........code
........block

1. Level one
    1. Level two

............code
............block

    1. Level two

You could use this: https://facelessuser.github.io/pymdown-extensions/extensions/superfences/.

It supports indented, fenced code blocks in a fairly same manner.

It’s been mentioned in this thread that https://facelessuser.github.io/pymdown-extensions/extensions/superfences/ allows for indented fenced code. This would be the better way.

After all this time, is there a better way to handle this? This seems to ignore blocks above and lose the syntax coloring.

@jsaintro While this solution does allow for indenting and continued numbering to occur you are not able to set the code style.

(IIRC, the whole three-backtick thing is a GitHub-specific implementation, and though it’s been picked up by Slack and a few other entities, it’s nowhere near universal, so I shy away from using it if at all possible.)

I think it originated even before Github, maybe I am wrong, but I don’t see why the three bactics is a problem. It doesn’t matter if it isn’t universal unless your docs have to be parsed by everything universally. My MkDoc docs are used just for MkDocs, so having a syntax that is Python Markdown specific isn’t a problem for me. I’ve personally found backtics to be easier to visualize in lists for me.

With that said, I did write a Python Markdown extension to allow triple backtic code blocks to work in lists properly: http://facelessuser.github.io/pymdown-extensions/extensions/superfences/. Now I realize it doesn’t come as a standalone extension, but as a group of extensions, but you can enable only what you want. I also realize some people just prefer using white space, or maybe they parse the same document with multiple parsers for different reasons, and I understand that as well. If people are looking for workarounds, maybe this will be a suitable one for some.

So… if you just want to show a code block inside a list, you have to get the right number of spaces, with a space in between the previous line as well, for example:

https://github.com/geerlingguy/drupal-vm/commit/2152f645186b22277add0a7e99e892a5c0a79203

Which renders as:

http://docs.drupalvm.com/en/latest/extras/behat/#setting-up-behat-for-your-project

(see https://github.com/geerlingguy/drupal-vm/issues/280)