Django Markdown to HTML Field
Project description
Django Markdown to HTML Field
A Django model field for handling Markdown content with advanced sanitization and extensions support.
This field converts Markdown to HTML on the model instance save and stores converted HMTL to a specific text field.
Features
- Markdown field for Django models
- Automatic conversion to HTML, which will be stored in TextField
- Built-in HTML sanitization
- Support for superscript (^text^) and subscript (
text) syntax - Configurable HTML tag and attribute whitelisting
- Automatic handling of external links with nofollow
- Lazy loading for images
- Multi-language URL handling
- Support for django-modeltranslation
Installation
pip install django-markdown-html-field
Usage
Register in settings.py
INSTALLED_APPS = [
...
'django_markdown_html_field'
]
Add MarkdownField to your model
from django_markdown_html_field.fields import MarkdownField
class Page(models.Model):
content = models.TextField(verbose_name='Content', blank=True, null=True)
content_markdown = MarkdownField(verbose_name='Content - Markdown', html_field='content')
In this example, the Markdown content will be stored in the content_markdown field and the HTML will be stored in the content field.
Conversion will be executed automatically on the model instance save.
Configuration of classes for html tags
You can configure the classes for html tags in the settings.py file.
MARKDOWN_FIELD_SANITIZER_CONFIG = {
'img': ('img-fluid', 'd-block', 'mx-auto', 'mw-100', 'mvh-75'),
'table': ('table', 'table-bordered', 'table-hover')
}
Customize the sanitizer and worker
You can inherit from the sanitizer and worker and implement your own logic.
After that you can register your custom classes in the settings.py file.
For example:
MARKDOWN_FIELD_WORKER = 'your_app.markdown.DoFollowMarkdownWorker'
MARKDOWN_FIELD_SANITIZER = 'your_app.markdown.DoFollowHtmlSanitizer'
Example of custom worker and sanitizer
from django_markdown_html_field.worker import MarkdownWorker
from django_markdown_html_field.sanitizer import HtmlSanitizer
class DoFollowHtmlSanitizer(HtmlSanitizer):
def _add_rel_attr(self, tag, attr):
if self.kwargs.get('dofollow', False):
return
super()._add_rel_attr(tag, attr)
class DoFollowMarkdownWorker(MarkdownWorker):
def __init__(self, text, instance=None, html_sanitizer=None, **kwargs):
super().__init__(text=text, instance=instance, html_sanitizer=html_sanitizer, **kwargs)
if instance:
self.kwargs.update({'dofollow': getattr(instance, 'dofollow', False)})
Using Custom worker and sanitizer in specific model only
from django_markdown_html_field.fields import MarkdownField
from your_app.markdown import DoFollowMarkdownWorker, DoFollowHtmlSanitizer
class Page(models.Model):
content = models.TextField(verbose_name='Content', blank=True, null=True)
content_markdown = MarkdownField(
verbose_name='Content - Markdown',
html_field='content',
markdown_worker=DoFollowMarkdownWorker,
html_sanitizer=DoFollowHtmlSanitizer,
)
Using MarkdownWorker separately
from django.views import View
from django.http import JsonResponse
from django_markdown_html_field.sanitizer import HtmlSanitizer
from django_markdown_html_field.worker import MarkdownWorker
class MarkdownView(View):
"""
Markdown view for preview html content
"""
def post(self, request):
return JsonResponse({'preview': MarkdownWorker(request.POST.get('content'), HtmlSanitizer).generate_html()})
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_markdown_html_field-1.0.1.tar.gz.
File metadata
- Download URL: django_markdown_html_field-1.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39287629f2e1a75bc5a134d7974f9ada1a21c2adb09933297f1982f8ec38f89d
|
|
| MD5 |
c107eb2b14bb6d1225dcd4d948441b87
|
|
| BLAKE2b-256 |
4f5fadf2b497e3de261b83f1475cc0594c8d6973687e135bd50d89db993c4871
|
File details
Details for the file django_markdown_html_field-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_markdown_html_field-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0afedf5c1b594b629a3e60e311cd9a1016a0d4068ed95c1150c4c5864d5bc739
|
|
| MD5 |
3231ef3cc584b217e4b08c72f02bdffa
|
|
| BLAKE2b-256 |
a351aab8e5434e8957baf80d0572190a19fefac6b0a666d1469254b2c6c636b0
|