Skip to content

Render ==highlighted== texts as <mark> elements in league/commonmark

License

Notifications You must be signed in to change notification settings

stefanzweifel/commonmark-mark-extension

Repository files navigation

Parse and Render ==marked text== with league/commonmark

Latest Version on Packagist Tests Check & fix styling Total Downloads

A league/commonmark extension to turn highlighted text into <mark>-HTML elements.

For example, the following markdown text …

The ==quick brown fox== jumps over the lazy dog.

… is turned into the following HTML.

<p>The <mark>quick brown fox</mark> jumps over the lazy dog.</p>

Installation

You can install the package via composer:

composer require wnx/commonmark-mark-extension

Usage

Create a custom CommonMark environment, and register the MarkExtension.

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Wnx\CommonmarkMarkExtension\MarkExtension;

// Configure the Environment with all the CommonMark parsers/renderers
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());

// Add this extension
$environment->addExtension(new MarkExtension());

// Instantiate the converter engine and start converting some Markdown!
$converter = new MarkdownConverter($environment);
echo $converter->convertToHtml('The ==quick== brown fox jumps over the ==lazy dog==');

If you're using a different character than = to highlight text in Markdown, you can pass a character configuration to the extension.

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Wnx\CommonmarkMarkExtension\MarkExtension;

// Define your configuration, if needed
$config = [
    'mark' => [
        'character' => ':',
    ],
];

// Configure the Environment with all the CommonMark parsers/renderers
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());

// Add this extension
$environment->addExtension(new MarkExtension());

// Instantiate the converter engine and start converting some Markdown!
$converter = new MarkdownConverter($environment);
echo $converter->convertToHtml('The ::quick:: brown fox jumps over the ::lazy dog::');

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.