mindkomm/commonmark-searchhighlight-extension

Extension for CommonMark PHP implementation to highlight search strings in Markdown files

2.0.0 2019-12-05 06:26 UTC

This package is auto-updated.

Last update: 2024-12-05 17:28:01 UTC


README

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