How to add line numbers to code blocks?

I found this post from 2021 Is it possible to show line numbers in code block? but no idea how to apply it.

  • Is this option built into Discourse? If so, how do I specify this in Markdown?
  • If it is not built into Discourse but is a plugin, where is the information on this plugin?
2 Likes

I asked the AI, I’m not sure if this will help:

To solve the problem of showing line numbers in a code block on Discourse, here’s what you need to do:

Discourse does not have a built-in feature to display line numbers in code blocks. However, there is a plugin that can help you achieve this.

The plugin is called “Code Review” (formerly known as “Code Line Numbers”). To use this plugin, you’ll need to:

  1. Check if the plugin is already installed: Reach out to your Discourse administrator or check the admin panel to see if the Code Review plugin is already installed on your Discourse instance.
  2. Install the plugin: If the plugin is not installed, ask your administrator to install it from the Discourse plugin repository. Alternatively, if you have access to the admin panel, you can install it yourself.
  3. Enable line numbers: Once the plugin is installed, you’ll need to enable line numbers in your code blocks. You can do this by adding the line_numbers attribute to your code block Markdown. For example:
```python line_numbers
def hello_world():
    print("Hello, World!")

This will render the code block with line numbers.

Note: The line_numbers attribute may have been renamed or modified in newer versions of the plugin. If you’re unsure, check the plugin documentation or consult with your Discourse administrator.

If you’re still having trouble, feel free to ask, and I’ll do my best to help!

3 Likes

That Topic contains the CSS they used which you could experiment with in your own local Component.

See from this post onwards

Shame no one has packaged that into a Theme Component on GitHub for easier consumption?

3 Likes

Did you try it? It’s best I think to check AI provided solutions before posting as gospel.

2 Likes

I stated that I asked the AI, because it seems to be very helpful and underused in the case of helping. Though I did not have much knowledge on this plugin, I still wanted to help.

AI can hallucinate and old information it might have relied upon can get out of date and lose relevance - hence worth checking.

4 Likes

Which AI? (gratuitous content to meet the 10-character minimum)

1 Like

image

Ah, ok… everyone is jumping on the AI bandwagon I guess :confused:

1 Like

Edit: ok I whipped together a quick theme component. I will add some settings later to control background and text colours.

10 Likes

3 posts were merged into an existing topic: Code-block line numbers

A post was merged into an existing topic: Code-block line numbers