PHP code example of cassarco / markdown-tools

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

    

cassarco / markdown-tools example snippets


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...
    ],
];



namespace App\Actions;

use Cassarco\MarkdownTools\Contracts\MarkdownFileRules as MarkdownFileRulesContract;

class MarkdownFileRules implements MarkdownFileRulesContract
{
    public function __invoke(): array
    {
        return [
            'uuid' => '



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()}");
    }
}

$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



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']}");
    }
}

// config/markdown-tools.php
'handler' => function (MarkdownFile $file) {
    Article::updateOrCreate(
        ['uuid' => $file->frontMatter()['uuid']],
        ['title' => $file->frontMatter()['title']]
    );
},

// app/Actions/MarkdownFileHandler.php
public function __invoke(MarkdownFile $file): void
{
    Article::updateOrCreate(
        ['uuid' => $file->frontMatter()['uuid']],
        ['title' => $file->frontMatter()['title']]
    );
}

// config/markdown-tools.php
'rules' => [
    'title' => '

// app/Actions/MarkdownFileRules.php
public function __invoke(): array
{
    return [
        'title' => '

// config/markdown-tools.php
use App\Actions\MarkdownFileHandler;
use App\Actions\MarkdownFileRules;

'schemes' => [
    'default' => [
        'path' => resource_path('markdown'),
        'rules' => MarkdownFileRules::class,
        'handler' => MarkdownFileHandler::class,
    ],
],
bash
php artisan markdown-tools:install
bash
php artisan vendor:publish --tag=markdown-tools-config
php artisan vendor:publish --tag=markdown-tools-actions
bash
php artisan markdown-tools:process
bash
php artisan vendor:publish --tag=markdown-tools-actions