Skip to content

Commit b1d0b1e

Browse files
committed
add shiki transformer example to tutorial
1 parent a18028e commit b1d0b1e

File tree

1 file changed

+82
-1
lines changed
  • app/web_development/tutorials/next-js-static-first-mdx-starterkit/code-highlighting-plugin

1 file changed

+82
-1
lines changed

app/web_development/tutorials/next-js-static-first-mdx-starterkit/code-highlighting-plugin/page.mdx

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Code highlighting plugin - Next.js 15 Tutorial - Next.js 15 Tutorial
33
description: Code highlighting plugin - Next.js 15 static first MDX starterkit | Web development tutorials | www.chris.lu
44
keywords: ['highlighting', 'rehype', 'plugin', 'pretty', 'code', 'shiki']
55
published: 2024-12-31T23:00:00.000Z
6-
modified: 2025-01-17T23:00:00.000Z
6+
modified: 2025-01-20T23:00:00.000Z
77
permalink: https://chris.lu/web_development/tutorials/next-js-static-first-mdx-starterkit/code-highlighting-plugin
88
section: Web development
99
---
@@ -701,6 +701,87 @@ function helloWorld() {
701701
702702
With this language you can use the **+** and **-** signs to tell the code highlighter to display the rows starting with a plus sign as if they had been added and the rows starting with a minus sign as if they had been removed
703703
704+
## Diff using a shiki transformer
705+
706+
You can also use shiki transformers in your rehype-pretty-code setup, as we just saw in the previous chapter, to create diffs one way is to use a language flag, the second way is to use the [shiki diff notation transformer](https://shiki.style/guide/transformers)
707+
708+
First we install the shiki transformers package:
709+
710+
```shell
711+
npm i @shikijs/transformers --save-exact
712+
```
713+
714+
Next we add a diff notation transformer to our Next.js configuration:
715+
716+
```js title="next.config.mjs" showLineNumbers {8} {22-24}
717+
import { withSentryConfig } from '@sentry/nextjs';
718+
//import type { NextConfig } from 'next'
719+
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants.js'
720+
import createMdx from '@next/mdx'
721+
import rehypeMDXImportMedia from 'rehype-mdx-import-media'
722+
import rehypePrettyCode from 'rehype-pretty-code'
723+
import { readFileSync } from 'fs'
724+
import { transformerNotationDiff } from '@shikijs/transformers'
725+
726+
const nextConfig = (phase/*: string*/) => {
727+
728+
const themePath = new URL('./node_modules/material-theme/themes/OneDark-Pro.json', import.meta.url)
729+
const themeFileContent = readFileSync(themePath, 'utf-8')
730+
731+
/** @type {import('rehype-pretty-code').Options} */
732+
const rehypePrettyCodeOptions = {
733+
theme: JSON.parse(themeFileContent),
734+
keepBackground: false,
735+
defaultLang: {
736+
block: 'js',
737+
},
738+
transformers: [transformerNotationDiff({
739+
matchAlgorithm: 'v3',
740+
})],
741+
}
742+
```
743+
744+
Line 8: we import the shiki "diff notation" transformer
745+
746+
Lines 22 to 24: add out shiki transformer to the rehype-pretty-code **transformers option**
747+
748+
> [!NOTE]
749+
> we did set the "transformer matching algorithm" option () to v3 (as recommended in the transformers chapter of the [shiki v2 migration guide](https://shiki.style/blog/v2#transformers-matching-algorithm)) to get rid of the shiki deprecation notice:
750+
>
751+
> > [SHIKI DEPRECATE]: The default `matchAlgorithm: "v1"` is deprecated and will be removed in the future. Please explicitly set `matchAlgorithm: "v3"` in the transformer options.
752+
753+
Next we edit our global.css to add styling for added and removed rows:
754+
755+
```css title="/app/global.css"
756+
[data-line].remove {
757+
background-color: #852424;
758+
border-left-color: #ff0000;
759+
}
760+
761+
[data-line].add {
762+
background-color: #248531;
763+
border-left-color: #18c218;
764+
}
765+
```
766+
767+
How the [transformer diff notation](https://shiki.style/packages/transformers#transformernotationdiff) works, is that at the end of every line you use a comment to tell the transformer if code got added (`// [!code ++]`) or removed (`// [!code --]`):
768+
769+
````md title="/app/(tutorial_examples)/code-highlighting_playground/page.mdx" showLineNumbers {11}
770+
<article>
771+
772+
```js
773+
function helloWorld() {
774+
// this is a comment
775+
let greeting = 'Hello World!' // [!code ++]
776+
console.log(greeting) // [!code --]
777+
}
778+
```
779+
780+
> [!MORE]
781+
> [shiki v2 migration guide](https://shiki.style/blog/v2)
782+
> [shiki "transformers" guide](https://shiki.style/guide/transformers)
783+
> [shiki "diff notation transformer" documentation](https://shiki.style/packages/transformers#transformernotationdiff)
784+
704785
## Highlighting inline code
705786
706787
Rehype pretty code can also highlight inline code.

0 commit comments

Comments
 (0)