Flask Study record Markdown Edit text

Source: Internet
Author: User

To allow Web pages to support markdown editing text, use the following 4 libraries

    • PageDown: Provides an editor on the front end that can convert markdown content to HTML text in real-time for an effect preview
    • Flask-pagedown: This library integrates PageDown into the FLASK-WTF library for easier use
    • MarkDown: Converting MarkDown markup text to HTML text
    • Bleach: Clear unsafe tags in HTML text based on whitelist

Use of PageDown

Acquaintance with other class libraries, initialization required

 from Import  = PageDown (APP)

Because PageDown is in front of you with the editor provided by JS, so similar to the moment library needs to include him in the template file, as follows:

{{Pagedown.include_pagedown ()}}

Flask-pagedown integrates the PageDown library into FLASK-WTF, so it can be used directly as a field of wtforms, and Wtf.quick_form () can handle his display well.

 from Import Pagedownfield class Postform (Form):     = Pagedownfield (U" micro-blog content ", validators=[validators. DataRequired ()])    = Submitfield (u' commit ')
Use of Markdown

Markdown can convert markdown markup text into HTML text into the database, using it as simple as:

 from Import  'html')# The first argument is markdown the original text, the second parameter is passed into the type of the transformation
Use of Bleach

Bleach is a white-list-based HTML filter that filters out unsafe tags to ensure that the security Doc

Filter label Signature

ImportBleach#Allowed LabelsAllow_tags = ['a','abbr','acronym','b','blockquote','Code','em','I','Li','ol','Pre','Strong','ul','H1','H2','H3','P']cleaned_text=Bleach.clean (html_text, tags=Allow_tags, strip=True)

Filter Properties:

ImportBleach#Allowed LabelsAllow_tags = ['a','abbr','acronym','b','blockquote','Code','em','I','Li','ol','Pre','Strong','ul','H1','H2','H3','P']#Allowed Properties#This setting will not filter the class property of all tags, and the Href,rel property of the A tag ....Attrs = {    '*': ['class'],    'a': ['href','rel'],    'img': ['src','alt'],}cleaned_text=Bleach.clean (html_text, tags=Allow_tags, strip=True, Attrs=attrs)
Other

Save in the database when the markdown text converted to HTML and then clean up, and finally save the safe HTML text in the database, because the unsafe label has been filtered so in the template can be added safe filter

Flask Study record Markdown Edit text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.