定制 alto/code-highlight 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alto/code-highlight

Composer 安装命令:

composer require alto/code-highlight

包简介

A high-performance, semantic syntax highlighter for PHP 8.4+ with zero dependencies. Features context-aware parsing, language embedding, and compatibility with Highlight.js and Prism themes.

README 文档

README

Syntax highlighting for PHP projects

Server-side highlighting for the full PHP stack: 27 languages covering PHP, HTML, Twig, JavaScript, CSS, YAML, and more. Zero dependencies, semantic PHP syntax understanding definitions vs. calls, embedded language support, and 490+ compatible themes. Works in Twig templates, Laravel Blade, Symfony controllers—anywhere PHP runs.

Tests PHPStan PHP License

Features

  • Built for PHP ecosystems: Highlight the full stack—PHP, HTML, Twig, JavaScript, CSS, SQL, YAML, and 20+ more languages in a single library.
  • Zero dependencies: No Node.js, Python, or external processes. Just Composer. Works in any PHP 8.4+ environment.
  • Full PHP syntax: Semantic parser that understands context—distinguishes class User (definition) from new User() (usage), function greet() ( definition) from greet() (call).
  • Embedded languages: Automatically switches parsers for <style> and <script> tags in HTML, fenced code blocks in Markdown, and {% block css %} in Twig.
  • Dark mode support: Multiple dark themes included out of the box ( CupertinoDark, Dracula, Noctis) plus compatibility with 490+ Highlight.js and Prism themes.

Install

composer require alto/code-highlight

Usage

use Alto\Code\Highlight\Highlighter;
use Alto\Code\Highlight\Theme\GitHubTheme;

// 1. Initialize with a theme
$highlighter = new Highlighter(new GitHubTheme());

// 2. Output the theme's CSS (typically in your <head>)
echo "<style>" . $highlighter->getTheme()->getStylesheet() . "</style>";

// 3. Highlight your code
echo $highlighter->highlight($code, 'php');

Languages

PHP

Alto uses a semantic parser for PHP that goes beyond pattern matching to understand code context. It correctly distinguishes between:

  • Definitions vs. usage: class User vs. new User(), function greet() vs. greet()
  • Context-aware scoping: Variables, function calls, class instantiation, method calls

Embeddings

Four languages support automatic embedded language detection:

  • HTML — CSS in <style> tags, JavaScript in <script> tags
  • SVG — CSS in <style> tags, JavaScript in <script> tags
  • Markdown — Any language in fenced code blocks (```language)
  • Twig — Languages via block names ({% block css %}, {% block javascript %})

Full list

Category Languages
Programming Bash, C#, Go, Java, JavaScript, PHP, Python, Ruby, Rust, Swift, TypeScript
Markup HTML, SVG, XML
Data Diff, DotEnv, HTTP, JSON, YAML
Prose Markdown
Query SQL
Stylesheet CSS, SCSS
Template Twig
Config Dockerfile, INI, Makefile

Themes

Built-in themes

Alto includes 7 built-in themes ready to use:

  • Light themes: Alto, GitHub, Polar, Solar
  • Dark themes: CupertinoDark, Dracula, Noctis
use Alto\Code\Highlight\Theme\DraculaTheme;

$highlighter = new Highlighter(new DraculaTheme());

Dark mode

Three dark themes are included out of the box:

  • CupertinoDark — macOS-inspired dark theme
  • Dracula — Popular dark theme with vibrant colors
  • Noctis — Low-contrast dark theme for extended coding sessions

Compatibility

Use existing CSS from the Highlight.js (240+ themes), Prism (250+ themes), or TextMate (.tmTheme) ecosystems:

use Alto\Code\Highlight\Adapter\HighlightJsThemeAdapter;

$theme = HighlightJsThemeAdapter::fromFile('/path/to/github-dark.css');
$highlighter = new Highlighter($theme);
use Alto\Code\Highlight\Adapter\TextMateThemeAdapter;

$theme = TextMateThemeAdapter::fromFile('/path/to/monokai.tmTheme', isDark: true);
$highlighter = new Highlighter($theme);

Integrations

Twig Extension

Twig Extension — Highlight code directly in Twig templates using blocks or filters.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

Released by the Alto project under the MIT License. See the LICENSE file for details.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固