Installation
composer require anh/markup-bundle
Add to config/app.php under providers:
Anh\MarkupBundle\MarkupBundle::class,
Publish config (if available) with:
php artisan vendor:publish --provider="Anh\MarkupBundle\MarkupBundle"
First Use Case
Register a markup handler in a service provider (e.g., AppServiceProvider):
use Anh\MarkupBundle\MarkupBundle;
public function boot()
{
MarkupBundle::registerHandler('html', new \Anh\MarkupBundle\Handlers\HtmlHandler());
}
Use the markup service in a controller:
use Anh\MarkupBundle\MarkupService;
public function index(MarkupService $markup)
{
$html = $markup->parse('html', '<p>Hello, <strong>Markup!</strong></p>');
return response()->json(['content' => $html]);
}
Handler Registration
Extend \Anh\MarkupBundle\Handlers\AbstractHandler for custom formats (e.g., Markdown, BBCode):
class MarkdownHandler extends AbstractHandler
{
public function parse($input): string
{
return \Parsedown::instance()->text($input);
}
}
Register via:
MarkupBundle::registerHandler('markdown', new MarkdownHandler());
Service Integration
Inject MarkupService into controllers, services, or Blade directives:
// Blade directive example
Blade::directive('markup', function ($expression) {
return "<?php echo app('markup')->parse('html', {$expression}); ?>";
});
Usage in Blade:
@markup('<b>Dynamic HTML</b>')
Middleware for Sanitization Use middleware to parse markup before rendering:
public function handle($request, Closure $next)
{
$request->merge([
'sanitized_content' => app('markup')->parse('html', $request->content)
]);
return $next($request);
}
Caching Parsed Output
Decorate MarkupService to cache results:
class CachedMarkupService implements \Anh\MarkupBundle\Contracts\MarkupService
{
public function parse(string $format, string $input): string
{
$cacheKey = "markup_{$format}_{md5($input)}";
return cache()->remember($cacheKey, now()->addHours(1), function() use ($format, $input) {
return app('anh.markup')->parse($format, $input);
});
}
}
Event-Driven Extensions Dispatch events before/after parsing (if the bundle supports it):
event(new \Anh\MarkupBundle\Events\MarkupParsing($format, $input));
Handler Not Found
Undefined handler for format 'xyz'.boot()).dd(app('anh.markup')->getHandlers());
XSS Vulnerabilities
HTMLPurifier in the handler:
$purifier = new \HTMLPurifier();
return $purifier->purify($input);
Performance Overhead
MarkupBundle::unregisterHandler('html');
Custom Formats
AbstractHandler and implement parse() for new formats (e.g., LaTeX, SVG).class LatexHandler extends AbstractHandler
{
public function parse($input): string
{
return \Dompdf\Dompdf::loadHtml($input)->render();
}
}
Service Binding
AppServiceProvider:
$this->app->bind(\Anh\MarkupBundle\Contracts\MarkupService::class, CustomMarkupService::class);
Testing
MarkupService in tests:
$mock = Mockery::mock(\Anh\MarkupBundle\Contracts\MarkupService::class);
$mock->shouldReceive('parse')->andReturn('<p>Mocked</p>');
$this->app->instance(\Anh\MarkupBundle\Contracts\MarkupService::class, $mock);
AbstractHandler:
public function parse($input): string
{
\Log::debug("Parsing {$this->getFormat()} with input: " . substr($input, 0, 50));
return $this->doParse($input);
}
How can I help you explore Laravel packages today?