A Fast, Simple and Straight-forward Markdown to HTML Converter for PHP.
composer require fastvolt/markdown
<?php
use FastVolt\Helper\Markdown;
$sample = " ## Hello, World ";
# init Markdown object
$mkd = Markdown::new();
# set markdown data to convert
$mkd -> setContent( $sample );
# convert data to markdown
print $mkd -> toHtml(); // <h2>Hello, World</h2>
Assuming we created a sample.md
file in /assets
folder with the following markdown contents:
file: assets/sample.md
# Topic
## Sub-topic
**Author:** __vincent__
file: index.php
<?php
use FastVolt\Helper\Markdown;
$file_directory = __DIR__ . '/assets/sample.md';
# init markdown object
$mkd = Markdown::new();
# set markdown file
$mkd -> setFile( $file_directory );
# convert to html
print $mkd -> toHtml();
// output: <h1>Topic</h1> <h2> Sub-topic</h2> <b>Author:</b> <i>vincent</i>
In order to achieve this, you need to create a folder to store the compiled markdown files.
➡️ If we've already set up directories named
pages
andfiles
with a file namedhello.md
in thefiles
directory, let's see how we can convert thehello.md
markdown file into an HTML file. Afterward, we will save the resulting HTML output in a new file namedhello.html
inpages
directory:
file: files/hello.md
### hello
file: index.php
use FastVolt\Helper\Markdown;
# convert md file to html file
$mkd = Markdown::new()
# set markdown file to compile
$mkd -> setFile( __DIR__ . '/files/hello.md' )
# set directory to store compiled html files
$mkd -> setCompileDir( __DIR__ . '/pages/' )
# convert to html
$mkd -> toHtmlFile( filename: 'hello' );
# check if markdown compile to html successfully
if ($mkd) {
print ("compile successful");
}
After above operation, you will get the following result:
file: pages/hello.html
<h3>hello</h3>
- PHP 8.1
- that's all 😇.
FastVolt's Markup Library is an extended/simplified version of Erusev's ParseDown Library.
Released under MIT License by @fastvolt.