PHP code example of moneo / markdown-for-agents

1. Go to this page and download the library: Download moneo/markdown-for-agents 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/ */

    

moneo / markdown-for-agents example snippets


use Moneo\MarkdownForAgents\Facades\MarkdownForAgents;

$result = MarkdownForAgents::url('https://example.com')->convert();

$result->markdown;       // string — the converted content
$result->tokens;         // int — estimated token count
$result->contentSignals; // ?array — ['ai-train' => 'yes', ...]
$result->driver;         // string — which driver was used
$result->fromCache;      // bool — served from cache?

// Single file
$result = MarkdownForAgents::file('/path/to/document.pdf')->convert();

// Laravel UploadedFile
$result = MarkdownForAgents::file($request->file('document'))->convert();

// Batch conversion
$results = MarkdownForAgents::files([$pdf, $image, $spreadsheet])->convert();

foreach ($results as $result) {
    echo "{$result->name}: {$result->tokens} tokens\n";
}

$result = MarkdownForAgents::driver('browser')
    ->html('<div><h1>Hello</h1><p>World</p></div>')
    ->convert();

// Use the browser driver for JS-heavy pages
$result = MarkdownForAgents::driver('browser')
    ->url('https://spa-app.com')
    ->waitUntil('networkidle0')
    ->convert();

$result = MarkdownForAgents::url('https://example.com')
    ->withFallback('browser')
    ->convert();

$result = MarkdownForAgents::driver('browser')
    ->url('https://example.com')
    ->waitUntil('networkidle0')
    ->userAgent('MyBot/1.0')
    ->rejectPatterns(['/^.*\.(css|font)/'])
    ->cookies([['name' => 'session', 'value' => 'abc', 'domain' => '.example.com']])
    ->authenticate('user', 'pass')
    ->convert();

// Skip cache for this request
$result = MarkdownForAgents::url($url)->noCache()->convert();

// Custom TTL (seconds)
$result = MarkdownForAgents::url($url)->cache(7200)->convert();

// Clear cache
MarkdownForAgents::clearCache('https://example.com');
MarkdownForAgents::flushCache();

$result = MarkdownForAgents::url($url)
    ->when($useBrowser, fn ($c) => $c->waitUntil('networkidle0'))
    ->unless($isAdmin, fn ($c) => $c->noCache())
    ->convert();

$formats = MarkdownForAgents::supportedFormats();
// Returns SupportedFormat[] — delegates to Workers AI

Route::get('/blog/{slug}', [BlogController::class, 'show'])
    ->middleware('markdown-for-agents');

use Moneo\MarkdownForAgents\Events\MarkdownConverted;

Event::listen(MarkdownConverted::class, function (MarkdownConverted $event) {
    Log::info("Converted {$event->source} in {$event->duration}s");
});

use Moneo\MarkdownForAgents\Facades\MarkdownForAgents;

MarkdownForAgents::extend('custom', function ($app) {
    return new MyCustomDriver($app['config']);
});

$result = MarkdownForAgents::driver('custom')->url($url)->convert();

use Moneo\MarkdownForAgents\Facades\MarkdownForAgents;
use Moneo\MarkdownForAgents\DTOs\ConversionResult;

MarkdownForAgents::shouldReceive('url->convert')
    ->andReturn(new ConversionResult(...));
bash
php artisan vendor:publish --tag=markdown-for-agents-config