spatie/commonmark-shiki-highlighter
Composer 安装命令:
composer require spatie/commonmark-shiki-highlighter
包简介
Highlight code blocks with league/commonmark and Shiki
README 文档
README
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:
- Antlers
- Blade
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:
composer require spatie/commonmark-shiki-highlighter
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
npm install shiki@^1.3.0
or Yarn
yarn add shiki@^1.3.0
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.
use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\MarkdownConverter; use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension; function convertToHtml(string $markdown, string $theme): string { $environment = (new Environment()) ->addExtension(new CommonMarkCoreExtension()) ->addExtension(new HighlightCodeExtension(theme: $theme)); $markdownConverter = new MarkdownConverter(environment: $environment); return $markdownConverter->convertToHtml($markdown); }
Alternatively, you can inject an already instantiated Shiki instance into the HighlightCodeExtension:
use Spatie\ShikiPhp\Shiki; use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension; $environment->addExtension(new HighlightCodeExtension(shiki: new Shiki()));
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.
```php{1,2}{3} <?php echo "We're highlighting line 1 and 2"; echo "And focusing line 3";
```php <?php + echo "This line is marked as added"; - echo "This line is marked as deleted";
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.
.shiki .highlight { background-color: hsl(197, 88%, 94%); padding: 3px 0; } .shiki .add { background-color: hsl(136, 100%, 96%); padding: 3px 0; } .shiki .del { background-color: hsl(354, 100%, 96%); padding: 3px 0; } .shiki.focus .line:not(.focus) { transition: all 250ms; filter: blur(2px); } .shiki.focus:hover .line { transition: all 250ms; filter: blur(0); }
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:
$environment = (new Environment()) ->addExtension(new CommonMarkCoreExtension()) ->addExtension(new HighlightCodeExtension(theme: $theme, throw: true));
A word on performance
Highlighting with Shiki is a resource intensive process. We highly recommend using some form of caching.
Testing
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
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.
spatie/commonmark-shiki-highlighter 适用场景与选型建议
spatie/commonmark-shiki-highlighter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.1M 次下载、GitHub Stars 达 90, 最近一次更新时间为 2021 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「commonmark-shiki-highlighter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/commonmark-shiki-highlighter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/commonmark-shiki-highlighter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/commonmark-shiki-highlighter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 4.1M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 90
- 点击次数: 30
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-09