Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use.

Overview

Code highlighting with Shiki in PHP

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

', language: 'php', theme: 'github-light', ); ">
\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 out of the box:

  • Antlers
  • Blade

Usage in Laravel and league/commonmark

Laravel users can easily use Shiki via our spatie/laravel-markdown package.

If you need a league/commonmark extension to highlight code, head over to spatie/commonmark-shiki-highlighter.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

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. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/shiki-php

In your project, you should have the JavaScript package shiki installed. You can install it via npm...

npm install shiki

... or Yarn.

yarn add shiki

Make sure you have installed Node 10 or higher.

Usage

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

', language: 'php', theme: 'github-light', ); ">
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:

<?php echo "Hello World"; ?> ">
class="shiki" style="background-color: #2e3440ff">class="line">"color: #81A1C1"><?"color: #D8DEE9FF">php "color: #81A1C1">echo"color: #D8DEE9FF"> "color: #ECEFF4">""color: #A3BE8C">Hello World"color: #ECEFF4">""color: #81A1C1">;"color: #D8DEE9FF"> "color: #81A1C1">?>

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

To get an array with all languages that Shiki supports, 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

To get an array with all themes that Shiki supports, 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

Shiki supports any VSCode themes.

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

', language: 'php', theme: __DIR__ . '/your-path-to/themes/some-theme.json', ); ">
use Spatie\ShikiPhp\Shiki;

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

Testing

You can run all the tests with this command:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

The Blade syntax highlighting source is taken from this repo.
The Antlers syntax highlighting source is taken from this repo.

Alternatives

If you don't want to install and handle Shiki yourself, take a look at Torchlight, which can highlight your code with minimal setup.

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Making multiple identical function calls has the same effect as making a single function call.

Making multiple identical function calls has the same effect as making a single function call.

Share value objects that contain the same primitive value as a singleton

sharable-value-objects Share value objects that contain the same primitive value as a singleton. Singletons are kept under WeakReference objects. Inst

A layout field that forces translations to have the same structure.
A layout field that forces translations to have the same structure.

Kirby translatedlayout plugin A layout field that forces translations to have anidentical structure to the one of the default language. This is an exp

Alerts users in the SilverStripe CMS when multiple people are editing the same page.

Multi-User Editing Alert Alerts users in the SilverStripe CMS when multiple people are editing the same page. Maintainer Contact Julian Seidenberg ju

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.
A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

A repository for showcasing my knowledge of the PHP programming language, and continuing to learn the language.

Learning PHP (programming language) I know very little about PHP. This document will list all my knowledge of the PHP programming language. Basic synt

DBML parser for PHP8. It's a PHP parser for DBML syntax.
DBML parser for PHP8. It's a PHP parser for DBML syntax.

DBML parser written on PHP8 DBML (database markup language) is a simple, readable DSL language designed to define database structures. This page outli

Comments
  • Downgrade to php7.4

    Downgrade to php7.4

    This PR does downgrade so Shiki does support PHP7.4 on top of PHP8 support.

    To make the tests pass, I needed to remove some nice PHP8 syntax.

    // Instead of this
    $highlightedCode = Shiki::highlight($code, highlightLines: ['1', '2-4']);
    
    // Downgrade to PHP 7.4
    $highlightedCode = Shiki::highlight($code, null, null, ['1', '2-4']);
    

    The good thing is, that someone who is using Shiki with PHP 8, can still use the nice PHP 8 syntax. It does only affect those, who wanna use it with PHP7.4.

    • Tests do pass for PHP 7.4
    • Readme has been updated

    Motivation

    It's a little dependency fun :-)

    We are working on a PR to add TipTap 2 for Statamic. Statamic does support PHP 7.4 at the moment. https://github.com/statamic/cms/pull/6043

    To upgrade, Statamic requires tiptap-php In theory, it does support PHP 7.4

    As tiptap does require shiki, shiki does require PHP 8 which does prevent it from beeing usable with PHP 7.4

    opened by jonassiewertsen 1
  • Add information to README.md about using NVM

    Add information to README.md about using NVM

    Hey Guys!

    I tried out the spatie/markdown package yesterday and I got stuck on why it would not render properly. It transpires the issue was that I use NVM, and the package was looking for node in a particular place, where it was not. It took me far longer to realize that this was what was happening, and required the help of someone in the Laracasts forum too!

    My first though was to update the package to accept another variable, so the user could specify the location of their node executable. But then I decided it probably made more sense just to symlink the file instead, as the likelihood is, in production, node would be installed instead of NVM, so it's less of an issue.

    I figured it would be good to add this to the readme, to help anyone else that gets stuck. If this PR is accepted, I will also make a similar on one the spatie/markdown package too!

    Thanks Guys :)

    opened by Drewdan 1
  • Allow for user customizable renderer scripts

    Allow for user customizable renderer scripts

    Per the title this PR allows a user to set a customizable renderer script. This can be helpful if someone wants to further control how Shiki renders a block to HTML. In my case the use-case I'm hoping to support is the same one used in the tests. Modifying the output to include CSS properties/variables.

    It's accomplished simply by allowing the Process CWD path to be customized. This way someone can easily copy the existing renderer script and make any needed modifications. Then they just call the static method somewhere before using the Shiki class to render a code block.

    opened by mallardduck 1
Releases(1.3.0)
Owner
Spatie
We create products and courses for the developer community
Spatie
Highlight code blocks with league/commonmark and Shiki

Highlight code blocks with league/commonmark and Shiki This package contains a block renderer for league/commonmark to highlight code blocks using Shi

Spatie 55 Aug 27, 2022
This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every translatable field.

Autotranslate This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every

null 4 Jan 3, 2022
This plugin allows you to create many-to-many relationships between pages in Kirby and synchronizes them on both sides.

Kirby 3 Many To Many Field This plugin allows you to create many-to-many relationships between pages in Kirby.

Jonas Holfeld 41 Nov 19, 2022
Bracket and tag highlighter for Sublime Text

BracketHighlighter Bracket Highlighter matches a variety of brackets such as: [], (), {}, "", '', <tag></tag>, and even custom brackets. This was orig

Isaac Muse 1.7k Dec 27, 2022
Silverstripe module allowing editors to create newsletters using elemental blocks and export them to a sendy instance

Silverstripe Sendy Silverstripe module allowing editors to create newsletters using elemental blocks and export them to a sendy instance. Introduction

Syntro Opensource 4 Apr 20, 2022
A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.

Slack for PHP A simple PHP package for sending messages to Slack with incoming webhooks, focussed on ease-of-use and elegant syntax. Note: this packag

Regan McEntyre 1.2k Oct 29, 2022
This simple code lets you to get how many % of this year had passed

Year-Percent-PHP This simple code lets you to get how many % of this year had passed Code is part of Telegram bot and was made for Telegram channel Ye

Puzzak 1 Jan 3, 2022