• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Editor markdown在flask上使用

武飞扬头像
lengyer
帮助1

html代码

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    <link rel="stylesheet" href="/editor/css/editormd.css">
  7.  
    </head>
  8.  
    <body>
  9.  
    <script type="text/javascript" src="/js/jquery-3.4.1.min.js"></script>
  10.  
    <script src="/editor/editormd.js"></script>
  11.  
    <script src="/editor/editormd.min.js"></script>
  12.  
    <div class="container" style="margin-top: 20px;">
  13.  
    <div class="row">
  14.  
    <div class="col-12" style="padding: 1px 0px">文章标题
  15.  
    <input type="text" class="form-control" id="headline">
  16.  
    </div>
  17.  
    <div class="col-12" id="test-editor">
  18.  
    <textarea style="display:none;" id="content"></textarea>
  19.  
    </div>
  20.  
    <button onclick="doPost()">发布文章</button>
  21.  
    </div>
  22.  
    </div>
  23.  
    </body>
  24.  
    </html>
学新通

JS代码段

  1.  
    <script type="text/javascript">
  2.  
    $(function () {
  3.  
    var editor = editormd("test-editor", {
  4.  
    width: "100%",
  5.  
    height: "500px",
  6.  
    path: "/editor/lib/", //依赖lib文件夹路径
  7.  
    saveHTMLToTextarea: true, // 获取用户编辑的内容放到textarea中
  8.  
    emoji: true, //emoji 表情,默认关闭
  9.  
    // htmlDecode: "style,script,iframe|on*", // 开启 HTML 标签解析,为了安全性,默认不开启
  10.  
    syncScrolling: "single",
  11.  
    breaks: true, //允许换行
  12.  
    /*上传图片文件*/
  13.  
    imageUpload: true, // 开启上传文件功能
  14.  
    imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"], // 上传图片的格式
  15.  
    imageUploadURL: "/image/upload" //后端上传图片的地址
  16.  
    });
  17.  
    });
  18.  
    function doPost() {
  19.  
    var headline = $("#headline").val();
  20.  
    var content = $("#content").val();
  21.  
    console.log(headline);
  22.  
    $.post('/editor/post', {
  23.  
    'headline': headline,
  24.  
    'content': content
  25.  
    },function (data) {
  26.  
    if (data.success)
  27.  
    window.alert('发布成功');
  28.  
    else {
  29.  
    window.alert('发布失败');
  30.  
    }
  31.  
    },'json')
  32.  
    }
  33.  
    </script>
学新通

后端保存image图片

  1.  
    @app.route('/image/upload', methods=['POST'])
  2.  
    def imageUpload():
  3.  
    # 获取文件
  4.  
    file = request.files.get('editormd-image-file')
  5.  
    # 如果没有文件返回上传失败
  6.  
    if not file:
  7.  
    res = {
  8.  
    'success': 0,
  9.  
    'message': '上传失败'
  10.  
    }
  11.  
    else:
  12.  
    # 分割文件获取后缀
  13.  
    ex = os.path.splitext(file.filename)[1]
  14.  
    # 分割文件获取文件名
  15.  
    fname = os.path.splitext(file.filename)[0]
  16.  
    # 重命名文件时间 后缀名
  17.  
    filename = datetime.now().strftime('%Y%m%d%H%M%S') ex
  18.  
    # 保存文件
  19.  
    file.save(fr'static/upload/{filename}')
  20.  
    # 返回上传成功
  21.  
    res = {
  22.  
    'success': 1,
  23.  
    'message': '上传成功',
  24.  
    'url': url_for('image', name=filename)
  25.  
    }
  26.  
    return jsonify(res)
学新通

后端保存文章代码段

  1.  
    # 保存文章
  2.  
    @app.route('/editor/post', methods=['POST'])
  3.  
    def editorPost():
  4.  
    headline = request.form['headline']
  5.  
    credate = datetime.now().strftime('%Y-%m-%d')
  6.  
    content = request.form['content']
  7.  
    filename = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
  8.  
    file_path = f'static//md//{headline}-{filename}.md'
  9.  
    with open(file_path, 'w', encoding='utf-8') as f:
  10.  
    f.write(content)
  11.  
    return 'Success'

url获取图片代码段

  1.  
    # 获取图片地址
  2.  
    @app.route('/image/<name>', methods=['GET', 'POST'])
  3.  
    def image(name):
  4.  
    with open(os.path.join('static//upload', name), 'rb') as f:
  5.  
    resp = Response(f.read(), mimetype="image/jpeg")
  6.  
    return resp

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbgkhf
系列文章
更多 icon
同类精品
更多 icon
继续加载