flask 添加markdown支持

news/2024/7/7 19:34:21

flask 添加markdown支持

flask blog 演示项目

Documentation = https://flask.palletsprojects.com/tutorial/
源码 https://github.com/pallets/flask/tree/main/examples/tutorial

利用 editor.md 开源库

https://github.com/pandao/editor.md

下载
重命名为 editormd
放在 static 目录下

D:\workspace\Python\blog\flaskr\templates\base.html

<!doctype html>
<title>{% block title %}{% endblock %} - wmx web</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="{{ url_for('static',filename='editormd/css/editormd.css') }}"/> 
<link rel="stylesheet" href="{{ url_for('static',filename='editormd/css/editormd.preview.css') }}"/>
<nav>
  <ul>
    <li><a href="{{ url_for('index') }}">Home</a></h1>
    {% if g.user %}
      <li><span>{{ g.user['username'] }}</span>
      <li><a href="{{ url_for('auth.logout') }}">Log Out</a>
    {% else %}
      <li><a href="{{ url_for('auth.register') }}">Register</a>
      <li><a href="{{ url_for('auth.login') }}">Log In</a>
    {% endif %}
  </ul>
</nav>
<section class="content">
  <header>
    {% block header %}{% endblock %}
  </header>
  {% for message in get_flashed_messages() %}
    <div class="flash">{{ message }}</div>
  {% endfor %}
  {% block content %}{% endblock %}
</section>

D:\workspace\Python\blog\flaskr\templates\blog\index.html

{% extends 'base.html' %}

{% block header %}
<h2>{% block title %}blogs{% endblock %}</h2>
{% if g.user %}
<a class="action" href="{{ url_for('blog.create') }}">New</a>
{% endif %}
{% endblock %}

{% block content %}
{% for post in posts %}
<article class="post">
  <header>
    <div>
      <h1>{{ post['title'] }}</h1>
      <div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
    </div>
    {% if g.user['id'] == post['author_id'] %}
    <a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
    {% endif %}
  </header>

  <button onclick="showBody('{{ loop.index }}')">显示内容</button>

  <div id="{{ loop.index }}" style="height:0px;">
    <textarea class="body" style="display:none;" >
      {{ post.body }}
     </textarea>
  </div>

</article>
{% if not loop.last %}
<hr>
{% endif %}

<!---------------------------------------------- scripts ---------------------------------------------->
<script src="{{ url_for('static',filename='editormd/examples/js/jquery.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/marked.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/prettify.min.js') }}"></script>

<script src="{{ url_for('static',filename='editormd/lib/raphael.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/underscore.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/sequence-diagram.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/flowchart.min.js') }}"></script>
<script src="{{ url_for('static',filename='editormd/lib/jquery.flowchart.min.js') }}"></script>

<script src="{{ url_for('static',filename='editormd/editormd.js') }}"></script>

<script type="text/javascript">
  $(function () {
    var objId = "{{ loop.index }}";
    testEditormdView = editormd.markdownToHTML(objId, {
      htmlDecode: "style,script,iframe",  // you can filter tags decode
      emoji: true,
      taskList: true,
      tex: true,  // 默认不解析
      flowChart: true,  // 默认不解析
      sequenceDiagram: true,  // 默认不解析
    });
  });
</script>


{% endfor %}



<script>
  function showBody(index) {
    var body = document.getElementById(index);
    if (body.style.height === '0px') {
        body.style.height = 'auto';
    } else {
        body.style.height = '0px';
    }
  }
</script>


{% endblock %}

D:\workspace\Python\blog\flaskr\templates\blog\create.html

{% extends 'base.html' %}

{% block header %}
  <h2>{% block title %}New Post{% endblock %}</h2>
{% endblock %}

{% block content %}
  <form method="post">
    <label for="title">Title</label>
    <input name="title" id="title" value="{{ request.form['title'] }}" required>
    <label for="body">Body</label>
    <div id="create-editormd">
      <textarea name="body" id="body">{{ request.form['body'] }}</textarea>
    </div>
    <input type="submit" value="Save">
  </form>

  <script src="{{ url_for('static',filename='editormd/examples/js/jquery.min.js') }}"></script>
  <script src="{{ url_for('static',filename='editormd/editormd.min.js') }}"></script>
<script type="text/javascript">
    var testEditor;

    $(function () {
        testEditor = editormd("create-editormd", {
            width: "90%",
            height: 640,
            syncScrolling: "single",
            path: "{{ url_for('static',filename='editormd/lib/') }}",
            // 上传图片
            imageUpload : true,
            imageFormats : [ "jpg", "jpeg", "gif", "png", "bmp", "webp" ],

        });
    });
</script>

{% endblock %}

D:\workspace\Python\blog\flaskr\templates\blog\update.html

{% extends 'base.html' %}

{% block header %}
  <h2>{% block title %}Edit "{{ post['title'] }}"{% endblock %}</h2>
{% endblock %}

{% block content %}
  <form method="post">
    <label for="title">Title</label>
    <input name="title" id="title" value="{{ request.form['title'] or post['title'] }}" required>
    <label for="body">Body</label>
    <div id="update-editormd">
      <textarea name="body" id="body">{{ request.form['body'] or post['body'] }}</textarea>
    </div>
    <input type="submit" value="Save">
  </form>
  <hr>
  <form action="{{ url_for('blog.delete', id=post['id']) }}" method="post">
    <input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
  </form>

  <script src="{{ url_for('static',filename='editormd/examples/js/jquery.min.js') }}"></script>
  <script src="{{ url_for('static',filename='editormd/editormd.min.js') }}"></script>
<script type="text/javascript">
    var testEditor;

    $(function () {
        testEditor = editormd("update-editormd", {
            width: "90%",
            height: 640,
            syncScrolling: "single",
            path: "{{ url_for('static',filename='editormd/lib/') }}",
            // 上传图片
            imageUpload : true,
            imageFormats : [ "jpg", "jpeg", "gif", "png", "bmp", "webp" ],
        });
    });
</script>

{% endblock %}


http://lihuaxi.xjx100.cn/news/1142775.html

相关文章

HBuilder开发uniapp添加android的模拟器的方法

我们知道使用uniapp开发多端app非常方便&#xff0c;开发过程中的模拟器也可以提高我们测试代码的效率。但我们按uniapp官网的方法&#xff0c;上google的官网下载模拟器&#xff0c;往往非常不方便。 下面我们来看一下使用其他模拟器的方法。 我们知道android开发中&#xf…

虹科新品 | 高可靠性、可适用于高磁/压的线性传感器!

PART 1 什么是线性传感器&#xff1f; 基本上&#xff0c;线性传感器是一种用于测量位移和距离的设备&#xff0c;具有高可靠性。测量网格通过光学传感器移动测量数据&#xff0c;数据被光学记录并通过控制器转换为电气数据&#xff0c;而控制器又可以转换为路径。 因此&…

DHCP+链路聚合+NAT+ACL小型实验

实验要求: 1.按照拓扑图上标识规划网络。 2.使用0SPF协议进程100实现ISP互通。 3.私网内PC属于VLAN1O, FTP Server属于VLAN2O,网关分 别为所连接的接入交换机&#xff0c;其中PC要求通过DHCP动态获取 4:私网内部所有交换机都为三层交换机&#xff0c;请合理规划VLAN&#…

华为云——代码托管的使用

一、打开前后端项目 登录华为云&#xff0c;点击页面右上角的用户名——点击个人设置 2.点击代码托管的HTTPS密码管理&#xff0c;设置自己的密码 3.回到代码仓库&#xff0c;复制HTTP地址 4.打开GitHubDesktop&#xff0c;点击左上角进行仓库克隆 &#xff08;我这里已经cl…

Linux速通 常用基本命令

大部分摘自《Linux 命令行与shell脚本编程大全》该书&#xff0c;少部分参考自csdn博客 目录 一、基本的bash shell 命令 1、文件和目录列表 基本列表功能 修改输出信息 过滤输出列表 2、处理文件 3、处理目录 4、查看文件内容 查看整个文件 查看部分文件 二、更多的…

15个对Web开发人员有用工具网站

1. 代码转图片 网址&#xff1a;https://carbon.now.sh/ 使用 Carbon 创建和分享源代码的精美图像。它提供了多种代码风格和主题。 3.图片图库 网址&#xff1a;https://unsplash.com/ 4. 智能 WebP、PNG 和 JPEG 图片压缩 网址&#xff1a;https://tinypng.com/ tiny…

为什么要使用Thrift与Protocol Buffers?

编码数据的格式 程序通常&#xff08;至少&#xff09;使用两种形式的数据&#xff1a; 在内存中&#xff0c;数据保存在对象、结构体、列表、数组、散列表、树等中。 这些数据结构针对 CPU 的高效访问和操作进行了优化&#xff08;通常使用指针&#xff09;。如果要将数据写…

江西省棒垒球协会成立大会暨第一届会员大会隆重举行·棒球1号位

大会隆重举行 3月18日&#xff0c;江西省棒垒球协会成立大会暨第一届会员大会于江西省体育宾馆举行。 出席本次成立大会的领导及嘉宾有&#xff1a;中国棒球协会秘书长谢斌&#xff1b;江西省体育总会秘书长熊伟&#xff1b;江西省民政厅三级主任科员王丹&#xff1b; 大会第…