Notation Diff

How to add diff notation to show added and removed lines in code blocks.

ShikiMarkdownMDX

Preview

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

Installation

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

const rehypeShikiOptions: RehypeShikiCoreOptions = {
  //...
  transformers: [transformerNotationDiff()],
};

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();
const html = highlighter.codeToHtml(code, {
  lang: "javascript", // [!code --]
  lang: "typescript", // [!code ++]
  theme: "one-light",
});
```