Overview
A clean, zero-dependency Markdown-to-HTML converter for HolyPHP.
bundled — works offline
zero dependencies
md
html
Quick start
import "markdown";
$md = "# Hello HolyPHP\n\nThis is **bold**, *italic*, and `inline code`.\n\n- First item\n- Second item";
$html = markdown_to_html($md);
println($html);
Using MarkdownParser directly for custom configuration:
import "markdown";
$parser = new MarkdownParser(true, true, 65536);
$html = $parser->parse("> Welcome to [HolyPHP](https://holyphp.dev)!");
println($html);
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install markdown
This adds the library to hphp.yaml, installs it into .hphp/packages/markdown/ and pins the exact version in hphp.lock:
require:
markdown: "^1.0"
Use it
import "markdown";
markdown is bundled with the compiler, so import "markdown"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install markdown # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install markdown
Documentation
Features
- Headings (
# through ###### -> <h1> through <h6>)
- Emphasis: bold (
**text**, __text__), italic (*text*, _text_), and bold+italic (***text***, ___text___)
- Inline code (
`code` -> <code>...</code>)
- Fenced code blocks (
``` and ~~~, with optional language class language-<lang>)
- Lists: unordered (
-, *, +) and ordered (1., 1))
- Links (
[label](url)) with automatic URL sanitization (javascript:, vbscript:, data: blocked by default)
- Blockquotes (
> ...), including nested blockquotes
- Horizontal rules (
---, ***, ___)
- HTML escaping enabled by default to prevent XSS
- Backslash escapes (
\*, \_, \`, \[, etc.)
API Reference
Functions
markdown_to_html(string $markdown, bool $escapeHtml = true): string Converts a Markdown string to HTML using a default MarkdownParser.
markdown_parse_inline(string $text, bool $escapeHtml = true): string Parses inline Markdown syntax (bold, italic, inline code, links, escapes) without wrapping in block tags.
markdown_escape_html(string $text): string Escapes &, <, >, ", and ' into HTML entities.
Class MarkdownParser
new MarkdownParser(bool $escapeHtml = true, bool $sanitizeUrls = true, int $maxInputLength = 1048576) Creates a new parser instance.
$escapeHtml: Escape raw HTML characters in text (default: true).
$sanitizeUrls: Strip dangerous URL schemes like javascript:, vbscript:, and data: in links (default: true).
$maxInputLength: Maximum allowed input size in bytes (default: 1048576).
parse(string $markdown): string Converts a Markdown document to HTML.
toHtml(string $markdown): string Alias for parse(string $markdown): string.
parseInline(string $text): string Parses inline Markdown elements.
escapeHtml(string $text): string Escapes HTML special characters.
setEscapeHtml(bool $escape): void Enables or disables HTML escaping.
setSanitizeUrls(bool $sanitize): void Enables or disables link URL sanitization.
setMaxInputLength(int $maxLength): void Updates the maximum allowed input length.
Error Handling
Invalid configuration or oversized inputs throw an Exception prefixed with markdown::
markdown: maxInputLength must be a positive integer — thrown when constructing MarkdownParser or calling setMaxInputLength() with <= 0.
markdown: input length <N> exceeds maximum allowed length of <M> — thrown when parse() receives input larger than maxInputLength.
try {
$parser = new MarkdownParser(true, true, 0);
} catch (Exception $e) {
println($e->getMessage());
}
Examples
01Quick start
Bundled with the compiler — import "markdown"; works offline in any file, or add it to a project with hphp install markdown.
import "markdown";
$md = "# Hello HolyPHP\n\nThis is **bold**, *italic*, and `inline code`.\n\n- First item\n- Second item";
$html = markdown_to_html($md);
println($html);
Using MarkdownParser directly for custom configuration:
import "markdown";
$parser = new MarkdownParser(true, true, 65536);
$html = $parser->parse("> Welcome to [HolyPHP](https://holyphp.dev)!");
println($html);
Try ithphp install markdown
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_markdown.hphp.