hyn/eloquent-markdown

将markdown、frontmatter和eloquent模型结合,轻松将带有元信息的markdown文件作为对象处理。

1.1 2016-12-14 21:54 UTC

This package is auto-updated.

Last update: 2024-09-05 19:04:35 UTC


README

GitHub license Latest Stable Version Build Status Total Downloads Donate

是否曾觉得你的markdown文件可以使用元信息... 一旦添加了frontmatter逻辑,处理这些文件是否会更人性化...

那么,让我们将markdown文件、frontmatter和eloquent结合起来!

如下所示

{
  "title": "Some elaborate .."
}
And of course your regular markdown nonsense.

转换为对象

echo $page->title; // Some elaborate ..
echo $page->getRenderedMarkdown(); // <p>And of course your regular markdown nonsense.</p>
echo $page->getMarkdown(); // And of course your regular markdown nonsense.
$page->setMarkdown('Foojaa'); // Yes update
$page->markdown = 'Foobar'; // Or on the assigned property
$page->save(); // Write the file to disk, YES!

安装

composer require hyn/eloquent-markdown

现在创建一个用于markdown文件的模型

class Page extends \Hyn\Eloquent\Markdown\Model
{}

设置文件系统和markdown解析器解析,在AppServiceProvider或其它地方添加

use Hyn\Eloquent\Markdown\Model;
use Hyn\Frontmatter\Parser;
use cebe\markdown\Markdown;

// ..

public function register() {
            Model::setMarkdownParser(new Parser(new Markdown));
            Model::setFilesystem($this->app->make('filesystem')->disk('content'));
}

content设置为配置为加载markdown文件的磁盘。或者实例化自己的文件系统实例。

使用

所以如果你有一个文件some/foo.md,使用Page::find('some/foo.md');来创建一个Page对象,其中任何frontmatter元信息都存储为属性,markdown内容存储在原始状态中作为markdown属性,生成的html分配给contents属性。

其他功能

  • save()
  • delete()
  • 更新markdown属性以自动更新生成的内容属性