A fast, simple, and straightforward Markdown to HTML converter for PHP.
composer require fastvolt/markdown
use FastVolt\Helper\Markdown;
$text = "## Hello, World";
// initialize markdown object
$markdown = Markdown::new();
// set markdown content
$markdown->setContent($text);
// compile as raw HTML
echo $markdown->toHtml();
<h2>Hello, World</h2>
sample.md:
#### Heading 4
### Heading 3
## Heading 2
# Heading 1
- List 1
- List 2
> THIS IS A BLOCKQUOTE
[A LINK](https://github.com/fastvolt)
index.php:
$markdown = Markdown::new();
// set markdown file to parse
$markdown->setFile('./sample.md');
// compile as raw HTML
echo $markdown->toHtml();
Output:
<h4>Heading 4</h4>
<h3>Heading 3</h3>
<h2>Heading 2</h2>
<h1>Heading 1</h1>
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
<blockquote><p>THIS IS A BLOCKQUOTE</p></blockquote>
<a href="https://github.com/fastvolt">A LINK</a>
blogPost.md:
Here is a Markdown File Waiting To Be Compiled To an HTML File
index.php:
$markdown = Markdown::new()
// set markdown file
->setFile(__DIR__ . '/blogPost.md')
// set compilation directory
->setCompileDir(__DIR__ . '/pages/')
// compile as an html file named 'newHTMLFile.html'
->toHtmlFile(filename: 'newHTMLFile');
if ($markdown) {
echo "Compiled to ./pages/newHTMLFile.html";
}
You can sanitize input HTML and prevent cross-site scripting (XSS) attack using the sanitize
flag:
$markdown = Markdown::new(
sanitize: true
);
$markdown->setContent('<h1>Hello World</h1>');
echo $markdown->toHtml();
Output:
<p><h1>Hello World</h1></p>
$markdown = Markdown::new();
$markdown->setInlineContent('_My name is **vincent**, the co-author of this blog_');
echo $markdown->ToHtml();
Output:
<i>My name is <strong>vincent</strong>, the co-author of this blog</i>
NOTE: Some markdown symbols are not supported with this method
Combine multiple markdown files, contents and compile them in multiple directories:
Header.md
# Blog Title
### Here is the Blog Sub-title
Footer.md
### Thanks for Visiting My BlogPage
index.php
$markdown = Markdown::new(sanitize: true)
// include header file's markdown contents
->setFile('./Header.md')
// body contents
->setInlineContent('_My name is **vincent**, the co-author of this blog_')
->setContent('Kindly follow me on my GitHub page via: [@vincent](https://github.com/oladoyinbov).')
->setContent('Here are the lists of my projects:')
->setContent('
- Dragon CMS
- Fastvolt Framework.
+ Fastvolt Router
+ Markdown Parser.
')
// include footer file's markdown contents
->setFile('./Footer.md')
// set compilation directory
->setCompileDir('./pages/')
// set another compilation directory to backup the result
->setCompileDir('./backup/pages/')
// compile and store as 'homepage.html'
->toHtmlFile(file_name: 'homepage');
if ($markdown) {
echo "Compile Successful";
}
Output:
pages/homepage.html
,backup/pages/homepage.html
<h1>Blog Title</h1>
<h3>Here is the Blog Sub-title</h3>
<i>My name is <strong>vincent</strong>, the co-author of this blog</i>
<p>Kindly follow me on my github page via: <a href="https://github.com/oladoyinbov">@vincent</a>.</p>
<p>Here are the lists of my projects:</p>
<ul>
<li>Dragon CMS</li>
<li>Fastvolt Framework.
<ul>
<li>Fastvolt Router</li>
<li>Markdown Parser.</li>
</ul>
</li>
</ul>
<h3>Thanks for Visiting My BlogPage</h3>
Markdown Syntax | Description | Example Syntax | Rendered Output |
---|---|---|---|
# to ###### |
Headings (H1βH6) | ## Heading 2 |
|
**text** or __text__ |
Bold | **bold** |
bold |
*text* or _text_ |
Italic | *italic* |
italic |
~~text~~ |
Strikethrough | ~~strike~~ |
|
`code` |
Inline code | `echo` |
echo |
|
Code block | ```php\n echo "Hi"; \n``` |
<pre><code>...</code></pre> |
- , + , or * |
Unordered list | - Item 1 * Item 2 |
<ul><li>Item</li></ul> |
1. 2. |
Ordered list | 1. Item 2. Item |
<ol><li>Item</li></ol> |
[text](url) |
Hyperlink | [GitHub](https://github.com) |
GitHub |
> blockquote |
Blockquote | > This is a quote |
This is a quote |
--- , *** , ___ |
Horizontal Rule | --- |
<hr> |
 |
Image |  |
<img src="logo.png" alt="Logo"> |
\ |
Escape special character | \*not italic\* |
not italic (as text) |
PHP 8.1 or newer.
This library is an extended and simplified version of the excellent Parsedown by Erusev.
This project is open-source and licensed under the MIT License by @fastvolt.