Giter VIP home page Giter VIP logo

commonmark's Introduction

league/commonmark

Latest Version Total Downloads Software License Build Status Coverage Status Quality Score SensioLabs Insight CII Best Practices

league/commonmark is a PHP-based Markdown parser created by Colin O'Dell which supports the full CommonMark spec. It is based on the CommonMark JS reference implementation by John MacFarlane (@jgm).

Goals

  • Fully support the CommonMark spec (100% compliance)
  • Match the C and JavaScript implementations of CommonMark to make a logical and similar API
  • Continuously improve performance without sacrificing quality or compliance
  • Provide an extensible parser/renderer which users may customize as needed

Installation

This project can be installed via Composer:

$ composer require league/commonmark

Note: See Versioning for important information on which version constraints you should use.

Basic Usage

The CommonMarkConverter class provides a simple wrapper for converting CommonMark to HTML:

use League\CommonMark\CommonMarkConverter;

$converter = new CommonMarkConverter();
echo $converter->convertToHtml('# Hello World!');

// <h1>Hello World!</h1>

โš ๏ธ Security warning: If you will be parsing untrusted input from users, please consider setting the html_input and allow_unsafe_links options. See https://commonmark.thephpleague.com/security/ for more details.

Advanced Usage & Customization

The actual conversion process requires two steps:

  1. Parsing the Markdown input into an AST
  2. Rendering the AST document as HTML

Although the CommonMarkConverter wrapper simplifies this process for you, advanced users will likely want to do this themselves:

use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;

// Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go
$environment = Environment::createCommonMarkEnvironment();

// Optional: Add your own parsers, renderers, extensions, etc. (if desired)
// For example:  $environment->addInlineParser(new TwitterHandleParser());

// Define your configuration:
$config = [
  'html_input' => 'escape',
  'renderer' => [
    // To configure GitHub style newlines to create line breaks:
    'soft_break' => "<br />\n",
  ],
];

// Create the converter
$converter = new CommonMarkConverter($config, $environment);

// Here's our sample input
$markdown = '# Hello World!';

// Let's render it!
echo $converter->convertToHtml($markdown);

// The output should be:
// <h1>Hello World!</h1>

This approach allows you to access/modify the AST before rendering it.

You can also add custom parsers/renderers by registering them with the Environment class. The documentation provides several customization examples such as:

You can also reference the core CommonMark parsers/renderers as they use the same functionality available to you.

Documentation

Documentation can be found at commonmark.thephpleague.com.

Related Packages

Integrations

League Extensions

The PHP League offers useful extensions that add extra syntax and features:

You can add them to your project or use them as examples to develop your own custom features.

Community Extensions

Custom parsers/renderers can be bundled into extensions which extend CommonMark. Here are some that you may find interesting:

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

Compatibility with CommonMark

This project aims to fully support the entire CommonMark spec. Other flavors of Markdown may work but are not supported. Any/all changes made to the spec or JS reference implementation should eventually find their way back into this codebase.

league/commonmark 0.15.5 and higher supports version 0.28 of the CommonMark spec.

(This package is not part of CommonMark, but rather a compatible derivative.)

Testing

$ ./vendor/bin/phpunit

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

Versioning

SemVer will be followed closely. 0.x.0 versions will introduce breaking changes to the codebase, so be careful which version constraints you use. It's highly recommended that you use Composer's caret operator to ensure compatibility; for example: ^0.18. This is equivalent to >=0.18.0 <0.19.0.

0.x.y 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, minor spec changes, etc.) As a result, you might get slightly different HTML, but any custom code built onto this library will still function correctly.

If you're only using the CommonMarkConverter class or ConverterInterface to convert Markdown (no other class references, custom parsers, etc.), then it should be safe to use a broader constraint like ~0.18, >0.18, etc. I personally promise to never break this specific class in any future 0.x release.

Stability

While this package does work well, the underlying code should not be considered "stable" yet. The original spec and JS parser may undergo changes in the near future which will result in corresponding changes to this code. Any methods tagged with @api are not expected to change, but other methods/classes might.

Major release 1.0.0 will be reserved for when either the CommonMark spec or this project are considered stable (see outstanding CommonMark spec issues). 0.x.y will be used until that happens.

Contributing

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.

For now, I'd like to maintain similar logic as the JS reference implementation until everything is stable. I'll gladly accept any contributions which:

  • Mirror fixes made to the reference implementation
  • Optimize existing methods or regular expressions
  • Fix issues with adhering to the spec examples

Major refactoring should be avoided for now so that we can easily follow updates made to the reference implementation. This restriction will likely be lifted once the CommonMark specs and implementations are considered stable.

Please see CONTRIBUTING for additional details.

Security

If you discover any security related issues, please email your report privately to [email protected] instead of using the issue tracker.

Credits & Acknowledgements

This code is a port of the CommonMark JS reference implementation which is written, maintained and copyrighted by John MacFarlane. This project simply wouldn't exist without his work.

Also a huge thank you to JetBrains for supporting the development of this project with complimentary PhpStorm licenses.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.