spatie/sidecar-shiki
Composer 安装命令:
composer require spatie/sidecar-shiki
包简介
Run Shiki highlighting with Sidecar
README 文档
README
Shiki is a beautiful syntax highlighter powered by the same language engine that many code editors use. This package allows you to run Shiki on AWS Lambda through Sidecar.
You won't need to install Node, or any of Shiki's dependencies, on your server.
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.
Requirements
This package requires that hammerstone/sidecar has been installed in your Laravel application.
Follow the Sidecar installation and configuration instructions.
Installation
You can install the package via composer:
composer require spatie/sidecar-shiki
Optionally, you can publish the config file with:
php artisan vendor:publish --tag="sidecar-shiki-config"
Register the HighlightFunction::class in your sidecar.php config file.
/* * All of your function classes that you'd like to deploy go here. */ 'functions' => [ \Spatie\SidecarShiki\Functions\HighlightFunction::class, ],
Deploy the Lambda function by running:
php artisan sidecar:deploy --activate
See Sidecar documentation for details.
Usage
You can highlight code by calling the \Spatie\SidecarShiki\SidecarShiki::highlight() function.
use Spatie\SidecarShiki\SidecarShiki; SidecarShiki::highlight( code: '<?php echo "Hello World"; ?>', language: 'php', theme: 'github-light', );
The output is this chunk of HTML rendered through AWS Lambda which will output beautifully in the browser:
<pre class="shiki" style="background-color: #2e3440ff"><code><span class="line"><span style="color: #81A1C1"><?</span><span style="color: #D8DEE9FF">php </span><span style="color: #81A1C1">echo</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">"</span><span style="color: #A3BE8C">Hello World</span><span style="color: #ECEFF4">"</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">?></span></span></code></pre>
Marking lines as highlighted, added, deleted or focus
use Spatie\SidecarShiki\SidecarShiki; // Highlighting lines 1 and 4,5,6 SidecarShiki::highlight( code: $code, language: 'php', highlightLines: [1, '4-6'], ); // Marking line 1 as added SidecarShiki::highlight( code: $code, language: 'php', addLines: [1], ); // Marking line 1 as deleted SidecarShiki::highlight( code: $code, language: 'php', deleteLines: [1], ); // Marking line 1 as focus SidecarShiki::highlight( code: $code, language: 'php', focusLines: [1], );
You can then target these classes in your own CSS to color these lines how you want.
Using the Commonmark HighlightCodeExtension
Here's how we can create a function that can convert markdown to HTML with all code snippets highlighted. Inside the function we'll create a new MarkdownConverter that uses the HighlightCodeExtension provided by this package.
The $theme argument on HighlightCodeExtension expects the name of one of the many themes that Shiki supports.
use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\MarkdownConverter; use Spatie\SidecarShiki\Commonmark\HighlightCodeExtension; function convertToHtml(string $markdown, string $theme): string { $environment = (new Environment()) ->addExtension(new CommonMarkCoreExtension()) ->addExtension(new HighlightCodeExtension($theme)); $markdownConverter = new MarkdownConverter(environment: $environment); return $markdownConverter->convertToHtml($markdown); }
Testing
The testsuite makes connections to AWS and runs the deployed Lambda function. In order to run the testsuite, you will need an active AWS account.
We can use the native sidecar:configure artisan command to create the necessary AWS credentials for Sidecar. First copy the testbench.example.yaml file to testbench.yaml. Then run ./vendor/bin/testbench sidecar:configure to start the Sidecar setup process. (You only have to do the setup once)
cp testbench.yaml.example testbench.yaml cp .env.example .env ./vendor/bin/testbench sidecar:configure
After finishing the Sidecar setup process, you will have received a couple of SIDECAR_* environment variables. Add these credentials to both .env and testbench.yaml.
Now we can deploy our local HighlightFunction to AWS Lambda. Run the following command in your terminal, before executing the testsuite.
./vendor/bin/testbench sidecar-shiki:setup
After the successful deployment, you can run the testsuite.
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
Special thanks to Stefan Zweifel for his sidecar-browsershot package as a big help in how to test this.
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/sidecar-shiki 适用场景与选型建议
spatie/sidecar-shiki 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.22k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2022 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「highlighter」 「commonmark」 「spatie」 「shiki」 「sidecar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/sidecar-shiki 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/sidecar-shiki 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/sidecar-shiki 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP code highlighter
Allows HTML tags to be used in Markdown, while still escaping or stripping all other HTML input.
Yii2 Code syntax highlighter based on Google-Code-Prettify library.
A validating SQL lexer and parser with a focus on MySQL dialect.
League/commonmark extension for HTML figure and caption.
A collection of libraries for working with HTML5 markup
统计信息
- 总下载量: 10.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-24