PHP code example of kartik-v / yii2-markdown

1. Go to this page and download the library: Download kartik-v/yii2-markdown library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kartik-v / yii2-markdown example snippets


'modules' => [
	/* other modules */
	'markdown' => [
		'class' => 'kartik\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 */
];

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),
]]);

// 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,
]);

'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 */
];

echo MarkdownEditor::widget([
    'model' => $model, 
    'attribute' => 'markdown',
    'smarty' => true,
]);

'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 */
];

echo MarkdownEditor::widget([
    'model' => $model, 
    'attribute' => 'markdown',
    'smarty' => true,
    'previewAction' => Url::to(['my/preview']),
]);

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));
    }
}

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

$ php composer.phar