# Code-block line numbers

**URL:** https://meta.discourse.org/t/code-block-line-numbers/330130
**Category:** Theme component
**Tags:** code
**Created:** [October 9, 2024, 5:45pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130 "2024-10-09T17:45:25Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 9, 2024, 5:45pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/1 "2024-10-09T17:45:25Z")

</div>

| | | |
| --- | --- | --- |
| ℹ | **Summary** | Adds line numbers to multi-line code-blocks in posts |
| 👓 | **Preview** | [Preview on Discourse Theme Creator](https://discourse.theme-creator.io/theme/Lilly/codeblock-line-numbering) |
| 🛠 | **Repository** | [https://github.com/Lillinator/code-block-line-numbers](https://github.com/Lillinator/code-block-line-numbers) |
| ❓ | **Install Guide** | [How to install a theme or theme component](https://meta.discourse.org/t/how-do-i-install-a-theme-or-theme-component/63682) |
| 📖 | **New to Discourse Themes?** | [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) |

Install this theme component

### Description

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).

Thanks to @Don for the great work on this too.

✅ Works in desktop and mobile views. 🖥 📱

Dark mode, with title:

 ![The image shows a screenshot of a code editor with a dark background, displaying code in three separate sections: CODE JS, CODE SQL, and CODE SCSS, each with a different programming language. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/c/e/8/ce8cdba152fe4bc3b4259ff529774a8868940441.png)

Light mode, without title - in previewer:

 ![The image shows a screenshot of a coding interface, likely for a programming language such as Python or Ruby. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/c/0/3/c03960f68ff2b5131b17641e97d5d2fc33f6b859.png)

Post result:

 ![The image shows a screenshot of a coding question, with two code blocks - one in Python and one in Ruby - that both print "Hello World", but neither will have line numbers. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/3/b/3/3b342d23a2b2f6ad6c5c379f1ccfe4ad20f0d667.png)

There are two settings.

| Setting | Description |
| --- | --- |
| `code-block header title` | adds a text header title to code blocks before the code language if it’s specified (leave blank for none) |
| `code-block text size` | text size in px for code blocks (leave blank for default) |

 ![The image shows a screenshot of a theme settings page with code-block header and text size options. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/f/d/0/fd03fdb5a72ea5e4c6d909b42ec16e2cc76e6a3b.png)

* * *

⚠ Note: if you are using CSS to wrap code-blocks on your forum, this component will count the wrap.

---

<div class="post-metadata">

### Author: ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)
#### Post date: [October 9, 2024, 5:58pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/2 "2024-10-09T17:58:42Z")

</div>

Without trying, but when copying, those line numbers don’t follow, right?

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 9, 2024, 5:59pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/3 "2024-10-09T17:59:54Z")

</div>

yea no they don’t work in quoted codeblocks.

---

<div class="post-metadata">

### Author: ![hoangphuctran93](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hoangphuctran93/32/290976_2.png) [@hoangphuctran93](https://meta.discourse.org/u/hoangphuctran93)
#### Post date: [October 13, 2024, 2:07pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/4 "2024-10-13T14:07:02Z")

</div>

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

 ![Current Status](https://global.discourse-cdn.com/meta/original/4X/2/d/2/2d2fceab11667b4a6c2a15aaae0a2a07b0520d3c.png)

* * *

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

 ![terminar like macos](https://global.discourse-cdn.com/meta/original/4X/c/4/0/c401c01942ffa7a2b7e8b0661655daab00b62b07.png)

```css
/*CSS Codeblock like terminar macos*/
.codeblock-buttons {
    box-shadow: 0px 0px 10px rgba(0,0,0,.4);
    border-radius: 7px 7px 0 0;
    display: flex;
    flex-direction: column;
    &:before{
        content:'Business Lab';
        display: grid;
        justify-content: center;
        font-family: var(--d-font-family--monospace);
        font-weight: bold;
        padding: 5px;
        background-color: #E8E6E8;
        color: black;
        order:1;
    }
    &:after{
        content:"";
        width: 12px;
        height: 12px;
        display: inline-block;
        border-radius: 15px;
        margin-left: 2px;
        border-width: 1px;
        border-style: solid;
        background: #EC6A5F;
        border-color: #D04E42;
        order:2;
        position: absolute;
        top: 10px;
        left: 5px;
    }
    & .codeblock-button-wrapper{
        right: .5rem !important;
        top: .25em !important;
        order:3;
        width: 100%;
        &:before{
            content:"";
            width: 12px;
            height: 12px;
            display: inline-block;
            border-radius: 15px;
            margin-left: 2px;
            border-width: 1px;
            border-style: solid;
            background: #F5C04F;
            border-color: #D6A13D;
            position: absolute;
            top: 6.5px;
            left: 30px;
        }
        &:after{
            content:"";
            width: 12px;
            height: 12px;
            display: inline-block;
            border-radius: 15px;
            margin-left: 2px;
            border-width: 1px;
            border-style: solid;
            background: #64CC57;
            border-color: #4EA73B;
            position: absolute;
            top: 6.5px;
            left: 48px;
        }
        & .btn.nohighlight{
            padding: .7em;
            float: right;
            &.copy-cmd{
                position: absolute;
                right: 0px;
            }
            &.fullscreen-cmd{
                position: absolute;
                right: 3.2em;
            }
            &.copy-cmd:not(.action-complete), &.fullscreen-cmd:not(.action-complete){
                opacity: 1;
                visibility: visible;
            }
        }
    }
    & code{
        order:4;
    }
}

```

```CSS
/*CSS Discourse Code Block Line Numbers*/
code{
    &.lines-decorator{
        padding: 0px;
        &.lines-shadow{
            background-image: linear-gradient(to right, var(--github) 25%, transparent), linear-gradient(to left, var(--github) 25%, transparent), linear-gradient(to right, var(--github), transparent), linear-gradient(to left, var(--github), transparent);
        }
        &.lines-number:not(.lines-count-1):not(.lines-in-quote) span.lines-line::before{
            background-color: #263238;
            color: rgba(233, 237, 237, 1);
        }
    }
    
}

```

You can refer to more [_here_](https://businesslab.vn/t/trinh-t-x-ly-logic-c-a-cau-l-nh-select-trong-sql/212).

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 13, 2024, 2:09pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/5 "2024-10-13T14:09:39Z")

</div>

> [@Codeblock Theme Picker](https://meta.discourse.org/t/codeblock-theme-picker/146396):
>
> discourse2Summary Codeblock Theme Picker will allow you to easily change the theme used in code blocks.eyeglassesPreview [Preview on Discourse Theme Creator](https://discourse.theme-creator.io/theme/Discourse/codeblock-theme-picker)hammer_and_wrenchRepository Link [https://github.com/discourse/hljs-theme-picker](https://github.com/discourse/hljs-theme-picker)open_bookNew to Discourse Themes? [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) Install this theme component Features This is just a handful of the available options. This component incl…

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [October 14, 2024, 11:19am UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/6 "2024-10-14T11:19:07Z")

</div>

Any chance of **this feature** being added to the discourse main line? The budget hosting plan I&nbsp;use does not support discretionary plugins and such. And this looks very useful. My thanks to @Lilly and other devs and testers.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [October 14, 2024, 11:25am UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/7 "2024-10-14T11:25:07Z")

</div>

It’s not a plugin. It’s a Theme Component. I don’t believe you are restricted from using Theme Components?

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [October 14, 2024, 11:42am UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/8 "2024-10-14T11:42:13Z")

</div>

@merefield Tx. I&nbsp;have just filed a request for clarification from my provider and been issued a ticket. If anything notable arises, I&nbsp;will report back.

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [October 14, 2024, 5:35pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/9 "2024-10-14T17:35:38Z")

</div>

This is indeed a matter for admins! I&nbsp;went to [https://meta.discourse.org/t/code-block-line-numbers/330130](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&nbsp;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.

Is there something else I need to do? TIA.

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 14, 2024, 5:39pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/10 "2024-10-14T17:39:15Z")

</div>

@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.

> [@Installing a theme or theme component](https://meta.discourse.org/t/installing-a-theme-or-theme-component/63682):
>
> bookmark This is a guide for installing and managing themes and theme components in Discourse. person_raising_hand Required user level: Administrator Installing and customizing themes can significantly improve the look and feel of your Discourse site. You can easily import and manage themes and their components through the Discourse admin panel. This guide covers: Importing new themes and theme components Adding theme components to a theme Managing automatic updates Additional resourc…

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [October 14, 2024, 6:01pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/12 "2024-10-14T18:01:01Z")

</div>

> [@robbie.morrison](#):
>
> Is there something else I need to do?

Yes. Add the Component to a Theme(s) (Otherwise it will remain “Unused”) and make the Theme active if it isn’t already.

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [October 14, 2024, 6:19pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/13 "2024-10-14T18:19:39Z")

</div>

@merefield @Lilly Many tx. I needed to add this theme component to my “default” theme. As covered here: [adding components to a theme](https://meta.discourse.org/t/installing-a-theme-or-theme-component/63682#adding-theme-components-to-a-theme-2). And it works (noting I had earlier added the wrong screenshot):

 ![The image is a screenshot of a computer interface showing line numbering. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/1/6/0/16080c5611d059a6123cfbbac48bd9cb414f3735.png)

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&nbsp;also have a small bug report for my next posting.

---

<div class="post-metadata">

### Author: ![robbie.morrison](https://avatars.discourse-cdn.com/v4/letter/r/c5a1d2/32.png) [@robbie.morrison](https://meta.discourse.org/u/robbie.morrison)
#### Post date: [October 14, 2024, 6:30pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/14 "2024-10-14T18:30:35Z")

</div>

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 image is a screenshot of a GitHub repository, showing a message about implementing a Sankey plotting of oemof results in a long-time simulation. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/8/d/0/8d04f2c413658aebd2d6651716c553fedb296592.png)

The original posting is [here](https://forum.openmod.org/t/tracking-the-source-of-allocated-energy-flows-in-energy-systems/4860/4). And the information came in via a simple URL. More on [Sankey diagrams](https://en.wikipedia.org/wiki/Sankey_diagram) on Wikipedia&nbsp;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.

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 14, 2024, 6:52pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/15 "2024-10-14T18:52:28Z")

</div>

ah yes the github line number issue. I’ll have a look later this week when I get some time and put a conditional in there. thanks!

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [November 3, 2024, 8:55pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/16 "2024-11-03T20:55:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [November 9, 2024, 8:29pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/19 "2024-11-09T20:29:19Z")

</div>

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 🤗).

Updated OP description and screenshots.

---

<div class="post-metadata">

### Author: ![Yt.w](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yt.w/32/348367_2.png) [@Yt.w](https://meta.discourse.org/u/Yt.w)
#### Post date: [March 1, 2025, 7:21pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/20 "2025-03-01T19:21:32Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/7/f/6/7f6979bda08465da5c6cf408f9d46c32179589f2.png)

Hello, This theme component report me error like this

```plaintext
Error: Undefined mixin. ╷ 17 │ @include sticky; │ ^^^^^^^^^^^^^^^ ╵ line-numbers.scss 17:9 @import /var/www/discourse/common.scss 1:570 root stylesheet

```

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [March 1, 2025, 7:24pm UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/21 "2025-03-01T19:24:19Z")

</div>

`sticky` mixin has been removed from core in a recent PR.  
I will make a PR!

* * *

There we go:  
[https://github.com/Lillinator/code-block-line-numbers/pull/17](https://github.com/Lillinator/code-block-line-numbers/pull/17)

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [March 3, 2025, 6:41am UTC](https://meta.discourse.org/t/code-block-line-numbers/330130/22 "2025-03-03T06:41:00Z")

</div>

merged. thanks @Arkshine 🙂
