Overview of Phiki syntax highlighting
2.xvscode-textmate, allowing it to provide a similar experience to the highlighting found in VS Code and Shiki within the PHP ecosystem.repository·2.x·Indexed 18 days ago
https://github.com/phikiphp/phikiPhiki is a PHP syntax highlighter inspired by Shiki that uses TextMate grammar files and Visual Studio Code themes to generate highlighted code for web applications. It supports custom grammars and themes, global and snippet-level caching via PSR-16, and integration with league/commonmark. Features include scope-based CSS classes via AddClassesTransformer, line highlighting, focusing, and diff annotations.
vscode-textmate, allowing it to provide a similar experience to the highlighting found in VS Code and Shiki within the PHP ecosystem.Phiki is a pure PHP package for syntax highlighting code using TextMate grammar files and Visual Studio Code themes. It is designed as a high-performance, zero-dependency alternative to Shiki and Highlight.php.
Key Advantages:
Psr\SimpleCache\CacheInterface implementation.You can highlight or focus specific lines by adding braces to the Markdown code block info string. The format is {highlight_lines}{focus_lines}.
{line_numbers} to highlight specific lines.{}{line_numbers} to focus specific lines. When lines are focused, Phiki adds a focus class to the <pre> element.// Highlight lines 2 and 4-8
```php {2,4-8}// Focus lines 2 and 4-8 (no highlights)
Transformers allow you to modify data at various stages of the syntax highlighting process. They can intercept and alter the input code, individual tokens, the HTML AST (Abstract Syntax Tree), or the final HTML output.
Transformers are applied sequentially: the return value from one transformer is passed as the input to the next.
Important considerations:
root, pre, code, etc.), be cautious when adding or removing elements, as this can break the structure expected by subsequent transformers in the pipeline.Phiki provides high-quality syntax highlighting by following a five-step pipeline that mimics the behavior of modern code editors like Visual Studio Code:
variable.other.php describing the text type).HighlightedToken objects.You can target specific lines to add custom CSS classes (e.g., for highlighting or focusing) using LineDecoration::forLine(index)->class('your-class'). Note that line indices are zero-based.
use Phiki\Transformers\Decorations\LineDecoration;
$output = (new Phiki)
->codeToHtml('<?php echo ...', Grammar::Php, Theme::GithubLight)
->decoration(
LineDecoration::forLine(0)->class('focus'),
);To add custom CSS classes to the gutter element (the area containing line numbers), ensure you have enabled the gutter with withGutter() and then use GutterDecoration::make()->class('your-class') via the PendingHtmlOutput::decoration() method.
use Phiki\Transformers\Decorations\GutterDecoration;
$output = (new Phiki)
->codeToHtml('<?php echo ...', Grammar::Php, Theme::GithubLight)
->withGutter()
->decoration(
GutterDecoration::make()->class('gutter-class'),
);Phiki supports inline annotations using special comments within the code. This is useful for avoiding hardcoded line numbers.
Add // [code! highlight], // [code! hl], or // [code! ~~] to a line.
Add // [code! focus], // [code! f], or // [code! **] to a line. This adds a focus class to the <pre> element.
// [code! insert], // [code! ++], or // [code! add]// [code! remove], // [code! --], or // [code! delete]Annotations can include a range parameter:
// [code! highlight:2] (current line + 2 following)// [code! highlight:-2] (current line + 2 preceding)// [code! highlight:1,3] (next 3 lines)// [code! highlight:start] and // [code! highlight:end] for open-ended ranges.echo "Hello, world!"; // [code! highlight]
echo "Hello, world!"; // [code! f]
$user = User::find(1); // [code! remove]
$user = User::findOrFail(1); // [code! insert]You can add custom CSS classes to the <pre> element that wraps the entire code block by using PreDecoration::make()->class('your-class') within the PendingHtmlOutput::decoration() method.
use Phiki\Transformers\Decorations\PreDecoration;
$output = (new Phiki)
->codeToHtml('<?php echo ...', Grammar::Php, Theme::GithubLight)
->decoration(
PreDecoration::make()->class('pre-class'),
);To add custom CSS classes to the <code> element that wraps the code content, use CodeDecoration::make()->class('your-class') via the PendingHtmlOutput::decoration() method.
use Phiki\Transformers\Decorations\CodeDecoration;
$output = (new Phiki)
->codeToHtml('<?php echo ...', Grammar::Php, Theme::GithubLight)
->decoration(
CodeDecoration::make()->class('code-class'),
);To bundle custom grammars and themes for reuse, create a PHP class that implements the Phiki\Contracts\ExtensionInterface.
Inside the register(Environment $environment): void method, use the $environment object to register your assets. You can use the grammar() method to register a new language grammar and the theme() method to register a new theme, both pointing to the file paths of your JSON definitions.
use Phiki\Contracts\ExtensionInterface;
use Phiki\Environment;
class MyExtension implements ExtensionInterface
{
public function register(Environment $environment): void
{
$environment
->grammar('my-language', __DIR__ . '/grammars/my-language.json')
->theme('my-theme', __DIR__ . '/themes/my-theme.json');
}
}