. * */ /* Name: Markdown Editor Slug: markdown-editor Category: content Url: https://www.vvveb.com Description: Change post and product page editor to markdown, automatically render as html on frontend. Author: givanz Version: 0.1 Thumb: markdown-editor.svg Author url: https://www.vvveb.com */ use Vvveb\Plugins\MarkdownEditor\System\Parsedown as Parsedown; use Vvveb\System\Event; if (! defined('V_VERSION')) { die('Invalid request!'); } class MarkdownEditorPlugin { private $parsedown; private function markdownToHtml($markdown) { //escape html tags $markdown = str_replace(['<', '>'], ['<', '>'], $markdown); //escape code blocks $markdown = preg_replace_callback('/```.*?```/ms', function ($matches) { return str_replace(['<', '>'], ['<', '>'], $matches[0]); }, $markdown); /* $markdown = preg_replace_callback('/```.*?```/ms', function ($matches) { return htmlentities($matches[0]); }, $markdown);*/ $this->parsedown = new Parsedown(); $html = $text = $this->parsedown->text($markdown); //admonitions support $html = preg_replace_callback('/

:::(\w+)<\/p>(.*?)\n

:::<\/p>/ms', function ($matches) { $text = $matches[1]; $type = str_replace(['info', 'tip', 'note', 'caution', 'danger'], ['primary', 'info', 'secondary', 'warning', 'danger'], $text); $icon = str_replace(['primary', 'info', 'secondary', 'warning', 'danger'], ['🛈', '💡', 'ℹ', ' 🛆', '🛇'], $type); $message = strip_tags($matches[2], ''); return '

'; }, $html); return $html; } function addMarkdownEditor() { //add script on compile Event::on('Vvveb\System\Core\View', 'compile', __CLASS__, function ($template, $htmlFile, $tplFile, $vTpl, $view) { //insert js and css on post and product page if ($template == 'content/post.html' || $template == 'product/product.html') { //insert script $vTpl->loadTemplateFile(__DIR__ . '/admin/template/editor.tpl'); //$vTpl->addCommand('body|append', $script); } return [$template, $htmlFile, $tplFile, $vTpl, $view]; }); } function admin() { $this->addMarkdownEditor(); } function app() { //post component Event::on('Vvveb\Component\Post', 'results', __CLASS__, function ($results = false) { if ($results) { $results['content'] = $this->markdownToHtml($results['content']); } return [$results]; }); //posts component Event::on('Vvveb\Component\Posts', 'results', __CLASS__, function ($results = false) { if (isset($results['post'])) { foreach ($results['post'] as &$post) { $post['content'] = $this->markdownToHtml($post['content']); } } return [$results]; }); } function __construct() { if (APP == 'admin') { $this->admin(); } else { if (APP == 'app') { $this->app(); } } } } $markdownPlugin = new MarkdownEditorPlugin();