This is a simple theme component that adds line numbers to code blocks in Discourse posts. It will not add line numbers for inline code, blocks of less than 2 lines, or to quoted code blocks. It also ignores Github onebox code. As well, the component will allow you to change the font size and add a header title that will appear before the code language (if it is specified after the first code fence).
Please add the option to choose dark or light theme, because my discussion page defaults to light theme but in the code block it is dark theme customization in github style.
This is the state when the discourse page uses a light theme and the codeblock is set to display in dark github style
Here I also share some customizations about codeblock display on my discourse page.
Hope it will be useful to someone, and make their discourse page more interesting
Any chance of this feature being added to the discourse main line? The budget hosting plan I use does not support discretionary plugins and such. And this looks very useful. My thanks to @Lilly and other devs and testers.
@merefield Tx. I have just filed a request for clarification from my provider and been issued a ticket. If anything notable arises, I will report back.
This is indeed a matter for admins! I went to https://meta.discourse.org/t/code-block-line-numbers/330130, clicked the blue “Install this theme component” button, entered the name of my forum, and then confirmed that action from my forum interface. So far so good.
This feature was then listed as a Component (I guess that is technically a Theme Component) and reports version 0.0.2. But the dialog suggests that this artifact is currently considered an “Unused component”. And simple trials using the three ``` back‑ticks syntax did not render line numbers.
@robbie.morrison please read the topic post (first post in this topic) because it contains all the information you need, and also refer to these instructions if you are installing theme components on your instance.
@merefield@Lilly Many tx. I needed to add this theme component to my “default” theme. As covered here: adding components to a theme. And it works (noting I had earlier added the wrong screenshot):
A couple of comments about the documentation. The correct location is, I believe: Components ▸ Customise ▸ Themes. And the section on adding to themes (see URL above) sounds somewhat more discretionary than it is. Perhaps this wording would be better: "You will need to explicitly add your theme competent to a theme before it will become operational. That includes the “default” theme.
Thanks to everyone for their interest and time. I also have a small bug report for my next posting.
My bug report as promised. The following code block from GitHub was already line numbered. After installing the theme component titled Discourse Code Block Line Numbers, the alternating blank lines were included.
The original posting is here. And the information came in via a simple URL. More on Sankey diagrams on Wikipedia EN for those interested!
Clarification; by alternating blank lines I mean the fully blank lines without any text whatsoever. The earlier rendering was not spaced out in this manner.
I made some improvements to this component and also added a couple of settings for adding a header title to code blocks and changing the font size. Fixed the Github onebox formatting issues too (thanks for the PR @gormus! ). Updated OP and added new screenshots and a link to theme creator preview.
This component has been updated again - I fixed a bug where the title was showing on Mermaid diagrams.
Also, @Don did a bit of a refactor of the js, cleaned some code, and also added nicer formatting, including a border and the code language in the header if it is specified in the post. (Thanks Don ).