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