Notation Diff

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

ShikiMarkdownMDX

Preview

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. Customize the styles in your CSS file if needed:

Shiki Highlighter

The main highlighter utility to render syntax highlighted code.

Usage

shikijs/rehype

  1. Set up transformerNotationDiff:
Shiki Notation Highlight Transformer
import { transformerNotationDiff } from "@shikijs/transformers";

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

export { rehypeShikiOptions };
  1. To add diff notation to your code blocks, use the [!code ++] and [!code --] markers to indicate added and removed lines, respectively:
  • Typescript/JavaScript:
```
const code = `type Languages = "javascript" | "typescript";`;
const highlighter = await highlight();
const html = highlighter.codeToHtml(code, {
  lang: "javascript", // [!code --]
  lang: "typescript", // [!code ++]
  theme: "one-light",
});
```
  • React Code (TSX/JSX):

Use the {/* [!code ++] */} and {/* [!code --] */} comment syntax instead:

```tsx
export default function Page() {
  return (
    <h1>Hello</h1> {/* [!code --] */}
    <h1>Hello World</h1> {/* [!code ++] */}
  );
}
```

The transformer automatically detects tsx and jsx languages and handles the { /* */ } comment syntax accordingly.