/commonmark-searchhighlight-extension

Extension for CommonMark PHP implementation to higlight search strings in Markdown files.

Primary LanguagePHPMIT LicenseMIT

SearchHighlight Extension

SearchHighlight is an extension for the CommonMark PHP implementation and can be used to highlight Markdown text that matches a search string.

The extension adds a <span class="search-highlight"> around the strings that match the search string.

Example for "something":

Installation

This project can be installed via Composer:

composer require mindkomm/commonmark-searchhighlight-extension

Usage

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

use Mind\CommonMark\SearchHighlightExtension\SearchHighlightExtension;

$searchstring = 'Your search string';
$config = [];

$environment = Environment::createCommonMarkEnvironment();

if (!empty($searchstring)) {
    // Pass the search string to the constructor.
    $environment->addExtension(new SearchHighlightExtension($searchstring));
}

$converter = new CommonMarkConverter($config, $environment);

echo $converter->convertToHtml('A text that contains your search string.');