{"id":13623761,"url":"https://github.com/spatie/shiki-php","last_synced_at":"2025-05-15T18:09:50.302Z","repository":{"id":39243869,"uuid":"380212711","full_name":"spatie/shiki-php","owner":"spatie","description":"Code highlighting with Shiki in PHP","archived":false,"fork":false,"pushed_at":"2024-09-29T17:25:06.000Z","size":1035,"stargazers_count":283,"open_issues_count":0,"forks_count":25,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-30T00:09:14.979Z","etag":null,"topics":["code","hightlight","php"],"latest_commit_sha":null,"homepage":"https://freek.dev/2024-how-to-render-markdown-with-perfectly-highlighted-code-snippets","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie"}},"created_at":"2021-06-25T11:10:46.000Z","updated_at":"2024-10-18T09:25:22.000Z","dependencies_parsed_at":"2024-02-19T09:43:25.870Z","dependency_job_id":"10060a60-7b2c-423a-b3a0-dab1916bc377","html_url":"https://github.com/spatie/shiki-php","commit_stats":{"total_commits":89,"total_committers":12,"mean_commits":7.416666666666667,"dds":0.5393258426966292,"last_synced_commit":"a730dee901447d07a02442d11efcf66c15b3c787"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fshiki-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fshiki-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fshiki-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fshiki-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/shiki-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254394722,"owners_count":22063984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code","hightlight","php"],"created_at":"2024-08-01T21:01:35.359Z","updated_at":"2025-05-15T18:09:50.277Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie"],"categories":["目录","Table of Contents","PHP"],"sub_categories":["标记和CSS Markup and CSS","Markup and CSS","Globalization"],"readme":"# Code highlighting with Shiki in PHP\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/shiki-php.svg?style=flat-square)](https://packagist.org/packages/spatie/shiki-php)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/spatie/shiki-php/run-tests.yml?branch=main\u0026label=Tests)](https://github.com/spatie/shiki-php/actions?query=workflow%3ATests+branch%3Amaster)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/spatie/shiki-php/php-cs-fixer.yml?branch=main\u0026label=Code%20Style)](https://github.com/spatie/shiki-php/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/shiki-php.svg?style=flat-square)](https://packagist.org/packages/spatie/shiki-php)\n\n[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.\n\n```php\n\\Spatie\\ShikiPhp\\Shiki::highlight(\n    code: '\u003c?php echo \"Hello World\"; ?\u003e',\n    language: 'php',\n    theme: 'github-light',\n);\n```\n\nThis 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: \n\n- Antlers\n- Blade\n\n## Usage in Laravel and league/commonmark\n\nLaravel users can easily use Shiki via our [spatie/laravel-markdown](https://github.com/spatie/laravel-markdown) package.\n\nIf you need a league/commonmark extension to highlight code, head over to [spatie/commonmark-shiki-highlighter](https://github.com/spatie/commonmark-shiki-highlighter).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/shiki-php.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/shiki-php)\n\nWe 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).\n\nWe 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).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/shiki-php\n```\n\nIn your project, you must have the JavaScript package [`shiki`](https://github.com/shikijs/shiki) installed, otherwise the `\u003cpre\u003e` element will not be present in the output. \n\nYou can install it via npm\n\n```bash\nnpm install shiki\n```\n\n... or Yarn.\n\n```bash\nyarn add shiki\n```\n\nMake sure you have installed Node 10 or higher.\n\n## Usage\n\nHere's an example where we are going to highlight some PHP code.\n\n```php\nuse Spatie\\ShikiPhp\\Shiki;\n\nShiki::highlight(\n    code: '\u003c?php echo \"Hello World\"; ?\u003e',\n    language: 'php',\n    theme: 'github-light',\n);\n```\n\nThe output is this chunk of HTML which will render beautifully in the browser:\n\n```php\n\u003cpre class=\"shiki\" style=\"background-color: #2e3440ff\"\u003e\u003ccode\u003e\u003cspan class=\"line\"\u003e\u003cspan style=\"color: #81A1C1\"\u003e\u0026lt;?\u003c/span\u003e\u003cspan style=\"color: #D8DEE9FF\"\u003ephp \u003c/span\u003e\u003cspan style=\"color: #81A1C1\"\u003eecho\u003c/span\u003e\u003cspan style=\"color: #D8DEE9FF\"\u003e \u003c/span\u003e\u003cspan style=\"color: #ECEFF4\"\u003e\u0026quot;\u003c/span\u003e\u003cspan style=\"color: #A3BE8C\"\u003eHello World\u003c/span\u003e\u003cspan style=\"color: #ECEFF4\"\u003e\u0026quot;\u003c/span\u003e\u003cspan style=\"color: #81A1C1\"\u003e;\u003c/span\u003e\u003cspan style=\"color: #D8DEE9FF\"\u003e \u003c/span\u003e\u003cspan style=\"color: #81A1C1\"\u003e?\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\n```\n\n## Marking lines as highlighted, added, deleted or focus\n\nShiki-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:\n\n```php\nuse Spatie\\ShikiPhp\\Shiki;\n\n// Highlighting lines 1 and 4,5,6\nShiki::highlight(\n    code: $code,\n    language: 'php',\n    highlightLines: [1, '4-6'],\n);\n\n// Marking line 1 as added\nShiki::highlight(\n    code: $code,\n    language: 'php',\n    addLines: [1],\n);\n\n// Marking line 1 as deleted\nShiki::highlight(\n    code: $code,\n    language: 'php',\n    deleteLines: [1],\n);\n\n// Marking line 1 as focus\nShiki::highlight(\n    code: $code,\n    language: 'php',\n    focusLines: [1],\n);\n```\n\nYou can then target these classes in your own CSS to color these lines how you want.\n\n## PHP 7.4 support\n\nShiki has a nice and easy syntax in combination with at least PHP 8.\n\nIt does support PHP 7.4, but does loose a little bit of it's nice syntax if using it with PHP7.4, as you need to follow the order of the variables.\n\n```php\n// As reference\nhighlight(\n    string $code,\n    ?string $language = 'php',\n    ?string $theme = 'nord',\n    ?array $highlightLines = [],\n    ?array $addLines = [],\n    ?array $deleteLines = [],\n    ?array $focusLines = []\n)\n\n// Instead of PHP 8 syntax\nShiki::highlight(\n    code: $code,\n    language: 'php',\n    deleteLines: [1],\n);\n\n// You need to follow PHP 7.4 syntax\nShiki::highlight(\n    $code,\n    'php',\n    null,\n    null,\n    [1],\n);\n```\n\n## Determining available languages\n\nTo get an array with [all languages that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/languages.md), call `getAvailableLanguages`\n\n```php\n$shiki = new \\Spatie\\ShikiPhp\\Shiki();\n\n$shiki-\u003egetAvailableLanguages(); // returns an array\n$shiki-\u003elanguageIsAvailable('php'); // returns true\n$shiki-\u003elanguageIsAvailable('non-existing-language'); // returns false\n```\n\n## Determining available themes\n\nTo get an array with [all themes that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/themes.md), call `getAvailableThemes`\n\n```php\n$shiki = new \\Spatie\\ShikiPhp\\Shiki();\n\n$shiki-\u003egetAvailableThemes(); // returns an array\n$shiki-\u003ethemeIsAvailable('github-light'); // returns true\n$shiki-\u003ethemeIsAvailable('non-existing-theme'); // returns false\n```\n\n### Using a custom theme\n\nShiki [supports](https://github.com/shikijs/shiki/blob/master/docs/themes.md) any [VSCode themes](https://code.visualstudio.com/docs/getstarted/themes).\n\nYou can load a theme simply by passing an absolute path as the theme parameter.\n\n```php\nuse Spatie\\ShikiPhp\\Shiki;\n\nShiki::highlight(\n    code: '\u003c?php echo \"Hello World\"; ?\u003e',\n    language: 'php',\n    theme: __DIR__ . '/your-path-to/themes/some-theme.json',\n);\n```\n\n## Using Node Version Manager\n\nUnder the hood, this package runs a node command  to render the markdown. If you use NVM during development, \nthen the package will be unlikely to find your version of node as it looks for the node executable in\n`/usr/local/bin` and `/opt/homebrew/bin` - if this is the case, then you should create a symlink between\nthe node distributable in your NVM folder, to that of the `usr/local/bin`. Such a command might look like this:\n\n```bash\nsudo ln -s /home/some-user/.nvm/versions/node/v17.3.1/bin/node /usr/local/bin/node\n```\n\nCreating this symlink will allow the package to find your NPM executable. Please note, if you change\nyour NPM version you will have to update your symlinks accordingly.\n\n## Testing\n\nYou can run all the tests with this command:\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Rias Van der Veken](https://github.com/riasvdv)\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nThe Blade syntax highlighting source is taken from [this repo](https://github.com/onecentlin/laravel-blade-snippets-vscode/blob/master/syntaxes/blade.tmLanguage.json).  \nThe Antlers syntax highlighting source is taken from [this repo](https://github.com/Stillat/vscode-antlers-language-server/blob/main/client/syntaxes/antlers.json).\n\n## Alternatives\n\nIf 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.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fshiki-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fshiki-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fshiki-php/lists"}