Download the PHP package spatie/commonmark-shiki-highlighter without Composer

On this page you can find all versions of the php package spatie/commonmark-shiki-highlighter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package commonmark-shiki-highlighter

Highlight code blocks with league/commonmark and Shiki

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

This package contains a block renderer for league/commonmark to highlight code blocks using Shiki PHP.

This package also ships with the following extra languages, on top of the 100+ that Shiki supports out of the box:

If you're using Laravel, make sure to look at our spatie/laravel-markdown package which offers easy integration with Shiki in laravel projects.

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:

In your project, you must have v1 of the JavaScript package shiki installed, otherwise the <pre> element will not be present in the output.

You can install it via npm

or Yarn

Usage

Here's how we can create a function that can convert markdown to HTML with all code snippets highlighted. Inside the function will create a new MarkdownConverter that uses the HighlightCodeExtension provided by this package.

Alternatively, you can inject an already instantiated Shiki instance into the HighlightCodeExtension:

Using themes

The $theme argument on HighlightCodeExtension expects the name of one of the many themes that Shiki supports.

Alternatively, you can use a custom theme. Shiki supports any VSCode themes. You can load a theme simply by passing an absolute path of a theme file to the $theme argument.

Marking lines as highlighted, added, deleted and focus

You can mark lines using the Markdown info tag as highlighted or focused. You can prefix lines with + or - to mark them as added or deleted. In the first pair of brackets, you can specify line numbers that should be highlighted. In an optional second pair you can specify which lines should be focused on.

md

More syntax examples for highlighting & focusing

Line numbers start at 1.

```php - Don't highlight any lines

```php{4} - Highlight just line 4
```php{4-6} - Highlight the range of lines from 4 to 6 (inclusive)
```php{1,5} - Highlight just lines 1 and 5 on their own
```php{1-3,5} - Highlight 1 through 3 and then 5 on its own
```php{5,7,2-3} - The order of lines don't matter. However, specifying 3-2 will not work.

```php{}{4} - Focus just line 4
```php{}{4-6} - Focus the range of lines from 4 to 6 (inclusive)
```php{}{1,5} - Focus just lines 1 and 5 on their own
```php{}{1-3,5} - Focus 1 through 3 and then 5 on its own
```php{}{5,7,2-3} - The order of lines don't matter. However, specifying 3-2 will not work.

Styling highlighted lines

When you mark lines as highlighted, added, deleted or focused, Shiki will apply some classes to those lines. You should add some CSS to your page to style those lines. Here's a bit of example CSS to get you started.

Throwing on exceptions

By default, the Shiki highlighter will not throw when something goes wrong and just return the non-highlighted code. If you want to throw the exception anyway, instantiate the highlighter with throw as true:

A word on performance

Highlighting with Shiki is a resource intensive process. We highly recommend using some form of caching.

Testing

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

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.


All versions of commonmark-shiki-highlighter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
league/commonmark Version ^2.4.2
spatie/shiki-php Version ^2.0
symfony/process Version ^6.0|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/commonmark-shiki-highlighter contains the following files

Loading the files please wait ....