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.0.tar.gz (7.9 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.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_markdown_html_field-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 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.0.tar.gz
Algorithm Hash digest
SHA256 c985af2e75a55b345cd1d46079e93dc7de258e80b2f578bb456790c4cd893d23
MD5 29d835a8e384f475cfba411924b0cd9e
BLAKE2b-256 292d4258dd4488cfaeffdce79c5fe3d84d07b1b619fa0d3d1dae8905c284dfdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_markdown_html_field-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ae0fc6d4fb86f55cb2e0862df52d343eee585d6401a3e5080ec13e453a569d7
MD5 c67c2eb045123ccfc2ad3142e5c2ba3a
BLAKE2b-256 61a2014dfa4e763522fb517aa11c9e3f9cc7fef8b013785ebe0a58830a043548

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