Phiki
Phiki is a PHP package for syntax highlighting code. It is designed as a replacement for other syntax highlighters like Shiki and Highlight.php. A key feature of Phiki is that it has zero third-party dependencies, which makes it easy to integrate into any PHP project.
Overview
Phiki’s core functionality involves highlighting code using TextMate grammar files and Visual Studio Code themes. This approach aims to achieve a high degree of highlighting accuracy, making code appear as if highlighted in a preferred code editor.
How it works
Phiki processes code through a five-step internal pipeline:
-
Tokenization
Phiki begins by tokenizing your code using a TextMate grammar file. This process breaks the code into smaller pieces of text, each with detailed “scope” information. A scope is a dot-separated string that describes the type of text, for example,variable.other.phpfor a PHP variable. -
Highlighting
After tokenization, Phiki highlights the tokens using a chosen Visual Studio Code theme. The scopes from each token are matched against a ruleset in the theme file. This ruleset defines the background color, foreground color, and font style for the token. The tokens are then transformed into aHighlightedTokenobject, which contains both the raw token and its styling information. -
Structuring
Following the highlighting, Phiki then creates an AST-like structure (Abstract Syntax Tree-like) to represent the desired HTML output. This intermediate structure allows for easier manipulation of the final output without complex string operations. -
Transformation
If necessary, Phiki can transform this intermediate structure. This stage is typically where extensions can hook into the process to modify the AST and add features such as line highlighting or custom class names. -
Rendering
Finally, Phiki “stringifies” the completed AST into HTML. This HTML can then be directly outputted to a browser, included in a template file, or saved to disk.
Why it exists
Phiki was developed to address shortcomings found in existing syntax highlighting solutions.
-
Highlight.php
While moderately performant, Highlight.php, a port of the popular highlight.js library, suffered from poor highlighting quality. -
Shiki
Shiki produced good-looking output but was slow to run from a PHP application because it required spawning a Node.js process for the actual highlighting. This made it impractical for live-rendered applications.
Phiki aims to solve these issues by offering a pure PHP implementation of both a TextMate tokenizer and a Visual Studio Code highlighter. This provides high-quality highlighting without the need for any third-party dependencies or services. The creator also mentions a personal interest in building and pushing the limits of what is possible with PHP.
Key concepts and features
-
TextMate grammars
Used to tokenize code and determine the “scope” of different text elements. -
Visual Studio Code themes
Used to apply styles (colors, fonts) to the tokenized code based on their scopes. -
Zero third-party dependencies
Enhances ease of integration into any PHP project.
Integrations
Phiki supports integrations with various platforms and tools, including:
- CommonMark
- Laravel
- Statamic