michelf / php-markdown

Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.
http://michelf.ca/projects/php-markdown/
Other
3.43k stars 528 forks source link

PHP Markdown

ci.yml

by Michel Fortin
https://michelf.ca/

based on Markdown by John Gruber
https://daringfireball.net/

Introduction

This is a library package that includes the PHP Markdown parser and its sibling PHP Markdown Extra with additional features.

Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).

"Markdown" is actually two things: a plain text markup syntax, and a software tool, originally written in Perl, that converts the plain text markup to HTML. PHP Markdown is a port to PHP of the original Markdown program by John Gruber.

Requirement

This library package requires PHP 7.4 or later.

Note: The older plugin/library hybrid package for PHP Markdown and PHP Markdown Extra is no longer maintained but will work with PHP 4.0.5 and later.

You might need to set pcre.backtrack_limit higher than 1 000 000 (the default), though the default is usually fine.

Usage

To use this library with Composer, first install it with:

$ composer require michelf/php-markdown

Then include Composer's generated vendor/autoload.php to enable autoloading:

require 'vendor/autoload.php';

Without Composer, for autoloading to work, your project needs an autoloader compatible with PSR-4 or PSR-0. See the included Readme.php file for a minimal autoloader setup. (If you cannot use autoloading, see below.)

With class autoloading in place:

use Michelf\Markdown;
$my_html = Markdown::defaultTransform($my_text);

Markdown Extra syntax is also available the same way:

use Michelf\MarkdownExtra;
$my_html = MarkdownExtra::defaultTransform($my_text);

If you wish to use PHP Markdown with another text filter function built to parse HTML, you should filter the text after the transform function call. This is an example with PHP SmartyPants:

use Michelf\Markdown, Michelf\SmartyPants;
$my_html = Markdown::defaultTransform($my_text);
$my_html = SmartyPants::defaultTransform($my_html);

All these examples are using the static defaultTransform static function found inside the parser class. If you want to customize the parser configuration, you can also instantiate it directly and change some configuration variables:

use Michelf\MarkdownExtra;
$parser = new MarkdownExtra;
$parser->fn_id_prefix = "post22-";
$my_html = $parser->transform($my_text);

To learn more, see the full list of configuration variables.

Usage without an autoloader

If you cannot use class autoloading, you can still use include or require to access the parser. To load the Michelf\Markdown parser, do it this way:

require_once 'Michelf/Markdown.inc.php';

Or, if you need the Michelf\MarkdownExtra parser:

require_once 'Michelf/MarkdownExtra.inc.php';

While the plain .php files depend on autoloading to work correctly, using the .inc.php files instead will eagerly load the dependencies that would be loaded on demand if you were using autoloading.

Public API and Versioning Policy

Version numbers are of the form major.minor.patch.

The public API of PHP Markdown consist of the two parser classes Markdown and MarkdownExtra, their constructors, the transform and defaultTransform functions and their configuration variables. The public API is stable for a given major version number. It might get additions when the minor version number increments.

Protected members are not considered public API. This is unconventional and deserves an explanation. Incrementing the major version number every time the underlying implementation of something changes is going to give nonessential version numbers for the vast majority of people who just use the parser. Protected members are meant to create parser subclasses that behave in different ways. Very few people create parser subclasses. I don't want to discourage it by making everything private, but at the same time I can't guarantee any stable hook between versions if you use protected members.

Syntax changes will increment the minor number for new features, and the patch number for small corrections. A new feature is something that needs a change in the syntax documentation. Note that since PHP Markdown Lib includes two parsers, a syntax change for either of them will increment the minor number. Also note that there is nothing perfectly backward-compatible with the Markdown syntax: all inputs are always valid, so new features always replace something that was previously legal, although generally nonsensical to do.

Bugs

To file bug reports please send email to: michel.fortin@michelf.ca

Please include with your report: (1) the example input; (2) the output you expected; (3) the output PHP Markdown actually produced.

If you have a problem where Markdown gives you an empty result, first check that the backtrack limit is not too low by running php --info | grep pcre. See Installation and Requirement above for details.

Development and Testing

Pull requests for fixing bugs are welcome. Proposed new features are going to be meticulously reviewed -- taking into account backward compatibility, potential side effects, and future extensibility -- before deciding on acceptance or rejection.

If you make a pull request that includes changes to the parser please add tests for what is being changed to the test/ directory. This can be as simple as adding a .text (input) file with a corresponding .xhtml (output) file to proper category under ./test/resources/.

Traditionally tests were in a separate repository, MDTest but they are now located here, alongside the source code.

Donations

If you wish to make a donation that will help me devote more time to PHP Markdown, please visit michelf.ca/donate.

Version History

PHP Markdown Lib 2.0.0 (26 Sep 2022)

PHP Markdown Lib 1.9.1 (23 Nov 2021)

PHP Markdown Lib 1.9.0 (1 Dec 2019)

PHP Markdown Lib 1.8.0 (14 Jan 2018)

PHP Markdown Lib 1.7.0 (29 Oct 2016)

PHP Markdown Lib 1.6.0 (23 Dec 2015)

Note: this version was incorrectly released as 1.5.1 on Dec 22, a number that contradicted the versioning policy.

PHP Markdown Lib 1.5.0 (1 Mar 2015)

PHP Markdown Lib 1.4.1 (4 May 2014)

PHP Markdown Lib 1.4.0 (29 Nov 2013)

PHP Markdown Lib 1.3 (11 Apr 2013)

This is the first release of PHP Markdown Lib. This package requires PHP version 5.3 or later and is designed to work with PSR-0 autoloading and, optionally with Composer. Here is a list of the changes since PHP Markdown Extra 1.2.6:

Copyright and License

PHP Markdown Lib Copyright (c) 2004-2022 Michel Fortin https://michelf.ca/
All rights reserved.

Based on Markdown
Copyright (c) 2003-2005 John Gruber
https://daringfireball.net/
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.