Search code examples
markdownmkdocspython-markdown

Enable line numbers for specific markdown code listings designated with backticks


I am using MkDocs with the codehilite markdown extension

I would like to enable code snippet line numbers only for specific snippets.

If I set

markdown_extensions:
  - codehilite:
      linenums: true

in my mkdocs.yml, this will enable line numbers for all code snippets.

I see that it is possible to activate line numbers for specific snippets by using the shebang language specifier together with double indentation:

#!python
""" Bubble sort """
def bubble_sort(items):
    for i in range(len(items)):
        for j in range(len(items) - 1 - i):
            if items[j] > items[j + 1]:
                items[j], items[j + 1] = items[j + 1], items[j]

However, I prefer using backticks (```) for designating code.

Is there a way to enable line numbers for specific code listings when using backticks?


Solution

  • No, this feature is not supported by the fenced code blocks extension of Python-Markdown. Only the global linenums setting of the codehilite extension is used for fenced code blocks.

    Of course, you always could fork the extension and alter the behavior to match your needs, so long as you do so within the confines of the relevant license.