yiipress/markdown is a native PHP extension for rendering Markdown to HTML
with bundled MD4C.
Warning
This package is deprecated in favor of https://github.com/iliaal/mdparser/.
- PHP 8.2 - 8.5.
- PIE to install the extension.
- On Linux and macOS: C compiler, and
make.
Prebuilt Windows DLLs are attached to GitHub releases.
Install the extension with PIE:
pie install yiipress/markdownThen require the extension from applications that use it:
composer require ext-markdown:*Create a renderer once, then call render() for each Markdown string:
use YiiPress\Markdown\MarkdownOptions;
use YiiPress\Markdown\MarkdownRenderer;
$renderer = new MarkdownRenderer(new MarkdownOptions(
tables: true,
tasklists: true,
htmlBlocks: true,
htmlSpans: true,
));
echo $renderer->render("# Hello");MarkdownOptions is immutable. Pass options to the constructor to choose the
Markdown features accepted by the parser and the HTML renderer flags used for
output.
Defaults enable common extensions:
- tables
- strikethrough
- task lists
- URL, email, and WWW autolinks
- whitespace collapse
- hard soft breaks
- footnotes
- admonitions
- raw HTML blocks and spans
Disable raw HTML by turning off both HTML options:
$renderer = new MarkdownRenderer(new MarkdownOptions(
htmlBlocks: false,
htmlSpans: false,
));Optional MD4C extensions can be enabled individually:
$renderer = new MarkdownRenderer(new MarkdownOptions(
latexMath: true,
wikilinks: true,
spoilers: true,
superscripts: true,
subscripts: true,
highlight: true,
));Most applications should use MarkdownOptions. The extension also exposes
MD4C's raw constants when you need to inspect the generated parser bitmask,
store low-level settings, or pass HTML renderer flags directly.
YiiPress\Markdown\Flagcontains parser feature flags, such asFlag::TABLES,Flag::NO_HTML, andFlag::FOOTNOTES.YiiPress\Markdown\Dialectcontains MD4C dialect presets, such asDialect::COMMONMARKandDialect::GITHUB.YiiPress\Markdown\HtmlFlagcontains flags for MD4C's HTML renderer, such asHtmlFlag::XHTMLandHtmlFlag::VERBATIM_ENTITIES.
Use HtmlFlag values with MarkdownOptions::$rendererFlags:
use YiiPress\Markdown\HtmlFlag;
use YiiPress\Markdown\MarkdownOptions;
use YiiPress\Markdown\MarkdownRenderer;
$renderer = new MarkdownRenderer(new MarkdownOptions(
rendererFlags: HtmlFlag::XHTML | HtmlFlag::VERBATIM_ENTITIES,
));Use Flag values when you need to inspect or compare parser settings:
use YiiPress\Markdown\Flag;
use YiiPress\Markdown\MarkdownOptions;
$options = new MarkdownOptions(htmlBlocks: false, htmlSpans: false);
$parserFlags = $options->toParserFlags();
if (($parserFlags & Flag::NO_HTML) === Flag::NO_HTML) {
// Raw HTML blocks and spans are disabled.
}Use Dialect values when interoperating with code that expects MD4C's dialect
bitmasks:
use YiiPress\Markdown\Dialect;
$githubFlags = Dialect::GITHUB;Version 1 is Markdown-to-HTML only. It intentionally does not expose a PHP
callback or event wrapper around MD4C's lower-level md_parse() API.
Compared with mdparser, this package prioritizes MD4C feature coverage and
medium/large input performance. It does not add mdparser-only features such as
smart punctuation, heading anchors, source positions, nofollow links, or tag
filtering.
Run the PHPT suite in Docker:
make testShow the currently bundled MD4C revision:
make md4c-versionCheck whether the configured upstream MD4C ref has changed:
make md4c-check-upgradeUpdate the vendored MD4C files:
make md4c-upgradeBy default, MD4C targets use upstream master. Pass MD4C_REF to check or
upgrade to another branch, tag, or commit:
make md4c-upgrade MD4C_REF=release-0.5.3This package vendors MD4C source code under third_party/md4c/. MD4C is
licensed under the MIT license; its license notice is included at
third_party/md4c/LICENSE.md.
This extension was inspired by eklausme/php-md4c, an earlier PHP extension wrapper for MD4C.