league/commonmark
Composer 安装命令:
composer require league/commonmark
包简介
Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)
README 文档
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). - Pygments Highlighter extension - Adds support for highlighting code with the Pygments library.
- LatexRenderer extension - For rendering Markdown to LaTeX.
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 was originally 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.
And a huge thanks to all of our amazing contributors:
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
- Anthropic for providing complimentary access to Claude Max through their Open Source Program
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.
league/commonmark 适用场景与选型建议
league/commonmark 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 442.25M 次下载、GitHub Stars 达 2.97k, 最近一次更新时间为 2014 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「markdown」 「github」 「md」 「gfm」 「commonmark」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 league/commonmark 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 league/commonmark 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 league/commonmark 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Handles basic OAuth/OAuth2 authentication along with classes for common services
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
An MT940 bank statement parser for PHP
Adds more BBCode
统计信息
- 总下载量: 442.25M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3007
- 点击次数: 33
- 依赖项目数: 795
- 推荐数: 46
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-08
