This module provides Markdown Editing and Conversion utilities for Yii Framework 2.0. It implements markdown conversion using PHP Markdown Extra and PHP Smarty Pants. In addition, you can customize the flavor of Markdown, by including additional custom conversion patterns. The module also includes an enhanced customized Markdown Editor Widget for markdown editing and preview at runtime. This widget is styled using Bootstrap 3.0. View a complete demo.
VIEW DEMO
This is an advanced markdown input widget with configurable options. It is styled using Bootstrap 3.0. Key features available with this widget are:
Configurable toolbar and buttons for formatting content
Live preview of Markdown formatted text as HTML
Maximize editor for full screen editing
Implements PHP Markdown Extra and PHP SmartyPantsTypographer functionality as provided by the Markdown.
Uses Bootstrap 3.0 styling wherever possible
Allows saving/exporting of the text-editor contents as Text or HTML
Configurable header, footer, and input options.
Supports localization and customization of messages and content.
You can see a demonstration here on usage of these functions with documentation and examples.
Installation
The preferred way to install this extension is through composer.
Note: Check the composer.json for this extension's requirements and dependencies.
Read this web tip /wiki on setting the minimum-stability settings for your application's composer.json.
You can setup additional configuration options for the markdown module:
'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 callechoMarkdown::convert($content);
// with custom post processingechoMarkdown::convert($content, ['custom' => [
'<h1>' => '<h1 class="custom-h1">',
'<h2>' => '<h2 class="custom-h2">',
'<p>' => Html::beginTag('p', $options),
]]);
MarkdownEditor
// add this in your viewuse kartik\markdown\MarkdownEditor;
// usage with modelechoMarkdownEditor::widget([
'model' => $model,
'attribute' => 'markdown',
]);
// usage without modelechoMarkdownEditor::widget([
'name' => 'markdown',
'value' => $value,
]);
Smarty Templates
Smarty templates can be enabled globally by setting the module params
'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 */
];
Note that it may be unwise to enable Smarty templates globally. You can set the module property smarty to a callable function and provide RBAC features.
Then create an action in your controller and implement RBAC there. That way Smarty templates is off by default and you can
turn it on and control access to it in the Controller.
After saving the value to the database you can render it in your views with Markdown::convert(). For example if you save the Markdown field in the content column of the Post table you can use something like the following.
请发表评论