PHPackages                             spatie/shiki-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/shiki-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/shiki-php
================

Highlight code using Shiki in PHP

2.3.3(3mo ago)31012.4M—1.3%3110MITJavaScriptPHP ^8.0CI passing

Since Jun 28Pushed 3w agoCompare

[ Source](https://github.com/spatie/shiki-php)[ Packagist](https://packagist.org/packages/spatie/shiki-php)[ Docs](https://github.com/spatie/shiki-php)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-shiki-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (28)Used By (10)

Code highlighting with Shiki in PHP
===================================

[](#code-highlighting-with-shiki-in-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/429ad7301625e0f6f6d0e908644d478baa75934458692d7abef0d87a484d8417/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f7368696b692d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/shiki-php)[![GitHub Tests Action Status](https://camo.githubusercontent.com/40b1a4f2936f8ebd73e14873f510a1c9b120a51d9be2d57e5989df2891b185f4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f7368696b692d7068702f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d5465737473)](https://github.com/spatie/shiki-php/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/98be84ff2ad98fc43b665085ce2de45e0de5c505c9c3bcc6febc98662549052a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f7368696b692d7068702f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d436f64652532305374796c65)](https://github.com/spatie/shiki-php/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/cd691989bd64f691357ee2d7bae5eec7e9a295e28c2e8416057010859e13d4ce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f7368696b692d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/shiki-php)

[Shiki](https://github.com/shikijs/shiki) is a beautiful syntax highlighter powered by the same language engine that many code editors use. This package allows you to use Shiki from PHP.

```
\Spatie\ShikiPhp\Shiki::highlight(
    code: '',
    language: 'php',
    theme: 'github-light',
);
```

This package also ships with the following extra languages, on top of the [100+ that Shiki supports](https://github.com/shikijs/shiki/tree/master/docs/languages.md) out of the box:

- Antlers
- Blade

Usage in Laravel and league/commonmark
--------------------------------------

[](#usage-in-laravel-and-leaguecommonmark)

Laravel users can easily use Shiki via our [spatie/laravel-markdown](https://github.com/spatie/laravel-markdown) package.

If you need a league/commonmark extension to highlight code, head over to [spatie/commonmark-shiki-highlighter](https://github.com/spatie/commonmark-shiki-highlighter).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/64145692afe9046003e6c5a700bcaa83bbed12f388586c0be295f6f8b0f43e91/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f7368696b692d7068702e6a70673f743d31)](https://spatie.be/github-ad-click/shiki-php)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/shiki-php
```

In your project, you must have the JavaScript package [`shiki`](https://github.com/shikijs/shiki) installed, otherwise the `` element will not be present in the output.

You can install it via npm

```
npm install shiki
```

... or Yarn.

```
yarn add shiki
```

Make sure you have installed Node 20 or higher.

Usage
-----

[](#usage)

Here's an example where we are going to highlight some PHP code.

```
use Spatie\ShikiPhp\Shiki;

Shiki::highlight(
    code: '',
    language: 'php',
    theme: 'github-light',
);
```

The output is this chunk of HTML which will render beautifully in the browser:

```
#2e3440ff">&lt;?php echo &quot;Hello World&quot;; ?&gt;
```

Marking lines as highlighted, added, deleted or focus
-----------------------------------------------------

[](#marking-lines-as-highlighted-added-deleted-or-focus)

Shiki-php allows you to mark certain lines as `highlighted`, `added`, `deleted` and `focus`. To do this, you can pass in the necessary lines to the `highlight` method:

```
use Spatie\ShikiPhp\Shiki;

// Highlighting lines 1 and 4,5,6
Shiki::highlight(
    code: $code,
    language: 'php',
    highlightLines: [1, '4-6'],
);

// Marking line 1 as added
Shiki::highlight(
    code: $code,
    language: 'php',
    addLines: [1],
);

// Marking line 1 as deleted
Shiki::highlight(
    code: $code,
    language: 'php',
    deleteLines: [1],
);

// Marking line 1 as focus
Shiki::highlight(
    code: $code,
    language: 'php',
    focusLines: [1],
);
```

You can then target these classes in your own CSS to color these lines how you want.

Determining available languages
-------------------------------

[](#determining-available-languages)

To get an array with [all languages that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/languages.md), call `getAvailableLanguages`

```
$shiki = new \Spatie\ShikiPhp\Shiki();

$shiki->getAvailableLanguages(); // returns an array
$shiki->languageIsAvailable('php'); // returns true
$shiki->languageIsAvailable('non-existing-language'); // returns false
```

Determining available themes
----------------------------

[](#determining-available-themes)

To get an array with [all themes that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/themes.md), call `getAvailableThemes`

```
$shiki = new \Spatie\ShikiPhp\Shiki();

$shiki->getAvailableThemes(); // returns an array
$shiki->themeIsAvailable('github-light'); // returns true
$shiki->themeIsAvailable('non-existing-theme'); // returns false
```

### Using a custom theme

[](#using-a-custom-theme)

Shiki [supports](https://github.com/shikijs/shiki/blob/master/docs/themes.md) any [VSCode themes](https://code.visualstudio.com/docs/getstarted/themes).

You can load a theme simply by passing an absolute path as the theme parameter.

```
use Spatie\ShikiPhp\Shiki;

Shiki::highlight(
    code: '',
    language: 'php',
    theme: __DIR__ . '/your-path-to/themes/some-theme.json',
);
```

Using Node Version Manager
--------------------------

[](#using-node-version-manager)

Under the hood, this package runs a node command to render the markdown. If you use NVM during development, then the package will be unlikely to find your version of node as it looks for the node executable in `/usr/local/bin` and `/opt/homebrew/bin` - if this is the case, then you should create a symlink between the node distributable in your NVM folder, to that of the `usr/local/bin`. Such a command might look like this:

```
sudo ln -s /home/some-user/.nvm/versions/node/v20.11.1/bin/node /usr/local/bin/node
```

Creating this symlink will allow the package to find your NPM executable. Please note, if you change your NPM version you will have to update your symlinks accordingly.

Testing
-------

[](#testing)

You can run all the tests with this command:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Rias Van der Veken](https://github.com/riasvdv)
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

The Blade syntax highlighting source is taken from [this repo](https://github.com/onecentlin/laravel-blade-snippets-vscode/blob/master/syntaxes/blade.tmLanguage.json).
The Antlers syntax highlighting source is taken from [this repo](https://github.com/Stillat/vscode-antlers-language-server/blob/main/client/syntaxes/antlers.json).

Alternatives
------------

[](#alternatives)

If you don't want to install and handle Shiki yourself, take a look at [Torchlight](https://torchlight.dev), which can highlight your code with minimal setup.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance89

Actively maintained with recent releases

Popularity67

Solid adoption and visibility

Community31

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Recently: every ~113 days

Total

24

Last Release

107d ago

Major Versions

0.0.2 → 1.0.02021-07-09

1.3.0 → 2.0.02024-02-19

PHP version history (2 changes)0.0.1PHP ^8.0

1.3.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (59 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (48 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (5 commits)")[![heywhy](https://avatars.githubusercontent.com/u/13123868?v=4)](https://github.com/heywhy "heywhy (4 commits)")[![s3ththompson](https://avatars.githubusercontent.com/u/970121?v=4)](https://github.com/s3ththompson "s3ththompson (3 commits)")[![Barbapapazes](https://avatars.githubusercontent.com/u/45267552?v=4)](https://github.com/Barbapapazes "Barbapapazes (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (2 commits)")[![mallardduck](https://avatars.githubusercontent.com/u/619938?v=4)](https://github.com/mallardduck "mallardduck (1 commits)")[![mxsgx](https://avatars.githubusercontent.com/u/45129872?v=4)](https://github.com/mxsgx "mxsgx (1 commits)")[![thecaliskan](https://avatars.githubusercontent.com/u/13554944?v=4)](https://github.com/thecaliskan "thecaliskan (1 commits)")[![timacdonald](https://avatars.githubusercontent.com/u/24803032?v=4)](https://github.com/timacdonald "timacdonald (1 commits)")[![timoisik](https://avatars.githubusercontent.com/u/9573441?v=4)](https://github.com/timoisik "timoisik (1 commits)")[![axelrindle](https://avatars.githubusercontent.com/u/7105632?v=4)](https://github.com/axelrindle "axelrindle (1 commits)")[![Drewdan](https://avatars.githubusercontent.com/u/5255492?v=4)](https://github.com/Drewdan "Drewdan (1 commits)")[![drewrichards](https://avatars.githubusercontent.com/u/5223220?v=4)](https://github.com/drewrichards "drewrichards (1 commits)")[![erikn69](https://avatars.githubusercontent.com/u/4933954?v=4)](https://github.com/erikn69 "erikn69 (1 commits)")[![JohnathonKoster](https://avatars.githubusercontent.com/u/5232890?v=4)](https://github.com/JohnathonKoster "JohnathonKoster (1 commits)")[![jonassiewertsen](https://avatars.githubusercontent.com/u/38906163?v=4)](https://github.com/jonassiewertsen "jonassiewertsen (1 commits)")[![joshbruce](https://avatars.githubusercontent.com/u/15252830?v=4)](https://github.com/joshbruce "joshbruce (1 commits)")

---

Tags

codehightlightphpspatieshiki

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/spatie-shiki-php/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-shiki-php/health.svg)](https://phpackages.com/packages/spatie-shiki-php)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/typescript-transformer

This is my package typescript-transformer

3706.5M16](/packages/spatie-typescript-transformer)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
