colinodell / commonmark-php
Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)
Fund package maintenance!
colinodell
Tidelift
www.colinodell.com/sponsor
www.paypal.me/colinpodell/10.00
Installs: 2 358
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2 727
Watchers: 35
Forks: 192
Open Issues: 25
Requires
- php: ^7.4 || ^8.0
- ext-mbstring: *
- league/config: ^1.1.1
- psr/event-dispatcher: ^1.0
- symfony/deprecation-contracts: ^2.1 || ^3.0
- symfony/polyfill-php80: ^1.16
Requires (Dev)
- ext-json: *
- cebe/markdown: ^1.0
- commonmark/cmark: 0.31.1
- commonmark/commonmark.js: 0.31.1
- composer/package-versions-deprecated: ^1.8
- embed/embed: ^4.4
- erusev/parsedown: ^1.0
- github/gfm: 0.29.0
- michelf/php-markdown: ^1.4 || ^2.0
- nyholm/psr7: ^1.5
- phpstan/phpstan: ^1.8.2
- phpunit/phpunit: ^9.5.21 || ^10.5.9 || ^11.0.0
- scrutinizer/ocular: ^1.8.1
- symfony/finder: ^5.3 | ^6.0 || ^7.0
- symfony/yaml: ^2.3 | ^3.0 | ^4.0 | ^5.0 | ^6.0 || ^7.0
- unleashedtech/php-coding-standard: ^3.1.1
- vimeo/psalm: ^4.24.0 || ^5.0.0
Suggests
- symfony/yaml: v2.3+ required if using the Front Matter extension
- dev-main / 2.6.x-dev
- 2.5.x-dev
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.x-dev
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.x-dev
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.x-dev
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.x-dev
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.x-dev
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-rc2
- 2.0.0-rc1
- 2.0.0-beta3
- 2.0.0-beta2
- 2.0.0-beta1
- 1.6.x-dev
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.x-dev
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.x-dev
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.x-dev
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.x-dev
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.0
- 1.0.0-rc1
- 1.0.0-beta4
- 1.0.0-beta3
- 1.0.0-beta2
- 1.0.0-beta1
- 0.19.x-dev
- 0.19.3
- 0.19.2
- 0.19.1
- 0.19.0
- 0.18.5
- 0.18.4
- 0.18.3
- 0.18.2
- 0.18.1
- 0.18.0
- 0.17.5
- 0.17.4
- 0.17.3
- 0.17.2
- 0.17.1
- 0.17.0
- 0.16.0
- 0.15.7
- 0.15.6
- 0.15.5
- 0.15.4
- 0.15.3
- 0.15.2
- 0.15.1
- 0.15.0
- 0.14.0
- 0.13.4
- 0.13.3
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.0
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-optimizations
This package is auto-updated.
Last update: 2024-10-04 00:07:57 UTC
README
league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and GitHub-Flavored Markdown. It is based on the CommonMark JS reference implementation by John MacFarlane (@jgm).
๐ฆ Installation & Basic Usage
This project requires PHP 7.4 or higher with the mbstring
extension. To install it via Composer simply run:
$ composer require league/commonmark
The CommonMarkConverter
class provides a simple wrapper for converting CommonMark to HTML:
use League\CommonMark\CommonMarkConverter; $converter = new CommonMarkConverter([ 'html_input' => 'strip', 'allow_unsafe_links' => false, ]); echo $converter->convert('# Hello World!'); // <h1>Hello World!</h1>
Or if you want GitHub-Flavored Markdown, use the GithubFlavoredMarkdownConverter
class instead:
use League\CommonMark\GithubFlavoredMarkdownConverter; $converter = new GithubFlavoredMarkdownConverter([ 'html_input' => 'strip', 'allow_unsafe_links' => false, ]); echo $converter->convert('# Hello World!'); // <h1>Hello World!</h1>
Please note that only UTF-8 and ASCII encodings are supported. If your Markdown uses a different encoding please convert it to UTF-8 before running it through this library.
Caution
If you will be parsing untrusted input from users, please consider setting the html_input
and allow_unsafe_links
options per the example above. See https://commonmark.thephpleague.com/security/ for more details. If you also do choose to allow raw HTML input from untrusted users, consider using a library (like HTML Purifier) to provide additional HTML filtering.
๐ Documentation
Full documentation on advanced usage, configuration, and customization can be found at commonmark.thephpleague.com.
โซ Upgrading
Information on how to upgrade to newer versions of this library can be found at https://commonmark.thephpleague.com/releases.
๐ป GitHub-Flavored Markdown
The GithubFlavoredMarkdownConverter
shown earlier is a drop-in replacement for the CommonMarkConverter
which adds additional features found in the GFM spec:
- Autolinks
- Disallowed raw HTML
- Strikethrough
- Tables
- Task Lists
See the Extensions documentation for more details on how to include only certain GFM features if you don't want them all.
๐๏ธ Related Packages
Integrations
- CakePHP 3
- Drupal
- Laravel 4+
- Sculpin
- Symfony 2 & 3
- Symfony 4
- Twig Markdown extension
- Twig filter and tag
- Laravel CommonMark Blog
Included Extensions
See our extension documentation for a full list of extensions bundled with this library.
Community Extensions
Custom parsers/renderers can be bundled into extensions which extend CommonMark. Here are some that you may find interesting:
- Emoji extension - UTF-8 emoji extension with Github tag.
- Sup Sub extensions - Adds support of superscript and subscript (
<sup>
and<sub>
HTML tags) - YouTube iframe extension - Replaces youtube link with iframe.
- Lazy Image extension - Adds various options for lazy loading of images.
- Marker Extension - Adds support of highlighted text (
<mark>
HTML tag)
Others can be found on Packagist under the commonmark-extension
package type.
If you build your own, feel free to submit a PR to add it to this list!
Others
Check out the other cool things people are doing with league/commonmark
: https://packagist.org/packages/league/commonmark/dependents
๐ท๏ธ Versioning
SemVer is followed closely. Minor and patch releases should not introduce breaking changes to the codebase; however, they might change the resulting AST or HTML output of parsed Markdown (due to bug fixes, spec changes, etc.) As a result, you might get slightly different HTML, but any custom code built onto this library should still function correctly.
Any classes or methods marked @internal
are not intended for use outside of this library and are subject to breaking changes at any time, so please avoid using them.
๐ ๏ธ Maintenance & Support
When a new minor version (e.g. 2.0
-> 2.1
) is released, the previous one (2.0
) will continue to receive security and critical bug fixes for at least 3 months.
When a new major version is released (e.g. 1.6
-> 2.0
), the previous one (1.6
) will receive critical bug fixes for at least 3 months and security updates for 6 months after that new release comes out.
(This policy may change in the future and exceptions may be made on a case-by-case basis.)
Professional support, including notification of new releases and security updates, is available through a Tidelift Subscription.
๐ทโโ๏ธ Contributing
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure with us.
If you encounter a bug in the spec, please report it to the CommonMark project. Any resulting fix will eventually be implemented in this project as well.
Contributions to this library are welcome, especially ones that:
- Improve usability or flexibility without compromising our ability to adhere to the CommonMark spec
- Mirror fixes made to the reference implementation
- Optimize performance
- Fix issues with adhering to the CommonMark spec
Major refactoring to core parsing logic should be avoided if possible so that we can easily follow updates made to the reference implementation. That being said, we will absolutely consider changes which don't deviate too far from the reference spec or which are favored by other popular CommonMark implementations.
Please see CONTRIBUTING for additional details.
๐งช Testing
$ composer test
This will also test league/commonmark against the latest supported spec.
๐ Performance Benchmarks
You can compare the performance of league/commonmark to other popular parsers by running the included benchmark tool:
$ ./tests/benchmark/benchmark.php
๐ฅ Credits & Acknowledgements
This code is partially based on the CommonMark JS reference implementation which is written, maintained and copyrighted by John MacFarlane. This project simply wouldn't exist without his work.
Sponsors
We'd also like to extend our sincere thanks the following sponsors who support ongoing development of this project:
- Tidelift for offering support to both the maintainers and end-users through their professional support program
- Blackfire for providing an Open-Source Profiler subscription
- JetBrains for supporting this project with complimentary PhpStorm licenses
Are you interested in sponsoring development of this project? See https://www.colinodell.com/sponsor for a list of ways to contribute.
๐ License
league/commonmark is licensed under the BSD-3 license. See the LICENSE
file for more details.
๐๏ธ Governance
This project is primarily maintained by Colin O'Dell. Members of the PHP League Leadership Team may occasionally assist with some of these duties.
๐บ๏ธ Who Uses It?
This project is used by Drupal, Laravel Framework, Cachet, Firefly III, Neos, Daux.io, and more!
Get professional support for league/commonmark with a Tidelift subscriptionTidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.