A markdown field.
Project description
django-markdownfield
A simple custom field for Django that can safely render Markdown and store it in the database.
Your text is stored in a MarkdownField
. When the model is saved, django-markdownfield will
parse the Markdown, render it, sanitise it with bleach, and store
the result in a RenderedMarkdownField
for display to end users.
django-markdownfield also implements the EasyMDE editor in admin views to make working with Markdown easier.
This can be disabled with MARKDOWN_EASYMDE=False
in your Django configuration.
Implementation
Implementing django-markdownfield is simple. See the below example.
from django.db import models
from markdownfield.fields import MarkdownField, RenderedMarkdownField
from markdownfield.validators import VALIDATOR_STANDARD
class Page(models.Model):
text = MarkdownField(rendered_field='text_rendered', validator=VALIDATOR_STANDARD)
text_rendered = RenderedMarkdownField()
Please also set SITE_URL
in your Django configuration - it will be needed for detecting
external links.
SITE_URL = "https://example.com"
Validators
django-markdownfield comes with a number of validators, which are used to process and clean the output of the markdown engine
VALIDATOR_STANDARD
from markdownfield.validators import VALIDATOR_STANDARD
This validator strips any tags that are not used by standard Markdown. It also automatically links
any URLs in the output, adding class="external"
, rel="nofollow noopener noreferrer"
, and
target="_blank"
to any URLs which it determines to be external.
VALIDATOR_CLASSY
from markdownfield.validators import VALIDATOR_CLASSY
This validator does much the same as VALIDATOR_STANDARD
, but it allows you to set the class on
links and images. This is useful to create buttons and other enhanced links.
VALIDATOR_NULL
from markdownfield.validators import VALIDATOR_NULL
This validator does not call bleach to sanitize the output at all. This is not safe for user input. It allows arbitrary (unsafe) HTML in your markdown input.
Creating Custom Validators
To create a custom validator, just create an instance of the markdownfield.validators.Validator
dataclass. An example of this is shown below.
from markdownfield.validators import Validator
# allows only bold and italic text
VALIDATOR_COMMENTS = Validator(
allowed_tags=["b", "i", "strong", "em"],
allowed_attrs={},
linkify=False
)
You can also find a standard set of markdown-safe tags and attrs in markdownfield.validators
, and extend
that.
from markdownfield.validators import Validator, MARKDOWN_TAGS, MARKDOWN_TAGS
# allows all standard markdown features,
# but also allows the class to be set on images and links
VALIDATOR_CLASSY = Validator(
allowed_tags=MARKDOWN_TAGS,
allowed_attrs={
**MARKDOWN_TAGS,
'img': ['src', 'alt', 'title', 'class'],
'a': ['href', 'alt', 'title', 'name', 'class']
}
)
License
This software is released under the MIT license.
Copyright (c) 2019 Luke Rogers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-markdownfield-0.2.6.tar.gz
.
File metadata
- Download URL: django-markdownfield-0.2.6.tar.gz
- Upload date:
- Size: 110.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c71dfec245aab77d26fb8b3ffb7efb5cea8c48039402a0ad709c1ced3f3c8ef0 |
|
MD5 | 2c767bb3fb18b2606ff65da9cfbe9a7d |
|
BLAKE2b-256 | 7c600dc43abfe33d93c8321f8fb2db6b44426608bcbfb6d74f4091326bf24243 |