Laravel-smartmd 一个实用 Markdown 编辑器兼容大部分主流的 Markdown 语法解析,包括数学公式、流程图等

Laravel-smartmd

Github https://github.com/NoisyWinds/laravel-smar...

Documentation | 中文文档

laravel-smartmd.svg?branch=master Software License Software License packagist

一个实用的 markdown 编辑器兼容大部分主流的 markdown 语法解析,您可以选择前后端的任意一种解析方式,包括数学公式、流程图、emoji 表情、上传图片等...这是一个 laravel 插件的项目,要求 laravel 版本大于等于 5.4, php 版本大于等于 7.1 ,更多功能和文档随缘更新...

效果截图

编辑器示例页面:Demo
js 渲染示例页面: Demo
php 渲染示例页面: Demo


参考和引用:

  • CodeMirror link
  • Simplemde-markdown link
  • markdown-it (markdown render) link
  • mermaid (flowchart) link
  • intervention (image handling) link

依赖于

  • PHP >= 7.1.0
  • Laravel >= 5.4.0

如何初始化

首先,安装 composer 的 noisywinds/laravel-smartmd 包:

composer require noisywinds/laravel-smartmd

将素材和配置文件迁移到项目:

php artisan vendor:publish --provider="NoisyWinds\Smartmd\SmartmdServiceProvider"

在 web.php 写入测试路径(后期可自行并入项目):

Route::group(['namespace' => 'Smartmd', 'prefix' => 'editor'], function () {
    Route::post('/upload', 'UploadController@imSave');
    Route::get('/write', function () {
        return view('vendor/smartmd/write');
    });
    Route::get('/php-show','ParseController@index');
    Route::get('/js-show',function(){
        return view('vendor/smartmd/js-show');
    });
});

重写 UploadController 或者 config/smartmd.php 来改变你的文件上传位置:

<?php
return [
    "image" => [
        /*
         * like filesystem, Where do you like to place pictures?
         */
        "root" => storage_path('app/public/images'),
        /*
         * return public image path
         */
        "url" => env('APP_URL').'/storage/images',
    ],
];
  • 注意: 上传的图片会经过优化和压缩,这些处理你可以在 UploadController 中修改(比如加水印)。

一些快捷键

  1. Bold (Ctrl + b)
  2. Italic (Ctrl + I)
  3. Insert Image (Ctrl + Alt + I)
  4. Insert Math (Ctrl + m)
  5. Insert flowchart (Ctrl + Alt + m)
  6. more... (mac command the same with ctrl)

一些编辑器的基础配置

var editor = new Smartmd({
        element: document.getElementById("editor"),
        // minheight default 30vh
        minHeight: "80vh",
        renderingConfig: {
            singleLineBreaks: false,
            // highlight (need highlight.js)
            codeSyntaxHighlighting: true,
        },
        autosave: {
            enabled: true,
            uniqueId: "write",
            delay: 1000,
        },
        autoCloseTags: true,
        matchTags: {bothTags: true},
        image:{
            // your UploadController route
            uploadPath:'./upload',
            type:['jpeg','png','bmp','gif','jpg'],
            // fileSize (kb)
            maxSize:4096,
        },
        // editor alert notice icon and color
        alertThemes:[
            {
                name: 'success',
                icon: 'fa fa-check-circle',
                color: '#38c172',
                defaultText: 'success'
            },
            {
                name: 'error',
                icon: 'fa fa-close-circle',
                color: '#e3342f',
                defaultText: 'Some things error'
            }
            //..add your themes and use editor.alert("themeName","text") to used;
        ]
        //...more see the docs
});

// diy editor drop function
var cm = smartmd.codemirror;
  cm.display.lineDiv.ondrop = function(ev){
       if(ev.target.className.indexOf("CodeMirror-line") > -1){
           // your drop down function
       }
       ev.preventDefault();
   }

解析 markdown

smartmd 提供了三种解析 markdown 的方式,您可以按照自己的需求通过不同方式渲染页面

不需要编辑器:

// require in your view meta
@include('Smartmd::js-parse')
<script>
    // create Parsemd object use javascript parse markdown
    var parse = new Parsemd();
    var html = parse.render(document.getElementById("editor").value.replace(/^\s+|\s+$/g, ''));
    document.getElementById("content").innerHTML = html;
</script>

需要编辑器:

<script>
    var smartmd = new Smartmd();
    smartmd.markdown("# hello world");
</script>

直接 php 返回 html :

  • 只有在 流程图、语法高亮、数学公式时通过 js 渲染:
// require in your view meta
@include('Smartmd::php-parse')

ParseController.php

use NoisyWinds\Smartmd\Markdown;
$parse = new Markdown();
$text = "# Your markdown text";
$html = $parse->text($text);
return view('Smartmd::php-show',['content'=>$html]);

如何拓展

1. 编辑器

参考 CodeMirror 的 开发手册

2. markdown 文本渲染

参考 markdown-it 的插件开发 链接
如需更改解析规则,后端可修改 Markdown.php, 前端需要修改 smartmd/mode/markdown.js

问题反馈

欢迎你在 issue 反馈你遇到的问题和你想兼容或者想拓展的需求,希望能给到你一些帮助。

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 5年前 自动加精
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 9

php7.1 怎么破

composer require noisywinds/laravel-smartmd

    1/1:        https://packagist.laravel-china.org/p/provider-latest$07f34de6a0                   cd828fb9857b78ad086823ae145333bdb499af1247e320009d0261.json
    Finished: success: 1, skipped: 0, failure: 0, total: 1
Using version ^1.0 for noisywinds/laravel-smartmd
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - noisywinds/laravel-smartmd v1.0.1 requires phpunit/phpunit ^7.4 -> satisfi                   able by phpunit/phpunit[7.4.0, 7.4.1, 7.4.2, 7.4.3, 7.4.4, 7.4.5, 7.5.0, 7.5.1,                    7.5.x-dev] but these conflict with your requirements or minimum-stability.
    - noisywinds/laravel-smartmd v1.0.0 requires phpunit/phpunit ^7.4 -> satisfi                   able by phpunit/phpunit[7.4.0, 7.4.1, 7.4.2, 7.4.3, 7.4.4, 7.4.5, 7.5.0, 7.5.1,                    7.5.x-dev] but these conflict with your requirements or minimum-stability.
    - Installation request for noisywinds/laravel-smartmd ^1.0 -> satisfiable by                    noisywinds/laravel-smartmd[v1.0.0, v1.0.1].

Installation failed, reverting ./composer.json to its original content.
5年前 评论
ThinkQ

加入laravel admin吧

5年前 评论

@mason 代码高亮要你自己引入highlight.js 和他们的 css

4年前 评论

@NoisyWind 这个解决了,粘贴上传怎么解决啊!

4年前 评论

@mason 现在只支持拖拽上传,粘贴你可以自己写一个,然后 commit 给我

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!