定制 cassarco/markdown-tools 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cassarco/markdown-tools

Composer 安装命令:

composer require cassarco/markdown-tools

包简介

A package for Laravel that lets you run Laravel Validation and/or a handler function over markdown files in your application.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 file
  • app/Actions/MarkdownFileHandler.php - handler for processing markdown files
  • app/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

Output

If files fail validation, you'll see Laravel validation errors:

Validation Error

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.

cassarco/markdown-tools 适用场景与选型建议

cassarco/markdown-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「markdown」 「laravel」 「cassarco」 「markdown-tools」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cassarco/markdown-tools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cassarco/markdown-tools 我们能提供哪些服务?
定制开发 / 二次开发

基于 cassarco/markdown-tools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-25