Skip to main content

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

django_markdown_html_field-1.0.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_markdown_html_field-1.0.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file django_markdown_html_field-1.0.2.tar.gz.

File metadata

  • Download URL: django_markdown_html_field-1.0.2.tar.gz
  • Upload date:
  • Size: 8.1 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

Hashes for django_markdown_html_field-1.0.2.tar.gz
Algorithm Hash digest
SHA256 64093dad6ba238a5c95a330ce881d811c6333452a9ce42f936aafb6c0a760b74
MD5 1b9d7ddfbdbe295f6ec76715da00e1fb
BLAKE2b-256 8f6b47ae9cc4ca0813d574a5ba791b5511bee3d684f3334d8929d75a30563342

See more details on using hashes here.

File details

Details for the file django_markdown_html_field-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_markdown_html_field-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ae9dbef9b396363b3ce44a16d6f37704f3ca0f68b1bf4dbdc3016452290a92fa
MD5 e66bdd9b9f09f1af88cec94e3c56fba9
BLAKE2b-256 b797d75956a1b94b8e501f3d2a666a5b54eba61c945bf93dc6d8bb5703004886

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page