unicall / yii2-markdown
为 Yii Framework 2.0 提供高级 Markdown 编辑和转换工具
Requires
This package is not auto-updated.
Last update: 2024-10-01 22:28:42 UTC
README
本模块为 Yii Framework 2.0 提供Markdown编辑和转换工具。它通过 PHP Markdown Extra 和 PHP Smarty Pants 实现Markdown转换。此外,您可以通过包含额外的自定义转换模式来自定义Markdown的版本。该模块还包含一个增强的自定义Markdown编辑器小部件,用于运行时的Markdown编辑和预览。此小部件使用Bootstrap 3.0进行样式设计。请查看完整演示。
Markdown
`查看演示`
这是一个使用PHP Markdown Extra 和 PHP SmartyPantsTypographer 处理Markdown转换到HTML的Markdown转换类。它还支持可配置的自定义转换处理模式,在一定程度上支持自定义Markdown的风格。查看示例和详细说明或查看完整演示。
MarkdownEditor
`查看演示`
这是一个具有可配置选项的高级Markdown输入小部件。它使用Bootstrap 3.0进行样式设计。此小部件的关键功能包括
- 可配置的工具栏和按钮,用于格式化内容
- Markdown格式化文本的实时HTML预览
- 最大化编辑器以进行全屏编辑
- 实现了Markdown提供的PHP Markdown Extra和PHP SmartyPantsTypographer功能
- 尽可能使用Bootstrap 3.0进行样式设计
- 允许将文本编辑器内容保存/导出为文本或HTML
- 可配置标题、页脚和输入选项
- 支持本地化和自定义消息和内容
演示
您可以在此处查看这些功能的演示、文档和示例。
安装
安装此扩展的首选方法是使用 composer。
注意:检查此扩展的composer.json文件以获取扩展的要求和依赖项。阅读有关设置应用composer.json中的
minimum-stability设置的网络提示/wiki。
运行以下命令之一:
$ php composer.phar require opotemkin/yii2-markdown "dev-master"
或
"opotemkin/yii2-markdown": "dev-master"
将其添加到您的composer.json文件的require部分。
使用方法
设置模块
将markdown添加到您的Yii配置文件的模块部分
'modules' = [
/* other modules */
'markdown' => [
'class' => 'kartik\markdown\Module',
]
];
您可以为markdown模块设置额外的配置选项
'modules' = [
'markdown' => [
// the module class
'class' => 'kartik\markdown\Module',
// the controller action route used for markdown editor preview
'previewAction' => '/markdown/parse/preview',
// the list of custom conversion patterns for post processing
'customConversion' => [
'<table>' => '<table class="table table-bordered table-striped">'
],
// whether to use PHP SmartyPantsTypographer to process Markdown output
'smartyPants' => true
]
/* other modules */
];
Markdown
use kartik\markdown\Markdown;
// default call
echo Markdown::convert($content);
// with custom post processing
echo Markdown::convert($content, ['custom' => [
'<h1>' => '<h1 class="custom-h1">',
'<h2>' => '<h2 class="custom-h2">',
'<p>' => Html::beginTag('p', $options),
]]);
MarkdownEditor
// add this in your view
use kartik\markdown\MarkdownEditor;
// usage with model
echo MarkdownEditor::widget([
'model' => $model,
'attribute' => 'markdown',
]);
// usage without model
echo MarkdownEditor::widget([
'name' => 'markdown',
'value' => $value,
]);
Smarty模板
可以通过设置模块参数全局启用Smarty模板
'modules' = [
'markdown' => [
'class' => 'kartik\markdown\Module',
'smarty' => true,
// Smarty class configuration
'smartyParams' => [],
// provide Yii::$app to the Smarty template as variable
'smartyYiiApp' => true,
// provide Yii::$app->params to the Smarty template as config variables
'smartyYiiParams' => true,
],
/* other modules */
];
然后在编辑器中定义Smarty
echo MarkdownEditor::widget([
'model' => $model,
'attribute' => 'markdown',
'smarty' => true,
]);
注意,全局启用Smarty模板可能不是明智之举。您可以设置模块属性smarty为可调用的函数,并提供RBAC功能。
'modules' = [
'markdown' => [
'class' => 'kartik\markdown\Module',
'smarty' => function($module) {
if (\Yii::$app->user->can('smarty')) {
if(\Yii::$app->user->can('smartyYiiApp'))
$module->smartyYiiApp=true;
else
$module->smartyYiiApp=false;
if(\Yii::$app->user->can('smartyYiiParams'))
$module->smartyYiiParams=true;
else
$module->smartyYiiParams=false;
return true;
}
return false;
}
],
/* other modules */
];
可能更好的选择是在配置文件中关闭smarty,并在小部件设置中在视图中打开它。
echo MarkdownEditor::widget([
'model' => $model,
'attribute' => 'markdown',
'smarty' => true,
'previewAction' => Url::to(['my/preview']),
]);
然后在控制器中创建一个动作并实现RBAC。这样,Smarty模板默认是关闭的,你可以在控制器中打开它并控制对其的访问。
class MyController extends Controller
{
public function actionPreview()
{
$module = Yii::$app->getModule('markdown');
if (\Yii::$app->user->can('smarty')) {
$module->smarty = true;
$module->smartyYiiApp = \Yii::$app->user->can('smartyYiiApp') ? true : false;
$module->smartyYiiParams = Yii::$app->user->can('smartyYiiParams') ? true : false;
}
if (isset($_POST['source'])) {
$output = (strlen($_POST['source']) > 0) ? Markdown::convert($_POST['source'], ['custom' => $module->customConversion]) : $_POST['nullMsg'];
}
echo Json::encode(HtmlPurifier::process($output));
}
}
将值保存到数据库后,你可以使用Markdown::convert()在视图中渲染它。例如,如果你在Post表的content列中保存Markdown字段,你可以使用以下类似的内容。
$content = Post::find(['page_id'=>'myPage'])->one()->content;
echo HtmlPurifier::process(Markdown::convert($content, ['custom' => $module->customConversion]))
许可证
yii2-markdown是在BSD 3-Clause许可证下发布的。有关详细信息,请参阅打包的LICENSE.md文件。