@md-plugins/md-plugin-codeblocks

A markdown-it plugin for code blocks.
13,396 4
Install
npm install @md-plugins/md-plugin-codeblocks
Latest Version:2.2.0
License:MIT
Last Updated:Sep 16, 2026
Links: GitHub  ·  npm
Maintainer: hawkeye64

md-plugin-codeblocks

npm version npm downloads npm monthly downloads license

GitHub Sponsors button PayPal donate button

Discord X

A Markdown-It plugin that enhances code block rendering by providing syntax highlighting, line numbering, and support for advanced features like tabbed code blocks. It integrates with Shiki for syntax highlighting and allows customization for various use cases.

Features

  • Syntax Highlighting: Automatically highlights code blocks using Shiki.
  • TwoSlash Hovers: Opt in to TypeScript-powered hover and query output for richer examples.
  • Line Numbering: Optionally adds line numbers to code blocks.
  • Magic Comments: Supports special comments like [[! highlight]], [[! add]], and [[! rem]] for inline code annotations.
  • Tabbed Code Blocks: Enables the creation of tabbed code blocks for multi-language or multi-file examples.
  • Customizable Components: Supports custom wrapper and copy button components.
  • Fallback for Unsupported Languages: Gracefully handles code blocks written in unsupported languages.

Installation

Install the plugin via your preferred package manager:

# with pnpm:
pnpm add @md-plugins/md-plugin-codeblocks
# with bun:
bun add @md-plugins/md-plugin-codeblocks
# with Yarn:
yarn add @md-plugins/md-plugin-codeblocks
# with npm:
npm install @md-plugins/md-plugin-codeblocks

Usage

Basic Setup

import MarkdownIt from 'markdown-it'
import { codeblocksPlugin } from '@md-plugins/md-plugin-codeblocks'

const md = new MarkdownIt()
md.use(codeblocksPlugin, {
  containerComponent: 'MarkdownPrerender',
  copyButtonComponent: '<MarkdownCopyButton',
  preClass: 'markdown-code',
  pageScripts: [
    "import MarkdownPrerender from '@/.q-press/components/MarkdownPrerender'",
    "import MarkdownCopyButton from '@/.q-press/components/MarkdownCopyButton.vue'",
  ],
})

Example Markdown Input

console.log('Hello, world!')

Example Output

The rendered output will include syntax-highlighted code wrapped in customizable components:

<markdown-prerender>
  <pre v-pre class="markdown-code language-javascript">
    <code>
      console<span class="token punctuation">.</span>
      <span class="token function">log</span>
      <span class="token punctuation">(</span>
      <span class="token string">'Hello, world!'</span>
      <span class="token punctuation">)</span>
      <span class="token punctuation">;</span>
    </code>
  </pre>
  <markdown-copy-button></markdown-copy-button>
</markdown-prerender>

Options

The md-plugin-codeblocks plugin supports the following options:

Option Type Default Description
defaultLang string 'markup' Default language for code blocks without a specified language.
containerComponent string 'markdown-prerender' Custom wrapper component for code blocks.
copyButtonComponent string 'markdown-copy-button' Custom copy button component.
preClass string 'markdown-code' CSS class for the <pre> element.
codeClass string '' CSS class for the <code> element.
tabPanelTagName string 'q-tab-panel' Tag name for the tab panels.
tabPanelTagClass string 'q-pa-none' CSS class for the tab panels.

Advanced Features

Line Numbering

The plugin supports magic comments for inline annotations:

```js [numbered]
// All lines will be numbered
console.log('Line 1')
console.log('Line 2')
console.log('Line 3')
```

TwoSlash Type Hovers

Add the twoslash attribute to TypeScript or JavaScript examples when you want inferred type information, compiler diagnostics, or ^? query output.

```ts [twoslash]
const count = 1
//    ^?
```

Code Block Height

Use maxheight when a long snippet should scroll inside the page. Use minheight when a short snippet needs more vertical room for overlays such as Twoslash hover popups.

```ts [twoslash minheight=12rem]
const selectedIcon = 'event' as const
//    ^?
```

Line Highlighting and Annotations

```js [highlight=2]
console.log('Line 1')
console.log('Line 2') // This line will be highlighted
console.log('Line 3')
```
```js
console.log('Line 1')
console.log('Line 2') [[highlight]] // This line will be highlighted
console.log('Line 3')
```
```js [add=2]
console.log('Line 1')
console.log('Line 2') // This line will be accented and prefixed with a '+'
console.log('Line 3')
```
```js [rem=2]
console.log('Line 1')
console.log('Line 2') // This line will be accented and prefixed with a '-'
console.log('Line 3')
```
Combining Annotations
```js [numbered highlight=1 rem=2 add=3]
console.log('Line 1') // This line will be highlighted
console.log('Line 2') // This line will be accented and prefixed with a '-'
console.log('Line 3') // This line will be accented and prefixed
```

Using Ranges

Additonally, with the exception of numbered, you can use ranges to annotate multiple lines:

[highlight=1,10-11 add=4,7-9 rem=12-14]

Tabbed Code Blocks

Easily create tabbed interfaces for multiple code examples:

When Q-Press renders the generated tab container, icon can select a bundled pnpm, npm, yarn, or bun icon. Other renderers may consume the generated tab-icons metadata themselves.

```tabs
<<|bash [icon=pnpm] pnpm|>>
pnpm add example-package

<<|bash [icon=npm] npm|>>
npm install example-package
```

Testing

Run the unit tests with Vitest:

pnpm test

Documentation

In case this README falls out of date, please refer to the documentation for the latest information.

Support

If md-plugin-codeblocks is useful in your workflow and you want to support ongoing maintenance:

License

This project is licensed under the MIT License. See the LICENSE file for details.

Related Packages

@md-plugins/md-plugin-image

A markdown-it plugin for handling images.

15,902 4
@md-plugins/md-plugin-inlinecode

A markdown-it plugin for handling inline code.

13,671 4
@md-plugins/md-plugin-frontmatter

A markdown-it plugin for handling frontmatter in markdown files.

13,642 4
@md-plugins/md-plugin-headers

A markdown-it plugin for handling headers (H1-H6).

13,577 4
@md-plugins/md-plugin-table

A markdown-it plugin for handling tables with custom tag and styles.

13,563 4

Version History

Version Released License
2.2.0 MIT
2.1.0 MIT
2.0.4 MIT
2.0.3 MIT
2.0.2 MIT
Pre-releases (77)
Version Released License
0.1.0-rc.19 MIT
0.1.0-rc.18 MIT
0.1.0-rc.17 MIT
0.1.0-rc.16 MIT
0.1.0-rc.15 MIT