opotemkin/yii2-markdown

为 Yii Framework 2.0 提供高级 Markdown 编辑和转换工具

安装: 646

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 41

类型:yii2-extension

v1.6.0 2018-03-22 19:01 UTC

This package is not auto-updated.

Last update: 2024-09-29 07:15:59 UTC


README

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

此模块为 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进行样式设计。此小部件的关键功能包括

  1. 可配置的工具栏和按钮,用于格式化内容
  2. Markdown格式文本的实时HTML预览
  3. 最大化编辑器以进行全屏编辑
  4. 实现Markdown提供的PHP Markdown Extra 和 PHP SmartyPantsTypographer功能
  5. 尽可能使用Bootstrap 3.0进行样式设计
  6. 允许将文本编辑器的内容保存/导出为文本或HTML
  7. 可配置标题、页脚和输入选项
  8. 支持本地化和自定义消息和内容

查看 示例和详情 或查看 完整演示

演示

您可以在这里看到这些功能的演示、文档和示例。

安装

安装此扩展的首选方法是通过 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()将其渲染。例如,如果你将Markdown字段保存到Post表的content列中,你可以使用以下类似的内容。

$content = Post::find(['page_id'=>'myPage'])->one()->content;
echo HtmlPurifier::process(Markdown::convert($content, ['custom' => $module->customConversion]))

许可证

yii2-markdown是在BSD 3-Clause许可证下发布的。请参阅附带的LICENSE.md以获取详细信息。