PHP code example of torchlight / engine

1. Go to this page and download the library: Download torchlight/engine 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/ */

    

torchlight / engine example snippets


return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,

        // Add Torchlight syntax highlighting. [tl! focus]
        TorchlightExtension::class, // [tl! focus]
    ]
]



use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Torchlight\Engine\CommonMark\Extension;

$environment = new Environment;
$environment
    ->addExtension(new CommonMarkCoreExtension)
    ->addExtension(new Extension('github-light'));

$converter = new MarkdownConverter($environment);
$output = $converter->convert(<<<'MD'



use Torchlight\Engine\CommonMark\Extension;


$extension = new Extension('github-light');

$extension->renderer()
    ->setDefaultGrammar('php');



namespace Torchlight\Engine\CommonMark;

use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;

interface BlockCache
{
    public function has(FencedCode $node): bool;

    public function get(FencedCode $node): string;

    public function set(FencedCode $node, string $result): void;
}




use Torchlight\Engine\CommonMark\Extension;


$extension = new Extension('github-light');

$extension->renderer()
    ->setBlockCache(new MyCacheImplementation);



use Torchlight\Engine\CommonMark\Extension;

echo str()->markdown('...your markdown content...', extensions: [
    new Extension('github-light'),
]);


namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
use Statamic\Facades\Markdown;
use Torchlight\Engine\CommonMark\Extension;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Add the Torchlight Engine extension
        Markdown::addExtension(function () {
            return new Extension('synthwave-84');
        });
    }
}



use Torchlight\Engine\Engine;

$engine = new Engine;

$code = <<<'PHP'
echo "Hello, world!"; // [tl! ++]
PHP;

$engine->codeToHtml(
    $code,         // The code to highlight
    'php',         // The language
    'github-light' // The theme(s) to use
);



use Torchlight\Engine\CommonMark\Extension;

$extension = new Extension([
    'light' => 'github-light',
    'dark' => 'github-dark',
]);



use Torchlight\Engine\Engine;

$engine = new Engine;

$engine->registerTheme('my-theme', '/path/to/my-theme.json');

echo $engine->codeToHtml('echo "hello";', 'php', 'my-theme');

$engine->registerTheme('my-theme', [
    'name' => 'My Custom Theme',
    'colors' => [
        'editor.background' => '#1a1a2e',
        'editor.foreground' => '#e0e0e0',
        'editorLineNumber.foreground' => '#666666',
    ],
    'tokenColors' => [
        [
            'scope' => ['keyword', 'storage.type'],
            'settings' => ['foreground' => '#c792ea'],
        ],
        [
            'scope' => ['string'],
            'settings' => ['foreground' => '#c3e88d'],
        ],
    ],
]);



use Torchlight\Engine\Engine;
use Torchlight\Engine\Theme\Theme;

$engine = new Engine;

echo $engine->codeToHtml(
    'echo "hello";',
    'php',
    Theme::override('nord', [
        'editor.background' => '#000000',
        'editor.lineHighlightBackground' => '#ff000033',
    ])
);

$engine->codeToHtml($code, 'php', [
    'light' => Theme::override('github-light', [
        'editor.background' => '#fafafa',
    ]),
    'dark' => Theme::override('nord', [
        'editor.background' => '#111111',
    ]),
]);
{3}{2,4-5}{9}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown. 
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! focus]
        TorchlightExtension::class, // [tl! focus]
    ]
]

return [
    'extensions' => [ // [tl! highlight]
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [ // [tl! highlight:2]
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,  // [tl! highlight:-2]
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

// This is a long bit of text, hard to highlight the middle. [tl! highlight:6,3]
return <<<EOT
spring sunshine
the smell of waters
from the stars

deep winter
the smell of a crow
from the stars

beach to school
the smell of water
in the sky
EOT;

// This is a long bit of text, hard to highlight the middle. 
return <<<EOT
spring sunshine
the smell of waters
from the stars

deep winter
the smell of a crow
from the stars

beach to school
the smell of water
in the sky
EOT; // [tl! highlight:-7,3]

return [
    'extensions' => [  // Start here [tl! highlight:start]
        // Add attributes straight from markdown.
        AttributesExtension::class, 
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ] // End here [tl! highlight:end]
]

// Reach down 4 lines, add the ID to one line [tl! #popover-trigger:4,1]
return <<<EOT
spring sunshine
the smell of waters
from the stars

deep winter
the smell of a crow
from the stars

beach to school
the smell of water
in the sky
EOT;

return [
    'extensions' => [
        // Add attributes straight from markdown. [tl! highlight:1]
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown. [tl! .highlight]
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! .foobar.bazbuz]
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown. [tl! ~~:1]
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! focus]
        TorchlightExtension::class, // [tl! focus]
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! **]
        TorchlightExtension::class, // [tl! **]
    ]
]

return [
    'heading_permalink' => [ // [tl! collapse:start]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ], // [tl! collapse:end]

    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'heading_permalink' => [ // [tl! collapse:5]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ],

    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

// torchlight! {"summaryCollapsedIndicator": "Click to show ]"}
return [
    'heading_permalink' => [ // [tl! collapse:start]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ], // [tl! collapse:end]

    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'heading_permalink' => [ // [tl! collapse:start open]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ], // [tl! collapse:end]

    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class, 
    ]
]

// torchlight! {"showSummaryCarets": false}
return [
    'heading_permalink' => [ // [tl! collapse:start]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ], // [tl! collapse:end]

    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'heading_permalink' => [ // [tl! hide:start]
        'html_class' => 'permalink',
        'id_prefix' => 'user-content',
        'insert' => 'before',
        'title' => 'Permalink',
        'symbol' => '#',
    ], // [tl! hide:end]

    'extensions' => [
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add]
    ]
]

// torchlight! {"diffIndicators": false}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add]
    ]
]

// torchlight! {"diffIndicators": true}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add]
    ]
]

// torchlight! {"diffIndicators": true, "diffIndicatorsInPlaceOfLineNumbers": false}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add]
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add]
    ]
]

$greeting = "Hello, World!";
$greeting = "Hello, PHP!"; // [tl! wd]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! highlight .animate-pulse]
        TorchlightExtension::class, // [tl! highlight .font-bold .italic .animate-pulse #pulse]
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting. [tl! .bg-gray-900:-1,4 .animate-pulse:1]
        TorchlightExtension::class,
    ]
]

// Learn more about Torchlight: [tl! link("https://torchlight.dev")]
TorchlightExtension::class,

TorchlightExtension::class, // [tl! link("https://torchlight.dev"):c0,26]

/**
 * @see https://youtu.be/LEXIYgOXsRU?si=wDC7GxC1y3pNdHjZ&t=69. [tl! autolink]
 */

$link = 'https://youtu.be/LEXIYgOXsRU?si=wDC7GxC1y3pNdHjZ&t=69'; // [tl! autolink]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        SomeOtherHighlighter::class, // [tl! remove]
        TorchlightExtension::class, // [tl! add reindex(+1000)]
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class, // [tl! reindex(vim.relative)]
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class, // [tl! reindex(vim.preserve)]
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]



use Torchlight\Engine\Options;

Options::setDefaultOptionsBuilder(function () {
    return new Options(
        // Specify your options here.
    );
});



use Torchlight\Engine\Options;

Options::setDefaultOptionsBuilder(function () {
    return Options::fromArray([]);
});


namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
use Torchlight\Engine\Options;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Options::setDefaultOptionsBuilder(fn () => Options::fromArray(config('torchlight.options')));
    }
}




use Torchlight\Engine\CommonMark\Extension;

Extension::setThemeResolver(function () {
    return [
        'light' => 'github-light',
        'dark' => 'github-dark',
    ];
});


namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
use Torchlight\Engine\CommonMark\Extension;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Extension::setThemeResolver(function () {
            return config('torchlight.theme');
        });
    }
}

// torchlight! {"lineNumberAndDiffIndicatorRightPadding": 10}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class,
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

// torchlight! {"lineNumberAndDiffIndicatorRightPadding": 10, "diffIndicatorsInPlaceOfLineNumbers": false}
return [
    'extensions' => [
        // Add attributes straight from markdown.
        AttributesExtension::class, // [tl! ++]
        
        // Add Torchlight syntax highlighting.
        TorchlightExtension::class,
    ]
]

// torchlight! {"classes": "some extra classes"}
return [
    // ...
];

// torchlight! {"copyable": true}

namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
use Statamic\Facades\Markdown;
use Torchlight\Engine\CommonMark\Extension;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Add the Torchlight Engine extension
        Markdown::addExtension(function () {
            return new Extension('synthwave-84');
        });
    }
}



use Torchlight\Engine\Engine;
use Torchlight\Engine\Annotations\AnnotationContext;

$engine = new Engine;

$engine->registerAnnotation('important', function (AnnotationContext $ctx) {
    $ctx->addBlockClass('has-important-lines')
        ->addLineClass('line-important');
});

$engine->registerAnnotation('badge', function (AnnotationContext $ctx) {
    if ($ctx->isCharacterRange()) {
        $ctx->addClassToCharacterRange('badge');
    } else {
        $ctx->addLineClass('line-badge');
    }
}, charRanges: true);

$engine->registerAnnotationMacro('important', ['highlight', '.important-line']);

// String replacement
$engine->addReplacer('sk_live_abc123', 'YOUR_API_KEY');

// Callable replacement
$engine->addReplacer(function (string $html) {
    return str_replace('old-class', 'new-class', $html);
});



use Torchlight\Engine\Contracts\TokenTransformer;
use Torchlight\Engine\Generators\RenderContext;

$engine->registerTokenTransformerFactory(function () {
    return new class implements TokenTransformer {
        public function transform(RenderContext $context, array $tokens): array
        {
            // Modify tokens here
            return $tokens;
        }

        public function supports(string $grammarName): bool
        {
            return $grammarName === 'my-grammar';
        }
    };
});



use Torchlight\Engine\Contracts\BlockDecorator;
use Torchlight\Engine\Generators\RenderContext;

$engine->registerBlockDecoratorFactory(function () {
    return new class implements BlockDecorator {
        public function shouldRender(RenderContext $context): bool
        {
            return true;
        }

        public function render(RenderContext $context, string $cleanedText): string
        {
            return '<div class="my-footer">Custom content</div>';
        }

        public function getPriority(): int
        {
            return 100;
        }
    };
});

use Torchlight\Engine\Contracts\Preprocessor;
use Torchlight\Engine\Engine;
use Torchlight\Engine\Preprocessors\PreprocessorArgs;

class MyPreprocessor implements Preprocessor
{
    public function process(PreprocessorArgs $args, Engine $engine): array
    {
        return $args->tokens;
    }

    public function supports(?string $grammarName): bool
    {
        return true;
    }
}
text
{
    "torchlightAnnotations": true,
    "lineNumbers": true, // [tl! focus:2]
    "lineNumbersStart": 1,
    "lineNumbersStyle": "text-align: right; -webkit-user-select: none; user-select: none;",
    "summaryCollapsedIndicator": "...",

    "diffIndicators": false,
    "diffIndicatorsInPlaceOfLineNumbers": true,
}
html
<code ... class="phiki language-php moonlight-ii torchlight some extra classes">
text
// torchlight! {"indentGuides": "html", "indentGuidesTabWidth": 4}
text
// torchlight! {"columnGuides": [80]}
text
// torchlight! {"columnGuides": [80, 120]}
text
// torchlight! {"withGutter": false}
 echo 'This is Torchlight';