Skip to content

fastvolt/markdown

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

63 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fastvolt

Markdown Parser for PHP

A fast, simple, and straightforward Markdown to HTML converter for PHP.

PHP Composer License: MIT GitHub Issues Repo Maintained: Yes

πŸš€ Installation

composer require fastvolt/markdown

πŸ“¦ Basic Usage

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();

Output:

<h2>Hello, World</h2>

πŸ“„ Convert Markdown File to Raw HTML

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>

πŸ“ Compile Markdown to HTML File

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";
}

πŸ”’ Sanitizing HTML Output (XSS Protection)

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>&lt;h1&gt;Hello World&lt;/h1&gt;</p>

βš™οΈ Advanced Use Case

Inline Markdown

$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


Example #1

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>

Supported Formatting Symbols

Markdown Syntax Description Example Syntax Rendered Output
# to ###### Headings (H1–H6) ## Heading 2

Heading 2

**text** or __text__ Bold **bold** bold
*text* or _text_ Italic *italic* italic
~~text~~ Strikethrough ~~strike~~ strike
`code` Inline code `echo` echo
<br>code block<br> 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>
![alt](image.jpg) Image ![Logo](logo.png) <img src="logo.png" alt="Logo">
\ Escape special character \*not italic\* not italic (as text)

βœ… Requirements

PHP 8.1 or newer.


ℹ️ Notes

This library is an extended and simplified version of the excellent Parsedown by Erusev.


πŸ“„ License

This project is open-source and licensed under the MIT License by @fastvolt.

About

A Fast, Simple and Straight-forward Markdown to HTML Converter for PHP.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •