Notation Focus

How to use Notation Focus transformer to highlight specific lines in code blocks.

ShikiMarkdownMDX

Preview

.ts
const code = `type Languages = "javascript" | "typescript";`;
const highlighter = await highlight(); 
const html = highlighter.codeToHtml(code, {
  lang: "typescript",
  theme: "one-light",
});

Installation

  1. Install the following dependencies:
Shiki Transformers
pnpm i @shikijs/transformers -D
  1. Set up transformerNotationFocus:
Shiki Notation Highlight Transformer
import { transformerNotationFocus } from "@shikijs/transformers";

const rehypeShikiOptions: RehypeShikiCoreOptions = {
  //...
  transformers: [
    transformerNotationFocus({
      classActivePre: "shiki-has-focused",
    }),
  ],
};

export { rehypeShikiOptions };
  1. Customize the styles in your CSS file if needed:
shikiShiki Highlighter#styles

The main highlighter utility to render syntax highlighted code.

Usage

To add diff notation to your code blocks, use the [!code ++] and [!code --] markers to indicate added and removed lines, respectively:

.mdx
```
const code = `type Languages = "javascript" | "typescript";`;
const highlighter = await highlight(); // [!code focus]
const html = highlighter.codeToHtml(code, {
  lang: "typescript", // [!code ++]
  theme: "one-light",
});
```