PHPackages                             mstafreshi/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. mstafreshi/shiki-php

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

mstafreshi/shiki-php
====================

Highlight code using Shiki in PHP

00JavaScript

Since Jun 26Pushed todayCompare

[ Source](https://github.com/mstafreshi/shiki-php)[ Packagist](https://packagist.org/packages/mstafreshi/shiki-php)[ RSS](/packages/mstafreshi-shiki-php/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

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

22

—

LowBetter than 21% of packages

Maintenance65

Regular maintenance activity

Popularity0

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9951266?v=4)[Mohsen Safari](/maintainers/mstafreshi)[@mstafreshi](https://github.com/mstafreshi)

---

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)")[![Barbapapazes](https://avatars.githubusercontent.com/u/45267552?v=4)](https://github.com/Barbapapazes "Barbapapazes (3 commits)")[![s3ththompson](https://avatars.githubusercontent.com/u/970121?v=4)](https://github.com/s3ththompson "s3ththompson (3 commits)")[![mstafreshi](https://avatars.githubusercontent.com/u/9951266?v=4)](https://github.com/mstafreshi "mstafreshi (3 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 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)")

### Embed Badge

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

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

###  Alternatives

[codeaken/sshkey

Library for working with and generating SSH keys

1261.0k1](/packages/codeaken-sshkey)[sclinternet/bm-calendar

Calendar module which provide the ability to block off days.

161.1k](/packages/sclinternet-bm-calendar)

PHPackages © 2026

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