python - flask+jinja2+sqlite3,如何保存markdown格式的文章
高洛峰
高洛峰 2017-04-17 17:38:35
0
2
692
  1. 背景:

    • 环境如题:win + python3 + flask + jinja。

    • 初学python,在做一个博客工具练习flask,sql目前只有入门知识。

    • 目标是想在发布文章的时候,支持markdown。

  2. 问题:标题/加粗等都没有问题,但是在插入图片/超链接的时候,拼接的sql语句会被文章(已经被转化为html)中的引号等符号破坏导致报错。

  3. 我做的尝试:

    • xxx.replace(""", "\"").replace("\\", "\"),实测并没有用。

    • 使用HTMLParser的时候,发现依赖的markupbase包没有(pip3 install 提示没找到,豆瓣源)。

  4. 想请教的是:

    • 是否需要把网页存到数据库中去?(或者只存markdown文本到数据库,展示的时候再渲染?)假如不应该,有什么好办法么?

    • 假如应该存数据库,有什么办法吧html代码转化成sql安全?比如还有需要替换的字符我没有注意到?或者有现成的库?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
阿神

You can view my answers to similar questions Flask implements article editing interface

Flask already has a plug-in for processing MD, Flask-Pagedown. However, you can also just use the mardown library. For two questions: Flask已经有了处理MD的插件,Flask-Pagedown。不过也可以只使用mardown这个库。对于两个问题:

  1. 是否需要把网页存到数据库中去?(或者只存markdown文本到数据库,展示的时候再渲染?)假如不应该,有什么好办法么?
    Flask作者写的书《Flask Web开发:基于Python的Web应用开发实战》中,是将HTML和markdown文本都保存在数据库中,浏览的时候直接取HTML,编辑的时候取markdown,将markdown保存到数据库的时候随便解析为HTML也保存

  2. 假如应该存数据库,有什么办法吧html代码转化成sql安全?比如还有需要替换的字符我没有注意到?或者有现成的库?
    如果使用markdown

    1. Do I need to save the web page in the database? (Or just save the markdown text to the database and then render it when displaying?) If not, is there any good way?
      In the book "Flask Web Development: Practical Web Application Development Based on Python" written by the author of Flask, both HTML and markdown text are saved in the database, and the HTML is fetched directly when browsing. Take markdown when editing, and when saving markdown to the database, parse it into HTML and save it as well
  3. If it should be stored in the database, is there any way to convert the html code into sql safely? For example, are there any characters that need to be replaced that I didn't notice? Or is there a ready-made library?
    If you use the markdown library, call it directly:

    #🎜🎜# #🎜🎜#
    html = markdown.markdown(md_text)
    #🎜🎜#If using plug-in: #🎜🎜#
    class Article(db.Model):
        ...
        @staticmethod
        def on_changed_body(target, value, oldvalue, initiator):
            # 需要转换的标签
            allowed_tags = [
                'a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
                'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
                'h1', 'h2', 'h3', 'p', 'img'
            ]
            # 需要提取的标签属性,否则会被忽略掉
            attrs = {
                '*': ['class'],
                'a': ['href', 'rel'],
                'img': ['src', 'alt']
            }
            target.content_html = bleach.linkify(
                bleach.clean(
                    markdown(value, output_format='html'),
                    tags=allowed_tags,
                    attributes=attrs,
                    strip=True
                )
            )
    
黄舟

The markdown text is stored in the database, and the server implements a filter in jinja2 to render HTML and display markdown. The filter I defined is as follows:

from app import app
from flask import Markup
import markdown2

@app.template_filter('neomarkdown')
def neomarkdown(markdown_content):
    content=Markup(markdown2.markdown(markdown_content, extras=["tables"]))
    return content

Use:

{{ html_content|neomarkdown }}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template