Search by

oxmark / oxmark-php-ext

d9beuD

Native PHP extension for Comark Markdown, syntax highlighting, and Mermaid SVG.

Package info

github.com/oxmark/oxmark-php-ext

Language:Rust

Type:php-ext

Ext name:ext-oxmark

pkg:composer/oxmark/oxmark-php-ext

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 4

v0.1.1 2026-12-01 14:18 UTC

README

oxmark_to_html(string $markdown, ?array $catalog = null, ?callable $load_grammar = null, bool $auto_detect = false): string renders UTF-8 Comark Markdown with Oxmark's default sanitization and inline Mermaid SVG. Pass a catalog and a loader together to enable syntax highlighting with the embedded GitHub light and dark themes. Invalid UTF-8 throws ValueError.

Install with PIE

pie install oxmark/oxmark-php-ext
php --ri oxmark

The package builds from source for PHP 8.4, 8.5, and 8.6 on Linux and macOS. Install Rust and Cargo, a C toolchain, libclang, and the PHP build tools for your target PHP, including phpize, php-config, Autoconf, Make, and libtool. Cargo fetches the locked Rust dependencies. The source includes the default themes under their upstream licenses.

Build and verify from source

phpize
./configure --with-php-config="$(command -v php-config)"
make
php -n -d "extension=$PWD/modules/oxmark.so" tests/auto_detect.php

The build picks PHP through the selected php-config. Set LIBCLANG_PATH to the directory containing libclang if bindgen cannot find it. To run the additional Shiki smoke test, set OXLIGHT_GRAMMAR_FIXTURES to the tests/fixtures/langs directory of an Oxlight checkout and pass tests/smoke.php to PHP in the last command.

In a Docker build using the official PHP image, install the build tools, unpack the extension source to /usr/src/php/ext/oxmark, then run docker-php-ext-install oxmark.

Composer grammar catalog

The PHP library can generate a catalog when it packages its assets. Use canonical Shiki registration names as the array keys, with a unique TextMate scope per registration. Each entry has these fields.

Field Value
sha256 The 64-character hexadecimal SHA-256 of the exact grammar JSON bytes.
aliases Optional list of alternative names. Aliases in this list are used for both Markdown and dependency lookup.
detect Optional Rust regex matching code written in this language. Used when auto-detection is enabled.
dependencies Optional ordered list of additional registrations a language requires, such as grammar injections. The extension also follows embeddedLangs in the grammar JSON. Include all required registrations in the catalog.
$catalog = require __DIR__ . '/grammar-catalog.php';
$html = oxmark_to_html($markdown, $catalog, static function (string $name): string {
    $json = file_get_contents(__DIR__ . '/grammars/' . $name . '.json');
    if ($json === false) {
        throw new RuntimeException("Could not read grammar {$name}");
    }
    return $json;
});

The loader receives a canonical name and returns one Shiki registration object as UTF-8 JSON. The extension calls it only for grammars used by the document and their missing dependencies. Equivalent catalogs reuse loaded registrations within a PHP process. Unknown languages render as code blocks. The names text, txt, plain, and plaintext are reserved for plain text. The extension verifies each asset against its digest and reports a ValueError for invalid catalog entries or missing dependencies. Exceptions from the loader propagate to the caller.

Auto-detect unlabeled fences

Enable detection by supplying auto_detect: true. Add a detect pattern for each language the library should infer.

$catalog['rust']['detect'] = '(?m)^\s*(?:pub\s+)?fn\s+[A-Za-z_]\w*\s*\(';
$html = oxmark_to_html($markdown, $catalog, $loadGrammar, auto_detect: true);

Use regex syntax without PHP delimiters. Patterns see the code as rendered, with the fence and final newline removed. Each nonblank unlabeled fence is inferred when exactly one catalog entry matches. Fences with zero or multiple matches render as plain code. Explicit labels, including aliases, take precedence. Matching uses catalog metadata, and the loader only fetches matched grammars and their missing dependencies. Names beginning with mermaid are reserved for explicit Mermaid fences.