Skip to content

stefanzweifel/commonmark-markdown-renderer

Repository files navigation

Render Markdown AST back to Markdown.

Latest Version on Packagist Tests Check & fix styling Total Downloads

Render a league/commonmark AST back to Markdown.

Note: Markdown knows alternate syntaxes for the same elements (heading, emphasis, unordered lists, horizontal rules). Therefore input and output Markdown may differ.

Installation

You can install the package via composer:

composer require wnx/commonmark-markdown-renderer

Usage

use League\CommonMark\Environment\Environment;
use League\CommonMark\Parser\MarkdownParser;
use Wnx\CommonmarkMarkdownRenderer\MarkdownRendererExtension;
use Wnx\CommonmarkMarkdownRenderer\Renderer\MarkdownRenderer;

$environment = new Environment($config);
$environment->addExtension(new MarkdownRendererExtension());

$markdownParser = new MarkdownParser($environment);
$markdownRenderer = new MarkdownRenderer($environment);

$markdown = "**Hello World!**";

$documentAST = $markdownParser->parse($markdown);

// Manipulate AST in your script (append, prepend or replace nodes)

$markdown = $markdownRenderer->renderDocument($documentAST);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.