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":
This project can be installed via Composer:
composer require mindkomm/commonmark-searchhighlight-extension
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.');