A super lightweight Markdown parser for PHP projects and applications.

Related tags

Markdown downmark
Overview

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.

Comments
Releases(v0.0.6)
  • v0.0.6(May 13, 2022)

    What's Changed

    • build(deps): bump dependabot/fetch-metadata from 1.3.0 to 1.3.1 by @dependabot in https://github.com/ryangjchandler/downmark/pull/3

    New Contributors

    • @dependabot made their first contribution in https://github.com/ryangjchandler/downmark/pull/3

    Full Changelog: https://github.com/ryangjchandler/downmark/compare/v0.0.5...v0.0.6

    Source code(tar.gz)
    Source code(zip)
  • v0.0.5(Apr 22, 2022)

  • v0.0.4(Apr 22, 2022)

  • v0.0.3(Apr 22, 2022)

  • v0.0.2(Mar 22, 2022)

    What's Changed

    • feature: preprocessors by @ryangjchandler in https://github.com/ryangjchandler/downmark/pull/2

    New Contributors

    • @ryangjchandler made their first contribution in https://github.com/ryangjchandler/downmark/pull/2

    Full Changelog: https://github.com/ryangjchandler/downmark/compare/v0.0.1...v0.0.2

    Source code(tar.gz)
    Source code(zip)
  • v0.0.1(Mar 21, 2022)

Owner
Ryan Chandler
Ryan Chandler
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

The League of Extraordinary Packages 2.4k Dec 29, 2022
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

Emanuil Rusev 14.3k Dec 28, 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

Aband*nthecar 16 Dec 24, 2022
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

Spatie 230 Jan 7, 2023
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

Clark Winkelmann 7 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

Gigadrive UG 3 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

The League of Extraordinary Packages 1.5k Jan 3, 2023
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

Jordan LeDoux 14 Sep 25, 2022
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

Hung Nguyen 3 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

JBZoo Toolbox 1 Dec 26, 2021
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

dflydev 173 Dec 30, 2022
PHP based Markdown documentation viewer

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

null 5 Dec 9, 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

ARCHTECH 104 Nov 12, 2022
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

PHPComLab 6 Sep 29, 2022
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

Ryan Winchester 76 Dec 30, 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

Adrien Pétremann 329 Dec 14, 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

Roave, LLC 40 Dec 14, 2022
PHP Documentation system.

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

Róbert Kelčák 1 Oct 4, 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

Michel Fortin 3.3k Jan 1, 2023
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

Michel Fortin 3.3k Jan 1, 2023