npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rehype-highlight-code-lines

v1.1.2

Published

Rehype plugin to add line numbers to code blocks and allow highlighting of desired code lines

Downloads

4,378

Readme

rehype-highlight-code-lines

npm version npm downloads publish to npm code-coverage type-coverage typescript license

This package is a unified (rehype) plugin that wraps each line of code in a container, enabling code block numbering and line highlighting.

unified is a project that transforms content with abstract syntax trees (ASTs) using the new parser micromark. remark adds support for markdown to unified. mdast is the Markdown Abstract Syntax Tree (AST) which is a specification for representing markdown in a syntax tree. rehype is a tool that transforms HTML with plugins. hast stands for HTML Abstract Syntax Tree (HAST) that rehype uses.

This plugin enables line numbering for code blocks and highlights specific lines as needed.

When should I use this?

The rehype-highlight-code-lines is ideal for adding line numbers to code blocks and highlighting specific lines.

The rehype-highlight-code-lines is NOT code highlighter and does NOT provide code highlighting! You can use a code highlighter for example rehype-highlight to highlight the code, then use the rehype-highlight-code-lines after.

[!IMPORTANT] If the code highlighter already provides numbering and highlighting code lines, don't use rehype-highlight-code-lines!

You can use rehype-highlight-code-lines even without a code highlighter.

Installation

This package is suitable for ESM only. In Node.js (version 16+), install with npm:

npm install rehype-highlight-code-lines

or

yarn add rehype-highlight-code-lines

Usage

In a code fence, right after the language of the code block:

  • Use curly braces {} to specify a range of line numbers to highlight specific lines.
  • Add showLineNumbers to enable line numbering.

```[language] {2,4-6} showLineNumbers

```[language] showLineNumbers {2}

```[language] {1-3}

```[language] showLineNumbers

You can use the specifiers without a language:

```{5} showLineNumbers

```showLineNumbers {5}

```{2,3}

```showLineNumbers

Say we have the following markdown file, example.md:

```javascript {2} showLineNumbers
let a1;
let a2;
let a3;
```

I assume you use rehype-highlight for code highlighting. Our module, example.js, looks as follows:

import { read } from "to-vfile";
import remark from "remark";
import gfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeHighlight from "rehype-highlight";
import rehypeHighlightLines from "rehype-highlight-code-lines";
import rehypeStringify from "rehype-stringify";

main();

async function main() {
  const file = await remark()
    .use(gfm)
    .use(remarkRehype)
    .use(rehypeHighlight)
    .use(rehypeHighlightLines)
    .use(rehypeStringify)
    .process(await read("example.md"));

  console.log(String(file));
}

Now, running node example.js you will see that each line of code is wrapped in a span, which has appropriate class names (code-line, numbered-code-line, highlighted-code-line) and line numbering attribute data-line-number.

<pre>
  <code class="hljs language-javascript">
    <span class="code-line numbered-code-line" data-line-number="1">
      <span class="hljs-keyword">let</span> a1;
    </span>
    <span
      class="code-line numbered-code-line highlighted-code-line" data-line-number="2">
      <span class="hljs-keyword">let</span> a2;
    </span>
    <span class="code-line numbered-code-line" data-line-number="3">
      <span class="hljs-keyword">let</span> a3;
    </span>
  </code>
</pre>

Without rehype-highlight-code-lines, the lines of code wouldn't be in a span.

<pre>
  <code class="hljs language-javascript">
    <span class="hljs-keyword">let</span> a1;
    <span class="hljs-keyword">let</span> a2;
    <span class="hljs-keyword">let</span> a3;
  </code>
</pre>

Note: hljs prefix comes from rehype-highlight.

Usage in HTML attributes

rehype-highlight-code-lines runs on <code> elements with directives like showLineNumbers and range number in curly braces like {2-4, 8}. That directives can be passed as a word in markdown (```ts showLineNumbers {2-4, 8}) or as a class and attribute in HTML (<code class="language-ts show-line-numbers" data-highlight-lines="2-4, 8">).

The inverse occurs when the option showLineNumbers is true. All <code> are processed and numbered. Then (```ts noLineNumbers), or as a class (<code class="language-ts no-line-numbers">) can be used to prevent processing.

The class directives can be with dash or without, or camel cased.

See some example usage as HTML class and attributes (only opening <code> tags are provided, the rest is omitted.):

<code class="language-typescript show-line-numbers">
<code class="language-typescript showlinenumbers">
<code class="language-typescript showLineNumbers">

<code class="language-typescript show-line-numbers" data-highlight-lines="2-4">
<code class="language-typescript show-line-numbers" data-start-numbering="11">
<code class="language-typescript show-line-numbers" data-start-numbering="11" data-highlight-lines="2-4">

<code class="language-typescript no-line-numbers">
<code class="language-typescript nolinenumbers">
<code class="language-typescript noLineNumbers">
<code class="language-typescript no-line-numbers" data-highlight-lines="2-4">

<code class="no-line-numbers">
<code class="show-line-numbers">
<code class="show-line-numbers" data-highlight-lines="2-4">
<code data-highlight-lines="2-4">
<code data-start-numbering="11">

Say we have the following HTML fragment in a example.md:

<pre><code class="language-javascript show-line-numbers" data-highlight-lines="2">
let a1;
let a2;
let a3;
</code></pre>

I assume you use rehype-highlight for code highlighting. Our module, example.js, looks as follows:

import { read } from "to-vfile";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeRaw from "rehype-raw";
import rehypeHighlight from "rehype-highlight";
import rehypeHighlightLines from "rehype-highlight-code-lines";
import rehypeStringify from "rehype-stringify";

main();

async function main() {
  const file = await unified()
    .use(remarkParse)
    .use(remarkRehype, { allowDangerousHtml: true })
    .use(rehypeRaw)
    .use(rehypeHighlight)
    .use(rehypeHighlightLines)
    .use(rehypeStringify)
    .process(await read("example.md"));

  console.log(String(file));
}

Now, running node example.js you will see that each line of code is wrapped in a span, which has appropriate class names (code-line, numbered-code-line, highlighted-code-line) and line numbering attribute data-line-number.

<pre>
  <code class="hljs language-javascript">
    <span class="code-line numbered-code-line" data-line-number="1">
      <span class="hljs-keyword">let</span> a1;
    </span>
    <span
      class="code-line numbered-code-line highlighted-code-line" data-line-number="2">
      <span class="hljs-keyword">let</span> a2;
    </span>
    <span class="code-line numbered-code-line" data-line-number="3">
      <span class="hljs-keyword">let</span> a3;
    </span>
  </code>
</pre>

Options

All options are optional and have default values.

type HighlightLinesOptions = {
  showLineNumbers?: boolean; // default is "false"
  lineContainerTagName?: "span" | "div"; // deprecated, always span
};

use(rehypeHighlightLines, HighlightLinesOptions);

showLineNumbers

It is a boolean option which is for all code to be numbered.

By default, it is false.

use(rehypeHighlightLines, {
  showLineNumbers: true,
});

Now, all code blocks will become numbered.

If you want to exclude a specific code block not to be numbered, use noLineNumbers.

```[language] noLineNumbers {2}

```[language] noLineNumbers

```noLineNumbers

If you want to exclude a specific code block not to be numbered in HTML fragment (in <pre>) use no-line-numbers class. In that case, the directive could be with dash, or without, or camel cased.

<code class="language-ts no-line-numbers">

<code class="language-ts nolinenumbers">

<code class="language-ts noLineNumbers">

Sometimes you may want to start the line numbering from a specific number. In that cases, use showLineNumbers=[number] in code blocks. For example, below, the code block's line numbering will start from number 8.

```[language] {2} showLineNumbers=8

```[language] showLineNumbers=8

```showLineNumbers=8

If you want to start the line numbering from a specific number in HTML fragment (in <pre>) use data-start-numbering attribute.

<code class="..." data-start-numbering="8">

lineContainerTagName (Deprecated)

It is marked as @deprecated and will be removed in the next versions.

It was a union option which was "div" | "span" for providing custom HTML tag name for code lines.

By default, it is span which is inline level container. If you set it as div, the container will still be span after deprecation.

use(rehypeHighlightLines, {
  lineContainerTagName: "div", // @deprecated, effectless, always "span"
});

Examples:

// line numbering will occur as per directive "showLineNumber" and code-line containers will be <span> inline element
use(rehypeHighlightLines);

// all code blocks will be numbered by default and code-line containers will be <span> inline element
use(rehypeHighlightLines, {
  showLineNumbers: true,
});

An example screen snapshot from a working app:

markdown code block input markdown code block result

Here you can find some demo applications below which the rehype-highlight and rehype-highlight-code-lines are used together:

Styling

The following styles can be added for line highlighting and line numbering to work correctly:

Choose the colors as you wish!

.parent-container-of-pre {
  display: grid; /* in order { overflow-x: auto; } works in child <pre> */
}

pre,
pre code {
  background-color: var(--color-code-background);

  direction: ltr;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  line-height: 1.2;
  tab-size: 2;
  hyphens: none;
}

pre {
  padding: 0.5rem 1rem;
  border: 1px solid var(--color-text-weak);
  border-radius: 5px;

  overflow-x: auto;
}

pre > code {
  float: left;
  min-width: 100%;
}

.code-line {
  min-width: 100%;
  padding-left: 12px;
  padding-right: 12px;
  margin-left: -12px;
  margin-right: -12px;
  border-left: 4px solid transparent; /* prepare for highlighted code-lines */

  display: inline-block;
}

.code-line.inserted {
  background-color: var(--color-inserted-line); /* inserted code-line (+) */
}

.code-line.deleted {
  background-color: var(--color-deleted-line); /* deleted code-line (-) */
}

.highlighted-code-line {
  background-color: var(--color-highlighted-line);
  border-left: 4px solid var(--color-highlighted-line-indicator);
}

.numbered-code-line::before {
  content: attr(data-line-number);

  margin-left: -8px;
  margin-right: 16px;
  width: 1rem;
  color: var(--color-text-weak);
  text-align: right;

  display: inline-block;
}

Syntax tree

This plugin modifies the hast (HTML abstract syntax tree).

Types

This package is fully typed with TypeScript.

The plugin exports the type HighlightLinesOptions.

Compatibility

This plugin works with rehype-parse version 1+, rehype-stringify version 1+, rehype version 1+, and unified version 4+.

Security

Use of rehype-highlight-code-lines involves rehype (hast), but doesn't lead to cross-site scripting (XSS) attacks.

My Plugins

I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to have a look my plugins.

My Remark Plugins

My Rehype Plugins

My Recma Plugins

  • recma-mdx-escape-missing-components – Recma plugin to set the default value () => null for the Components in MDX in case of missing or not provided so as not to throw an error
  • recma-mdx-change-props – Recma plugin to change the props parameter into the _props in the function _createMdxContent(props) {/* */} in the compiled source in order to be able to use {props.foo} like expressions. It is useful for the next-mdx-remote or next-mdx-remote-client users in nextjs applications.

License

MIT License © ipikuka