linkaixiang4883/yii2-markdown

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

安装: 7

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 41

类型:yii2-extension

dev-master / 1.3.x-dev 2023-12-15 03:52 UTC

This package is auto-updated.

Last update: 2024-09-15 05:18:28 UTC


README

Krajee Logo
yii2-markdown

Stable Version Unstable VersionLicense License Total Downloads Monthly Downloads Daily Downloads

原始模块由 kartik-v 提供

此模块为Yii Framework 2.0提供Markdown编辑和转换工具。它使用PHP Markdown Extra和PHP Smarty Pants实现Markdown转换。此外,您可以通过包含额外的自定义转换模式来自定义Markdown的版本。该模块还包括一个增强的Markdown编辑器小部件,用于实时编辑和预览。这个小部件使用Bootstrap 3.0进行样式设计。查看完整演示

Markdown

查看演示
这是一个Markdown转换类,它使用PHP Markdown ExtraPHP SmartyPantsTypographer将Markdown转换为HTML。它还支持配置自定义的转换模式,以在一定程度上自定义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设置的网络提示/维基

运行以下命令之一:

$ php composer.phar require linkaixiang4883/yii2-markdown "dev-master"

或将其添加到composer.json文件的require部分:

"linkaixiang4883/yii2-markdown": "dev-master"

使用

使用

设置模块

markdown添加到您的Yii配置文件的模块部分

'modules' => [
	/* other modules */
	'markdown' => [
		'class' => 'linkaixiang4883\markdown\Module',
	]
];

您可以为markdown模块设置额外的配置选项

'modules' => [
	'markdown' => [
		// the module class
		'class' => 'linkaixiang4883\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 linkaixiang4883\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 linkaixiang4883\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' => 'linkaixiang4883\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' => 'linkaixiang4883\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 = Post::find(['page_id'=>'myPage'])->one()->content;
echo HtmlPurifier::process(Markdown::convert($content, ['custom' => $module->customConversion]))

许可证

yii2-markdown在BSD 3-Clause许可证下发布。有关详细信息,请参阅捆绑的LICENSE.md文件。