A super lightweight Markdown parser for PHP projects and applications.

Last update: May 31, 2022

A speedy Markdown parser for PHP applications.

Latest Version on Packagist Tests Total Downloads

This is a super lightweight Markdown parser for PHP projects and applications. It has a rather verbose but powerful extension API for adding custom blocks and inline elements.

Features

At the moment, Downmark has support for the following Markdown blocks:

  • Headings (h1 - h6)
  • Blockquotes (multi-line support)
  • Single-level unordered and ordered lists
  • Backtick-delimited code blocks

It also has support for the following inline elements:

  • Links
  • Images
  • Bold, italic and strikethrough
  • Inline code

Installation

You can install the package via Composer:

composer require ryangjchandler/downmark

Usage

To parse a string of Markdown and compile it to HTML, do the following:

use Downmark\Downmark;

$parser = Downmark::create();

$html = $parser->parse('**Hello!**');

Block extensions

To create a custom block-level extensions, you first need to register it with the parser:

use Downmark\Blocks\Block;

Downmark::create()
    ->block("/::: (.*)/", function (array $matches): Block {

    });

The Downmark::block() methods accepts 2 arguments. The first is a regular expression, used by the parser to find the start of your block. The second is a Closure that should return an instance of Downmark\Blocks\Block.

You can create an object that extends the Downmark\Blocks\Block class. This class only requires you to implement a single public toHtml(): string method.

' . $this->content . '
'; } }">
use Downmark\Blocks\Block;

class NoticeBlock extends Block
{
    public function __construct(
        protected ?string $content = '',
    ) {}

    public function toHtml(): string
    {
        return '
' . $this->content . '
'
; } }

Inside of the extension callback, you can return an instance of NoticeBlock.

Downmark::create()
    ->block("/::: (.*)/", function (array $matches): Block {
        return new NoticeBlock($matches[1]);
    });

When the parser compiles your Markdown, it will check if this block matches and execute the callback function.

Note: Documentation on building multi-line blocks coming soon... If you're super eager, source-dive the tests to find out how it works.

Inline extensions

Downmark also provides an API for extending Markdown with custom inline elements. The example below extends Downmark to support a "mention" syntax that generates links to Twitter profiles.

%s', $matches[1], $matches[0]); });">
Downmark::create()
    // Look for any inline text that matches a single `@` character followed by 1 to 15 alphanumeric (incl. underscore) characters.
    ->inline("/@([A-Za-z0-9_]{1,15})(?!\w)/", function (array $matches): string {
        return sprintf('%s', $matches[1], $matches[0]);
    });

The callback function should return a string. This will be used to replace the regular-expression match.

Preprocessors

A preprocessor is any class that implements the Downmark\Interfaces\Preprocessor interface.

use Downmark\Interfaces\Preprocessor;
use Downmark\Blocks\Block;
use Downmark\Blocks\Paragraph;

class RickRoller implements Preprocessor
{
    public function preprocess(Block $block): Block
    {
        if (! $block instanceof Paragraph) {
            return $block;
        }

        $block->content = 'Rick rolled!';

        return $block;
    }
}

The RickRoller class above will receive each Block in the document. It can then modify the block, return a new block or leave it as it is. This preprocessor will replace the content inside of all paragraph blocks with Rick rolled!.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

GitHub

https://github.com/ryangjchandler/downmark
You might also like...

PHP Markdown & Extra

PHP Markdown & Extra An updated and stripped version of the original PHP Markdown by Michel Fortin. Works quite well with PSR-0 autoloaders and is Com

Jan 18, 2022

PHP based Markdown documentation viewer

PHP based viewer for Markdown files, to view them with fenced code highlighting and navigation.

Mar 31, 2022

Easily add routes to your Laravel app by creating Markdown or Blade files

Laravel Pages This package lets you create pages using Markdown or Blade without having to worry about creating routes or controllers yourself. Essent

Jul 6, 2022

Render colored Markdown contents on console terminal

Render colored  Markdown contents on console terminal

cli-markdown Render colored markdown contents on console terminal Preview run demo by php example/demo.php Features support auto render color on termi

Jan 27, 2022

markdown wiki/blog

markdown wiki/blog

Kwiki markdown wiki/blog Usage Place your markdown files in the /wiki directory. Categories are directories and subcategories are subdirectories. If y

May 29, 2022

Gruik ! An open-source markdown note-taking web app. [ABANDONED PROJECT]

What is Gruik ? It's a free & open-source note-taking service. A space where you can store notes, tutorials, code snippets... by writing them in markd

Mar 31, 2022

Docbook Tool for static documentation generation from Markdown files

Roave Docbook Tool Static HTML and PDF generator tool for generating documentation from Markdown files. Generates a deployable HTML file from Markdown

Aug 1, 2022

PHP Documentation system.

PHP Documentation system Simple but powerful Markdown docs. Features Search within Markdown files Customizable Twig templates (Note: default design is

Jun 8, 2022

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

PHP Markdown PHP Markdown Lib 1.9.0 - 1 Dec 2019 by Michel Fortin https://michelf.ca/ based on Markdown by John Gruber https://daringfireball.net/ Int

Aug 5, 2022

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

PHP Markdown PHP Markdown Lib 1.9.0 - 1 Dec 2019 by Michel Fortin https://michelf.ca/ based on Markdown by John Gruber https://daringfireball.net/ Int

Aug 5, 2022

A super fast, highly extensible markdown parser for PHP

A super fast, highly extensible markdown parser for PHP What is this? A set of PHP classes, each representing a Markdown flavor, and a command line to

Jul 30, 2022

a super lightweight markdown wiki/blog/tweeting system.

laMDWiki is a super light weight blog/wiki platform running on PHP!

Dec 24, 2021

A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own...

PHPMVC A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own... How to Start Clone this repo - git clone https:/

Aug 2, 2022

A super lightweight PSR-7 implementation

PSR-7 implementation A super lightweight PSR-7 implementation. Very strict and very fast. Description Guzzle Laminas Slim Nyholm Lines of code 3.300 3

Aug 12, 2022

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

Jul 5, 2021

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

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

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

Aug 6, 2022

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

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

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

Aug 6, 2022

Better Markdown Parser in PHP

Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Aug 16, 2022

Better Markdown Parser in PHP

Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Aug 11, 2022
Comments
Related tags
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

Aug 6, 2022
Better Markdown Parser in PHP
Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Aug 11, 2022
A simple regex-based Markdown parser in PHP

Slimdown A simple regex-based Markdown parser in PHP. Supports the following elements (and can be extended via Slimdown::add_rule()): Headers Links Bo

Jul 27, 2022
A highly configurable markdown renderer and Blade component for Laravel
A highly configurable markdown renderer and Blade component for Laravel

A highly configurable markdown renderer and Blade component for Laravel This package contains: a Blade component that can render markdown a highly con

Aug 13, 2022
Generate pseudo-static pages from markdown and HTML files for Flarum

Flarum Pages Generator This is not a Flarum extension. This package provides a Flarum extender that you can use in the local extend.php to define cust

Feb 21, 2022
Symfony 5 bundle to easily create dynamic subpages with Markdown. Useful for help sections and wikis.

MarkdownWikiBundle This bundle allows you to create rich subpages in a Symfony project using Markdown. Pages are stored in a file cache and sourced fr

Apr 26, 2022
Convert HTML to Markdown with PHP

HTML To Markdown for PHP Library which converts HTML to Markdown for your sanity and convenience. Requires: PHP 7.2+ Lead Developer: @colinodell Origi

Aug 11, 2022
A PHP tool to generate templateable markdown documentation from the docblocks or type-hints of your codebase.

Roster Installation To install, simply require the package using composer: composer require

Sep 8, 2021
PHP Markdown Engine Support

PHP Markdown Version v1.x support all PHP version >=5.4 v2.x support all PHP version >=7.0 Cài đặt thư viện Thư viện này được cài đặt thông qua Compos

Jul 1, 2022
Rendering markdown from PHP code

JBZoo / Markdown Installing composer require jbzoo/markdown Usage Rendering Table <?php declare(strict_types=1); use JBZoo\Markdown\Table; echo (new

Dec 26, 2021