corwatts/yii2-markdown-files
Composer 安装命令:
composer require corwatts/yii2-markdown-files
包简介
A simple flat-file Markdown renderer
README 文档
README
yii2-markdown-files provides a simple way to write updates, posts, or blocks of text in individual Markdown files with YAML frontmatter, render them on the fly and use the rendered HTML and frontmatter however you like.
Give updates on your Yii2 site or have a list of posts for a simple blog or news feed. Store your posts in version control with the rest of your code. No database required!
The idea for this extension was influenced by Jekyll, a static website generator written in Ruby.
Installation
Install via composer:
composer require 'corwatts/yii2-markdown-files'
Configuration
Enable the module by adding the snippet below to your main.php configuration file.
'modules' => [ 'blog' => [ // name this module what you like 'class' => \corwatts\MarkdownFiles\Module::className(), 'posts' => '@frontend/views/blog/posts', 'drafts' => '@frontend/views/blog/drafts', ] ],
class: is the namespaced class for this moduleposts: is a path pointing to the directory containing publishable markdown files. The path can contain Yii2 aliases.drafts: is a path pointing to the directory containing markdown files that aren't quite ready for publishing. The path can contain Yii2 aliases. Drafts are only rendered in the Yii2devenvironment.
Note: If you're going to use the included console command ensure this configuration is added somewhere the console application can access (like common/config/main.php).
Usage
Before rendering and displaying posts the individual post files must be created. A simple way to scaffold new posts is using the console command included in this extension. See below for instructions on how to set it up and use it.
It is easy to create new posts without the included console command. Posts follow a specific ruleset:
- Create a file in the
postsordraftsdirectory path specified in the module configuration above. - Similar to Jekyll, the filename has a specific format. It should start with the date (YYYY-MM-DD format) followed by a snake_cased description, and ending with the
.mdextension. Something like2017-05-20_test_post_1.md. When these files are processed the date is extracted from the filename. The rest of the descriptive filename is not used at this time.
Post Template
A basic post template looks like this:
--------- author: "Your Name" title: "Blog Title" --------- A post
The default YAML variables are author and title. You can set your own custom variables here too, and they will be available on the rendered side.
After running files through parseFiles() you'll have an array of arrays that look like this:
[
[
'date' => [
'year' => '2017',
'month' => '05',
'day' => '12',
'full' => '2017-05-12',
'name' => 'hello_world'
],
'yaml' => [
'title' => 'Blog Title',
'author' => 'Your Name'
],
'content' => '<p>A post</p>'
],
...[other posts]...
]
You can then iterate over the arrays and render the posts in your view partial.
$posts = \Yii::$app->getModule('blog'); // get module instance ->fetch() // get a list of markdown files ->parse() // parse the list of files ->result; // grab the array of parsed files return $this->render('index', ['posts'=>$posts]); //render the view
Protip: Cache the results to avoid having to recompile all your posts on every page hit.
Now you can render this data in a simple partial like this:
<?php use \yii\helpers\Html; foreach($posts as $file) { $yaml = $file['yaml']; $content = $file['content']; $date = Html::encode(date('F j, Y', strtotime($file['date']['full']))); print "<h4>".Html::encode($yaml['title'])."</h4>"; print "<em>Written by ".Html::encode($yaml['author'])." on $date</em>"; print $content; }
Console Command
Yii2-markdown-files provides a console command that plugs into the ./yii command line tool. You can use it to easily generate new posts. By default it provdes the 'blog/create' command.
To enable this, bootstrap the BlogController by adding it to the bootstrap array in your console configuration. Likely console/config/main.php:
'bootstrap' => ['blog'],
Now you can execute ./yii blog/create hello_world. Provide a descriptive snake_cased slug like my_first_post. It will generate a post template and print out its location. Edit this template to write your post.
Testing
Unit tests are included with this codebase. First scaffold the testing framework via composer test-scaffold (this only needs to be ran the first time). The unit tests can then be ran via composer test and coverage can be generated via composer test-coverage.
corwatts/yii2-markdown-files 适用场景与选型建议
corwatts/yii2-markdown-files 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.63k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「blog」 「markdown」 「flat」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 corwatts/yii2-markdown-files 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 corwatts/yii2-markdown-files 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 corwatts/yii2-markdown-files 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Laravel package for opinionated blog implementation
Adds more BBCode
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
统计信息
- 总下载量: 3.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-05-25