cassarco / markdown-tools
A package for Laravel that lets you run Laravel Validation and/or a handler function over markdown files in your application.
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/cassarco/markdown-tools
Requires
- php: ^8.3
- cassarco/league-commonmark-wikilinks: ^1.0.2
- spatie/laravel-package-tools: ^1.16
- symfony/dom-crawler: ^7.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- dev-main / 2.x-dev
- v2.0.1
- v2.0.0
- v1.0.0
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.5.0
- dev-dependabot/github_actions/actions/checkout-6
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-7
- dev-dependabot/github_actions/actions/checkout-5
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.6
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-6
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.4.0
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.5
- dev-dependabot/github_actions/dependabot/fetch-metadata-2.2.0
This package is auto-updated.
Last update: 2026-02-03 16:53:49 UTC
README
A Laravel package for processing markdown files with validation and custom handlers.
Why?
Markdown files with YAML front-matter are a great way to manage content. They're version-controlled, portable, and easy to edit. But when your Laravel application needs that content in the database, you need a reliable way to import it.
This package lets you:
- Sync markdown to your database - Import blog posts, documentation, or any content from markdown files to Eloquent models, keeping them in sync as you update your files.
- Validate front-matter - Use Laravel's validation rules to ensure every markdown file has the required metadata (title, slug, dates, etc.) before processing.
- Process multiple content types - Define separate schemes for different content types (articles, docs, pages) each with their own validation rules and handlers.
- Automate in CI/CD - Run the command in your deployment pipeline to automatically sync content changes.
How it works
Define one or more schemes in your configuration file, then run the bundled command to process them.
Installation
Install the package via composer:
composer require cassarco/markdown-tools
Run the install command to publish the config and action stubs:
php artisan markdown-tools:install
This publishes:
config/markdown-tools.php- configuration fileapp/Actions/MarkdownFileHandler.php- handler for processing markdown filesapp/Actions/MarkdownFileRules.php- validation rules for front-matter
Alternatively, publish them separately:
php artisan vendor:publish --tag=markdown-tools-config php artisan vendor:publish --tag=markdown-tools-actions
Configuration
The published config file defines your schemes:
use App\Actions\MarkdownFileHandler; use App\Actions\MarkdownFileRules; return [ 'schemes' => [ 'default' => [ 'path' => resource_path('markdown'), 'rules' => MarkdownFileRules::class, 'handler' => MarkdownFileHandler::class, ], ], 'common-mark' => [ // League/CommonMark settings... ], ];
Usage
Define Validation Rules
Edit app/Actions/MarkdownFileRules.php to specify validation rules for front-matter properties:
<?php namespace App\Actions; use Cassarco\MarkdownTools\Contracts\MarkdownFileRules as MarkdownFileRulesContract; class MarkdownFileRules implements MarkdownFileRulesContract { public function __invoke(): array { return [ 'uuid' => 'required|uuid', 'title' => 'required|string|max:255', ]; } }
Define a Handler
Edit app/Actions/MarkdownFileHandler.php to process each markdown file:
<?php namespace App\Actions; use App\Models\Article; use Cassarco\MarkdownTools\Contracts\MarkdownFileHandler as MarkdownFileHandlerContract; use Cassarco\MarkdownTools\MarkdownFile; use function Laravel\Prompts\info; class MarkdownFileHandler implements MarkdownFileHandlerContract { public function __invoke(MarkdownFile $file): void { Article::updateOrCreate([ 'uuid' => $file->frontMatter()['uuid'], ], [ 'uuid' => $file->frontMatter()['uuid'], 'title' => $file->frontMatter()['title'], ]); info("Processed: {$file->pathname()}"); } }
Process Your Schemes
Run the bundled command:
php artisan markdown-tools:process
If files fail validation, you'll see Laravel validation errors:
MarkdownFile Methods
The handler receives a MarkdownFile instance with these methods:
$file->frontMatter() // Front-matter as a PHP array $file->markdown() // Raw markdown content $file->html() // HTML without table of contents $file->toc() // Table of contents HTML $file->htmlWithToc() // HTML with embedded table of contents $file->pathname() // File path
Real-World Example
Here's how I use this package to sync markdown files to my database:
<?php namespace App\Actions; use App\Models\Article; use Cassarco\MarkdownTools\Contracts\MarkdownFileHandler as MarkdownFileHandlerContract; use Cassarco\MarkdownTools\MarkdownFile; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use function Laravel\Prompts\info; class MarkdownFileHandler implements MarkdownFileHandlerContract { public function __invoke(MarkdownFile $file): void { Article::updateOrCreate([ 'uuid' => $file->frontMatter()['uuid'], ], [ 'uuid' => $file->frontMatter()['uuid'], 'title' => $file->frontMatter()['title'], 'slug' => $file->frontMatter()['slug'] ?? Str::slug($file->frontMatter()['title']), 'description' => $file->frontMatter()['description'], 'table_of_contents' => $file->toc(), 'content' => $file->html(), 'image' => $file->frontMatter()['image'], 'tags' => collect($file->frontMatter()['tags']), 'published_at' => Carbon::make($file->frontMatter()['published_at']), 'deleted_at' => Carbon::make($file->frontMatter()['deleted_at']), 'created_at' => Carbon::make($file->frontMatter()['created_at']), 'updated_at' => Carbon::make($file->frontMatter()['updated_at']), ]); info("Processing {$file->frontMatter()['title']}"); } }
Upgrading from v1 to v2
Version 2.0 introduces breaking changes to support config caching. Handlers and rules are now class-based instead of closures.
Step 1: Update your dependencies
composer require cassarco/markdown-tools:^2.0
This requires Laravel 11+ and PHP 8.3+.
Step 2: Publish the action stubs
php artisan vendor:publish --tag=markdown-tools-actions
This creates app/Actions/MarkdownFileHandler.php and app/Actions/MarkdownFileRules.php.
Step 3: Migrate your handler logic
Move your closure logic from the config file to the new handler class.
Before (v1):
// config/markdown-tools.php 'handler' => function (MarkdownFile $file) { Article::updateOrCreate( ['uuid' => $file->frontMatter()['uuid']], ['title' => $file->frontMatter()['title']] ); },
After (v2):
// app/Actions/MarkdownFileHandler.php public function __invoke(MarkdownFile $file): void { Article::updateOrCreate( ['uuid' => $file->frontMatter()['uuid']], ['title' => $file->frontMatter()['title']] ); }
Step 4: Migrate your validation rules
Move your rules array to the new rules class.
Before (v1):
// config/markdown-tools.php 'rules' => [ 'title' => 'required', ],
After (v2):
// app/Actions/MarkdownFileRules.php public function __invoke(): array { return [ 'title' => 'required', ]; }
Step 5: Update your config
Replace closures with class references:
// config/markdown-tools.php use App\Actions\MarkdownFileHandler; use App\Actions\MarkdownFileRules; 'schemes' => [ 'default' => [ 'path' => resource_path('markdown'), 'rules' => MarkdownFileRules::class, 'handler' => MarkdownFileHandler::class, ], ],
Note: The default scheme was renamed from 'markdown' to 'default'.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you find a bug that impacts the security of this package please send an email to security@cassar.co instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.

