You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
12 KiB

3 years ago
  1. # league/commonmark
  2. [![Latest Version](https://img.shields.io/packagist/v/league/commonmark.svg?style=flat-square)](https://packagist.org/packages/league/commonmark)
  3. [![Total Downloads](https://img.shields.io/packagist/dt/league/commonmark.svg?style=flat-square)](https://packagist.org/packages/league/commonmark)
  4. [![Software License](https://img.shields.io/badge/License-BSD--3-brightgreen.svg?style=flat-square)](LICENSE)
  5. [![Build Status](https://img.shields.io/github/workflow/status/thephpleague/commonmark/Tests/main.svg?style=flat-square)](https://github.com/thephpleague/commonmark/actions?query=workflow%3ATests+branch%3Amain)
  6. [![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/thephpleague/commonmark.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/commonmark/code-structure)
  7. [![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/commonmark.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/commonmark)
  8. [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/126/badge)](https://bestpractices.coreinfrastructure.org/projects/126)
  9. [![Sponsor development of this project](https://img.shields.io/badge/sponsor%20this%20package-%E2%9D%A4-ff69b4.svg?style=flat-square)](https://www.colinodell.com/sponsor)
  10. ![league/commonmark](commonmark-banner.png)
  11. **league/commonmark** is a highly-extensible PHP Markdown parser created by [Colin O'Dell][@colinodell] which supports the full [CommonMark] spec and [GitHub-Flavored Markdown]. It is based on the [CommonMark JS reference implementation][commonmark.js] by [John MacFarlane] \([@jgm]\).
  12. ## 📦 Installation & Basic Usage
  13. This project requires PHP 7.4 or higher with the `mbstring` extension. To install it via [Composer] simply run:
  14. ``` bash
  15. $ composer require league/commonmark
  16. ```
  17. The `CommonMarkConverter` class provides a simple wrapper for converting CommonMark to HTML:
  18. ```php
  19. use League\CommonMark\CommonMarkConverter;
  20. $converter = new CommonMarkConverter([
  21. 'html_input' => 'strip',
  22. 'allow_unsafe_links' => false,
  23. ]);
  24. echo $converter->convertToHtml('# Hello World!');
  25. // <h1>Hello World!</h1>
  26. ```
  27. Or if you want GitHub-Flavored Markdown, use the `GithubFlavoredMarkdownConverter` class instead:
  28. ```php
  29. use League\CommonMark\GithubFlavoredMarkdownConverter;
  30. $converter = new GithubFlavoredMarkdownConverter([
  31. 'html_input' => 'strip',
  32. 'allow_unsafe_links' => false,
  33. ]);
  34. echo $converter->convertToHtml('# Hello World!');
  35. // <h1>Hello World!</h1>
  36. ```
  37. 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.
  38. 🔒 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, considering using a library (like [HTML Purifier](https://github.com/ezyang/htmlpurifier)) to provide additional HTML filtering.
  39. ## 📓 Documentation
  40. Full documentation on advanced usage, configuration, and customization can be found at [commonmark.thephpleague.com][docs].
  41. ## ⏫ Upgrading
  42. Information on how to upgrade to newer versions of this library can be found at <https://commonmark.thephpleague.com/releases>.
  43. ## 💻 GitHub-Flavored Markdown
  44. The `GithubFlavoredMarkdownConverter` shown earlier is a drop-in replacement for the `CommonMarkConverter` which adds additional features found in the GFM spec:
  45. - Autolinks
  46. - Disallowed raw HTML
  47. - Strikethrough
  48. - Tables
  49. - Task Lists
  50. See the [Extensions documentation](https://commonmark.thephpleague.com/customization/extensions/) for more details on how to include only certain GFM features if you don't want them all.
  51. ## 🗃️ Related Packages
  52. ### Integrations
  53. - [CakePHP 3](https://github.com/gourmet/common-mark)
  54. - [Drupal](https://www.drupal.org/project/markdown)
  55. - [Laravel 4+](https://github.com/GrahamCampbell/Laravel-Markdown)
  56. - [Sculpin](https://github.com/bcremer/sculpin-commonmark-bundle)
  57. - [Symfony 2 & 3](https://github.com/webuni/commonmark-bundle)
  58. - [Symfony 4](https://github.com/avensome/commonmark-bundle)
  59. - [Twig Markdown extension](https://github.com/twigphp/markdown-extension)
  60. - [Twig filter and tag](https://github.com/aptoma/twig-markdown)
  61. - [Laravel CommonMark Blog](https://github.com/spekulatius/laravel-commonmark-blog)
  62. ### Included Extensions
  63. See [our extension documentation](https://commonmark.thephpleague.com/extensions/overview) for a full list of extensions bundled with this library.
  64. ### Community Extensions
  65. Custom parsers/renderers can be bundled into extensions which extend CommonMark. Here are some that you may find interesting:
  66. - [Alt Three Emoji](https://github.com/AltThree/Emoji) An emoji parser for CommonMark.
  67. - [Sup Sub extensions](https://github.com/OWS/commonmark-sup-sub-extensions) - Adds support of superscript and subscript (`<sup>` and `<sub>` HTML tags)
  68. - [YouTube iframe extension](https://github.com/zoonru/commonmark-ext-youtube-iframe) - Replaces youtube link with iframe.
  69. - [Lazy Image extension](https://github.com/simonvomeyser/commonmark-ext-lazy-image) - Adds various options for lazy loading of images.
  70. Others can be found on [Packagist under the `commonmark-extension` package type](https://packagist.org/packages/league/commonmark?type=commonmark-extension).
  71. If you build your own, feel free to submit a PR to add it to this list!
  72. ### Others
  73. Check out the other cool things people are doing with `league/commonmark`: <https://packagist.org/packages/league/commonmark/dependents>
  74. ## 🏷️ Versioning
  75. [SemVer](http://semver.org/) 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.
  76. 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.
  77. ## 🛠️ Maintenance & Support
  78. When a new **minor** version (e.g. `1.5` -> `1.6`) is released, the previous one (`1.5`) will continue to receive security and critical bug fixes for *at least* 3 months.
  79. 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.
  80. (This policy may change in the future and exceptions may be made on a case-by-case basis.)
  81. **Professional support, including notification of new releases and security updates, is available through a [Tidelift Subscription](https://tidelift.com/subscription/pkg/packagist-league-commonmark?utm_source=packagist-league-commonmark&utm_medium=referral&utm_campaign=readme).**
  82. ## 👷‍♀️ Contributing
  83. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure with us.
  84. 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.
  85. Contributions to this library are **welcome**, especially ones that:
  86. * Improve usability or flexibility without compromising our ability to adhere to the [CommonMark spec]
  87. * Mirror fixes made to the [reference implementation][commonmark.js]
  88. * Optimize performance
  89. * Fix issues with adhering to the [CommonMark spec]
  90. Major refactoring to core parsing logic should be avoided if possible so that we can easily follow updates made to [the reference implementation][commonmark.js]. 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.
  91. Please see [CONTRIBUTING](https://github.com/thephpleague/commonmark/blob/main/.github/CONTRIBUTING.md) for additional details.
  92. ## 🧪 Testing
  93. ``` bash
  94. $ composer test
  95. ```
  96. This will also test league/commonmark against the latest supported spec.
  97. ## 🚀 Performance Benchmarks
  98. You can compare the performance of **league/commonmark** to other popular parsers by running the included benchmark tool:
  99. ``` bash
  100. $ ./tests/benchmark/benchmark.php
  101. ```
  102. ## 👥 Credits & Acknowledgements
  103. - [Colin O'Dell][@colinodell]
  104. - [John MacFarlane][@jgm]
  105. - [All Contributors]
  106. This code is partially based on the [CommonMark JS reference implementation][commonmark.js] which is written, maintained and copyrighted by [John MacFarlane]. This project simply wouldn't exist without his work.
  107. ### Sponsors
  108. We'd also like to extend our sincere thanks the following sponsors who support ongoing development of this project:
  109. - [Tidelift](https://tidelift.com/subscription/pkg/packagist-league-commonmark?utm_source=packagist-league-commonmark&utm_medium=referral&utm_campaign=readme) for offering support to both the maintainers and end-users through their [professional support](https://tidelift.com/subscription/pkg/packagist-league-commonmark?utm_source=packagist-league-commonmark&utm_medium=referral&utm_campaign=readme) program
  110. - [JetBrains](https://www.jetbrains.com/) for supporting this project with complimentary [PhpStorm](https://www.jetbrains.com/phpstorm/) licenses
  111. - [Taylor Otwell](https://twitter.com/taylorotwell) for sponsoring this project through GitHub sponsors
  112. Are you interested in sponsoring development of this project? See <https://www.colinodell.com/sponsor> for a list of ways to contribute.
  113. ## 📄 License
  114. **league/commonmark** is licensed under the BSD-3 license. See the [`LICENSE`](LICENSE) file for more details.
  115. ## 🏛️ Governance
  116. This project is primarily maintained by [Colin O'Dell][@colinodell]. Members of the [PHP League] Leadership Team may occasionally assist with some of these duties.
  117. ## 🗺️ Who Uses It?
  118. This project is used by [Drupal](https://www.drupal.org/project/markdown), [Laravel Framework](https://laravel.com/), [Cachet](https://cachethq.io/), [Firefly III](https://firefly-iii.org/), [Neos](https://www.neos.io/), [Daux.io](https://daux.io/), and [more](https://packagist.org/packages/league/commonmark/dependents)!
  119. ---
  120. <div align="center">
  121. <b>
  122. <a href="https://tidelift.com/subscription/pkg/packagist-league-commonmark?utm_source=packagist-league-commonmark&utm_medium=referral&utm_campaign=readme">Get professional support for league/commonmark with a Tidelift subscription</a>
  123. </b>
  124. <br>
  125. <sub>
  126. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  127. </sub>
  128. </div>
  129. [CommonMark]: http://commonmark.org/
  130. [CommonMark spec]: http://spec.commonmark.org/
  131. [commonmark.js]: https://github.com/jgm/commonmark.js
  132. [GitHub-Flavored Markdown]: https://github.github.com/gfm/
  133. [John MacFarlane]: http://johnmacfarlane.net
  134. [docs]: https://commonmark.thephpleague.com/
  135. [docs-examples]: https://commonmark.thephpleague.com/customization/overview/#examples
  136. [docs-example-twitter]: https://commonmark.thephpleague.com/customization/inline-parsing#example-1---twitter-handles
  137. [docs-example-smilies]: https://commonmark.thephpleague.com/customization/inline-parsing#example-2---emoticons
  138. [All Contributors]: https://github.com/thephpleague/commonmark/contributors
  139. [@colinodell]: https://www.twitter.com/colinodell
  140. [@jgm]: https://github.com/jgm
  141. [jgm/stmd]: https://github.com/jgm/stmd
  142. [Composer]: https://getcomposer.org/
  143. [PHP League]: https://thephpleague.com