PHPackages                             helgesverre/markdown - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. helgesverre/markdown

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

helgesverre/markdown
====================

A dirty-tricks Markdown parser (PHP FFI -&gt; md4c).

v0.3.0(1mo ago)04↓85.7%MITCPHP ^8.5CI passing

Since Jun 5Pushed 1mo agoCompare

[ Source](https://github.com/HelgeSverre/markdown)[ Packagist](https://packagist.org/packages/helgesverre/markdown)[ RSS](/packages/helgesverre-markdown/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (6)Versions (4)Used By (0)

helgesverre/markdown
====================

[](#helgesverremarkdown)

[![CI](https://github.com/HelgeSverre/markdown/actions/workflows/ci.yml/badge.svg)](https://github.com/HelgeSverre/markdown/actions/workflows/ci.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/374dbdb6e7dfc57b5a735c07938edf1b5c5909555c3b20339871fa9d4116fd23/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656c67657376657272652f6d61726b646f776e2e737667)](https://packagist.org/packages/helgesverre/markdown)[![Total Downloads](https://camo.githubusercontent.com/3415dd07b15d99b17f495f2b727566911c9dc10297dadb0577b7b50a39981e45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656c67657376657272652f6d61726b646f776e2e737667)](https://packagist.org/packages/helgesverre/markdown)[![License](https://camo.githubusercontent.com/4ad63949f717b8c4dafa418ce1b9e1232b7c0e1e92825b3c037fc0d6291799fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f68656c67657376657272652f6d61726b646f776e2e737667)](https://packagist.org/packages/helgesverre/markdown)

A fast PHP Markdown parser backed by [md4c](https://github.com/mity/md4c) through PHP FFI.

It renders GitHub-flavored Markdown, supports front matter and heading TOCs, and ships prebuilt native libraries so normal installs do not need a C compiler.

Install
-------

[](#install)

```
composer require helgesverre/markdown
```

Requirements:

- PHP 8.5+
- `ext-ffi`
- `ffi.enable=1` for web/FPM use, or an opcache preload setup

Bundled native artifacts are selected at runtime:

PlatformArtifactmacOS Apple Silicon + Intel`lib/darwin/libmd4cshim.dylib`Linux x86-64`lib/linux-x86_64/libmd4cshim.so`Linux aarch64`lib/linux-aarch64/libmd4cshim.so`Windows x64`lib/windows-x86_64/md4cshim.dll``HelgeSverre\Markdown\Ffi\Library::path()` resolves libraries in this order:

1. `$MARKDOWN_FFI_LIB`
2. the bundled `lib//` binary
3. a local `native/` build

Usage
-----

[](#usage)

### Render HTML

[](#render-html)

```
use HelgeSverre\Markdown\Markdown;

$html = Markdown::toHtml("# Hello\n\n- a\n- b\n");

$htmls = Markdown::toHtmlBatch([
    "# One\n",
    "# Two\n",
]);
```

`toHtml()` is the fast path: Markdown in, HTML out. `toHtmlBatch()` packs many documents into one native call and renders them across a C thread pool where pthreads are available.

For explicit lifecycle and options, construct the parser directly:

```
use HelgeSverre\Markdown\Data\Dialect;
use HelgeSverre\Markdown\Parser;

$parser = new Parser(
    dialect: Dialect::GitHub,
    safe: false,
    xhtml: false,
);

$html = $parser->toHtml("# Hello\n");
```

### Parse Documents

[](#parse-documents)

`parse()` strips YAML front matter, renders the body, injects GitHub-style heading ids, and returns a `ParsedMarkdown` value with HTML, front matter, and TOC data.

```
use HelgeSverre\Markdown\Markdown;

$doc =  'Hello World', 'tags' => ['php', 'markdown']]
$result->toc;         // [['level' => 1, 'text' => 'Introduction', 'slug' => 'introduction'], ...]
(string) $result;     // same as $result->html
```

Malformed front matter degrades to an empty array. Heading ids are lower-cased, ASCII-folded, and de-duplicated with suffixes like `intro-1`.

Front matter is decoded by a vendored [libyaml](https://github.com/yaml/libyaml) FFI path (parsed to JSON in C, then `json_decode`d) — no pure-PHP YAML parser is involved. Inputs libyaml's walker does not support — anchors/aliases and `
