bryceandy/press

Laravel 的 markdown 博客

1.0 2020-05-29 00:25 UTC

README

Actions Status Total Downloads Latest Stable Version License

Laravel 包可用于使用 markdown 文件发布和更新您的博客。

✅ 定位您的 markdown 文件位置(例如本地文件目录、云端)
✅ 创建或更新这些文件
✅ 选择您文章的自定义字段
✅ 自定义任何额外字段如何被解析
✅ 通过安排命令来选择何时发布或更新文章

要求

您的项目需要满足以下要求

  • PHP 版本 >=7.4
  • Laravel 版本 7 及以上
  • ext-json 已安装

安装

在您的终端中运行以下命令以安装包

composer require bryceandy/press

用法

发布配置并运行迁移

运行以下 artisan 命令,并将 config 文件 press.php 发布到 config 目录。

php artisan vendor:publish --tag=press-config

您可能需要将 markdown 文件存储在特定的驱动程序中。但当前此包支持本地文件驱动程序。

更新发布文件中的配置选项 file.path,其中包含您将存储 markdown 文件的目录。

如果您不更新此路径,请确保在项目根目录中创建一个 blogs 目录并存储文件。

php artisan migrate

您还应该运行 artisan migrate 命令以添加文章表。

Markdown 文件语法

您的博客文章将有一个必填字段 标题,以及您自己的自定义字段。以下是为您的 markdown 文件提供的示例语法

---
title: Post title  
field1: Additional field  
field2: Another additional field  
---  
The body of your post here  

自定义解析额外字段

如果您希望自定义如何解析(保存到数据库)额外字段,或者您可能希望覆盖标题字段如何解析

假设我们想要创建一个 生日 字段,该字段应解析为 Carbon 实例

  1. 在文章的 markdown 文件中添加生日字段
---
title: An awesome post  
birthday: Jan 20, 2020  
field: Another field  
field2: Yet another field  
---  
The rest of the body  
  1. 为字段创建一个自定义类并扩展 FieldContract

  2. 继承静态 process 方法并返回包含 $field 值的数组

<?php

namespace App\Fields;

use Bryceandy\Press\Contracts\FieldContract;
use Carbon\Carbon;

class Birthday extends FieldContract
{
    public static function process($field, $value, $data)
    {
        return [
            $field => Carbon::parse($value),
            // or $field => $value,
        ];
    }
}
  1. 发布 Press 服务提供者
php artisan vendor:publish --tag=press-provider

不要忘记在 config/app.php 文件中注册已发布的服务提供者和 Press 门面

/*
 * Package Service Providers...
 */
 App\Providers\PressServiceProvider::class,

/**
 * Aliases
 */
 'aliases' => [
    ...
    'Press' => Bryceandy\Press\Facades\Press::class,
 ],
  1. 注册字段类

在服务提供者中,您将找到 registerFields 方法,您需要在其中注册自定义字段类

private function registerFields()
{
    return [
        \App\Fields\Birthday::class,
    ];
}

发布文章

当您准备好发布文章或更新现有文章时,您可以运行以下命令或安排它在您想要的任何时候在后台运行

php artisan press:process

获取您的文章

如果您成功运行了前面的命令,这意味着您的文章已保存到您的文章表中。

您可以使用 Bryceandy\Press\Post 模型来获取文章。

自定义字段

在检索所有自定义字段时,您可以使用以下方式使用 extra 字段

$post = Bryceandy\Press\Post::all()->first();

// The field that was parsed with the Birthday class
$birthday = $post->extra('birthday');  

// The field that was only written in the markdown file without a custom class to parse
$field = $post->extra('field');   

// Other fields  
$title = $post->title;
$body = $post->body;  

即将推出的功能

🔘 支持云驱动程序,特别是 Amazon S3。

许可

MIT 许可证。

请随时提交拉取请求以贡献到此包。