Skip to content

Latest commit

 

History

History
42 lines (25 loc) · 1.18 KB

File metadata and controls

42 lines (25 loc) · 1.18 KB

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.');