Convert Figma logo to code with AI

erusev logoparsedown

Better Markdown Parser in PHP

14,825
1,128
14,825
164

Top Related Projects

A super fast, highly extensible markdown parser for PHP

Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.

Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

An HTML5 parser and serializer for PHP.

Quick Overview

Parsedown is a fast and extensible Markdown parser for PHP. It aims to provide a high-speed, standards-compliant implementation of Markdown with support for GitHub Flavored Markdown (GFM) and other popular extensions.

Pros

  • High performance, significantly faster than many other PHP Markdown parsers
  • Supports GitHub Flavored Markdown (GFM) out of the box
  • Easy to use and integrate into existing PHP projects
  • Actively maintained with regular updates and improvements

Cons

  • Limited customization options compared to some more complex parsers
  • Doesn't support all Markdown extensions or flavors
  • May have occasional inconsistencies with other Markdown implementations
  • Requires PHP 5.3 or later, which might be an issue for older projects

Code Examples

  1. Basic usage:
$Parsedown = new Parsedown();
echo $Parsedown->text('Hello **Parsedown**!');

This example demonstrates how to create a Parsedown instance and parse a simple Markdown string.

  1. Parsing inline Markdown:
$Parsedown = new Parsedown();
echo $Parsedown->line('This is *inline* Markdown');

This example shows how to parse inline Markdown without creating new paragraphs.

  1. Enabling safe mode:
$Parsedown = new Parsedown();
$Parsedown->setSafeMode(true);
echo $Parsedown->text('# Hello [World](https://example.com)');

This example demonstrates how to enable safe mode, which escapes HTML and removes potentially dangerous URLs.

Getting Started

To use Parsedown in your PHP project, follow these steps:

  1. Install Parsedown using Composer:

    composer require erusev/parsedown
    
  2. Include Parsedown in your PHP file:

    require 'vendor/autoload.php';
    
    $Parsedown = new Parsedown();
    $html = $Parsedown->text('# Hello, Parsedown!');
    
    echo $html;
    

This will output the parsed HTML:

<h1>Hello, Parsedown!</h1>

You can now use the $Parsedown->text() method to parse Markdown strings into HTML in your PHP application.

Competitor Comparisons

A super fast, highly extensible markdown parser for PHP

Pros of Markdown

  • Offers multiple parsing flavors (GFM, traditional, extra)
  • Provides more extensive configuration options
  • Supports parsing into an abstract syntax tree (AST)

Cons of Markdown

  • Slightly slower parsing speed
  • Less actively maintained (fewer recent updates)
  • May require more setup for basic usage

Code Comparison

Parsedown:

$parsedown = new Parsedown();
$html = $parsedown->text('# Hello, World!');

Markdown:

$parser = new \cebe\markdown\GithubMarkdown();
$html = $parser->parse('# Hello, World!');

Both libraries offer simple usage for basic Markdown parsing. However, Markdown provides more flexibility with different flavors:

$parser = new \cebe\markdown\Markdown(); // Traditional Markdown
$parser = new \cebe\markdown\GithubMarkdown(); // GitHub Flavored Markdown
$parser = new \cebe\markdown\MarkdownExtra(); // Markdown Extra

This allows developers to choose the most appropriate parsing style for their specific needs, whereas Parsedown focuses on a single, optimized implementation.

Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.

Pros of PHP Markdown

  • More extensive feature set, including support for extra syntax extensions
  • Longer development history and established reputation in the PHP community
  • Offers multiple parsing modes (fast, regular, extra) for different use cases

Cons of PHP Markdown

  • Generally slower performance compared to Parsedown
  • Less frequent updates and maintenance
  • More complex codebase, potentially harder to contribute to or customize

Code Comparison

PHP Markdown:

$parser = new Michelf\Markdown();
$html = $parser->transform($markdown);

Parsedown:

$parser = new Parsedown();
$html = $parser->text($markdown);

Both libraries offer simple APIs for converting Markdown to HTML, but Parsedown's method is slightly more concise.

Key Differences

  • Parsedown is known for its speed and simplicity, making it a good choice for projects where performance is crucial
  • PHP Markdown offers more features and compatibility with various Markdown flavors, suitable for projects requiring advanced Markdown support
  • Parsedown has a more active development cycle with frequent updates
  • PHP Markdown provides more granular control over parsing options

Use Cases

  • Choose Parsedown for projects prioritizing speed and simplicity
  • Opt for PHP Markdown when advanced Markdown features or specific syntax extensions are needed
  • Consider PHP Markdown for legacy projects or those requiring strict CommonMark compliance

Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

Pros of CommonMark

  • More compliant with the CommonMark specification
  • Offers a more extensible architecture for custom syntax and renderers
  • Provides better support for advanced Markdown features like tables and task lists

Cons of CommonMark

  • Generally slower parsing speed compared to Parsedown
  • Steeper learning curve for customization and extension
  • Larger codebase and potentially higher memory usage

Code Comparison

Parsedown:

$parsedown = new Parsedown();
$html = $parsedown->text('# Hello, world!');

CommonMark:

use League\CommonMark\CommonMarkConverter;

$converter = new CommonMarkConverter();
$html = $converter->convertToHtml('# Hello, world!');

Both libraries offer simple ways to convert Markdown to HTML, but CommonMark provides more options for configuration and customization:

$config = [
    'html_input' => 'strip',
    'allow_unsafe_links' => false,
];
$converter = new CommonMarkConverter($config);

CommonMark's extensibility allows for easy addition of custom syntax:

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;

$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new TableExtension());

An HTML5 parser and serializer for PHP.

Pros of html5-php

  • Full HTML5 parsing and serialization capabilities
  • Supports DOM manipulation and traversal
  • More comprehensive HTML handling, including error correction

Cons of html5-php

  • Larger library size and potentially higher resource usage
  • Steeper learning curve due to more complex API
  • May be overkill for simple HTML processing tasks

Code Comparison

Parsedown:

$parsedown = new Parsedown();
$html = $parsedown->text('# Hello, world!');

html5-php:

$html = <<< EOD
<html><body><h1>Hello, world!</h1></body></html>
EOD;
$dom = HTML5::loadHTML($html);
$h1 = $dom->getElementsByTagName('h1')->item(0);

Summary

Parsedown is a lightweight Markdown parser, ideal for simple conversions. html5-php is a full-featured HTML5 parser and serializer, better suited for complex HTML manipulation tasks. Parsedown is easier to use for basic Markdown-to-HTML conversion, while html5-php offers more power and flexibility for working with HTML documents but requires more setup and knowledge to use effectively.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Parsedown

Total Downloads Version License

Better Markdown Parser in PHP — demo

Features

Installation

Install the composer package:

composer require erusev/parsedown

Or download the latest release and include Parsedown.php

Example

$Parsedown = new Parsedown();

echo $Parsedown->text('Hello _Parsedown_!'); # prints: <p>Hello <em>Parsedown</em>!</p>

You can also parse inline markdown only:

echo $Parsedown->line('Hello _Parsedown_!'); # prints: Hello <em>Parsedown</em>!

More examples in the wiki and in this video tutorial.

Security

Parsedown is capable of escaping user-input within the HTML that it generates. Additionally Parsedown will apply sanitisation to additional scripting vectors (such as scripting link destinations) that are introduced by the markdown syntax itself.

To tell Parsedown that it is processing untrusted user-input, use the following:

$Parsedown->setSafeMode(true);

If instead, you wish to allow HTML within untrusted user-input, but still want output to be free from XSS it is recommended that you make use of a HTML sanitiser that allows HTML tags to be whitelisted, like HTML Purifier.

In both cases you should strongly consider employing defence-in-depth measures, like deploying a Content-Security-Policy (a browser security feature) so that your page is likely to be safe even if an attacker finds a vulnerability in one of the first lines of defence above.

Safe mode does not necessarily yield safe results when using extensions to Parsedown. Extensions should be evaluated on their own to determine their specific safety against XSS.

Escaping HTML

WARNING: This method is not safe from XSS!

If you wish to escape HTML in trusted input, you can use the following:

$Parsedown->setMarkupEscaped(true);

Beware that this still allows users to insert unsafe scripting vectors, ex: [xss](javascript:alert%281%29).

Questions

How does Parsedown work?

It tries to read Markdown like a human. First, it looks at the lines. It’s interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line starts with a - then perhaps it belongs to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines).

We call this approach "line based". We believe that Parsedown is the first Markdown parser to use it. Since the release of Parsedown, other developers have used the same approach to develop other Markdown parsers in PHP and in other languages.

Is it compliant with CommonMark?

It passes most of the CommonMark tests. Most of the tests that don't pass deal with cases that are quite uncommon. Still, as CommonMark matures, compliance should improve.

Who uses it?

Laravel Framework, Bolt CMS, Grav CMS, Herbie CMS, Kirby CMS, October CMS, Pico CMS, Statamic CMS, phpDocumentor, RaspberryPi.org, Symfony Demo and more.

How can I help?

Use it, star it, share it and if you feel generous, donate.

What else should I know?

I also make Nota — a notes app designed for local Markdown files.