In this guide, you will learn how to use transformers for Shiki syntax highlighter in an Astro application. You will go through the process of setting up a new Astro project, installing the transformers for Shiki, and representing code additions and deletions. You will also learn how to focus on specific lines in a visually appealing manner.
Prerequisites
You’ll need the following:
- Node.js 18 or later
Table Of Contents
- Create a new Astro application
- Integrate Transformers for Shiki in your Astro project
- Build and Test your Astro application locally
Create a new Astro application
Let’s get started by creating a new Astro project. Execute the following command:
npm create astro
is the recommended way to scaffold an Astro project quickly.
When prompted, choose:
Empty
when prompted on how to start the new project.Yes
when prompted if plan to write Typescript.Strict
when prompted how strict Typescript should be.Yes
when prompted to install dependencies.Yes
when prompted to initialize a git repository.
Once that’s done, you can move into the project directory and start the app:
The app should be running on localhost:4321.
Now, let’s move on to integrating transformers for Shiki in your Astro application.
Integrate Transformers for Shiki in your Astro project
Install Transformers for Shiki
Execute the command below to install the necessary package for using transformers for Shiki:
The command installs the following library:
@shikijs/transformers
: A collection of transformers for shiki.
That’s the only library required to represent code in a visually appealing manner. Let’s update our Astro configuration to load these transformers.
Load Transformers for Shiki in Astro Configuration
To see the transformers for Shiki in effect, import the following in your Astro configuration file (astro.config.mjs
).
transformerNotationDiff
: Allows you to represent a change in a line of code.transformerMetaHighlight
: Allows you to highlight a line of code.transformerNotationFocus
: Allows you to focus a line of code.
To demonstrate usage of these transformers, you will create a markdown file in the src/content/blog
directory. Let’s create a sample.md
inside the directory (src/content/blog
) with the following content:
In the markdown you’d see the lines that need to be representing differently have comments-like string in them. Here are those three strings:
// [!code --]
: Allows you to specify which line should be represented as a deletion.// [!code ++]
: Allows you to specify which line should be represented as an addition.// [!code focus]
: Allows you to specify which line should be in focus. This is done via blurring out the rest of the lines in the code.
These specific strings output a CSS class with the line, that we need to define ourselves. Let’s define the particular CSS required in the src/pages/index.astro
file:
Basically, you’ve added a set of classes that choose how an added, removed or focused line would look like. Now, render the markdown by using the default Shiki syntax highlighting in Astro. To do so, make the following additions in the index route:
and you are done! As you have seen in this blog itself, you’d be able to see a visually appealing representation of code addition and deletion.
Build and Test your Astro application locally
To test the application, prepare a build and run the preview server using the command below:
Conclusion
In this guide, you learned how to use Transformers for Shiki in an Astro application to focus into specific lines of code and in a visually appealing manner highlight code additions and deletions.
If you have any questions or comments, feel free to reach out to me on Twitter.