PHP code example of spatie / commonmark-shiki-highlighter

1. Go to this page and download the library: Download spatie/commonmark-shiki-highlighter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / commonmark-shiki-highlighter example snippets


use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension;

function convertToHtml(string $markdown, string $theme): string
{
    $environment = (new Environment())
        ->addExtension(new CommonMarkCoreExtension())
        ->addExtension(new HighlightCodeExtension(theme: $theme));

    $markdownConverter = new MarkdownConverter(environment: $environment);

    return $markdownConverter->convertToHtml($markdown);
}

use Spatie\ShikiPhp\Shiki;
use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension;

$environment->addExtension(new HighlightCodeExtension(shiki: new Shiki()));
{1,2}{3}

echo "We're highlighting line 1 and 2";
echo "And focusing line 3";


+ echo "This line is marked as added";
- echo "This line is marked as deleted";

$environment = (new Environment())
    ->addExtension(new CommonMarkCoreExtension())
    ->addExtension(new HighlightCodeExtension(theme: $theme, throw: true));