django-markymark

Latest Version CI Status Coverage Status Documentation Status

django-markymark provides helpers and tools to integrate markdown into Django.

Features

  • Django form fields to integrate the bootstrap markdown editor (without the dependency on bootstrap)

  • django-filer integration

  • django-anylink integration

Requirements

django-markymark supports Python 3 only and requires at least Django 3.2

Prepare for development

A Python 3 interpreter is required in addition to Poetry.

$ poetry install

Now you’re ready to start the example project to experiment with markymark.

$ poetry run python examples/manage.py runserver

Resources

Contents:

Installation

  • Install with pip:

    pip install django-markymark
    
  • Your INSTALLED_APPS setting:

    INSTALLED_APPS = (
        # ...
        'markymark',
    )
    

Configuration for Django-CMS

If you plan to use django-markymark in a Django-CMS project, you should change the following two settings to make sure there are no iconlibrary problems:

MARKYMARK_FONTAWESOME_CSS = None
MARKYMARK_ICONLIBRARY = 'fa4'

Configuration

Markymark uses settings to make the markdown field work in the django admin site.

Django Settings

MARKYMARK_EXTENSIONS

A list with extra extensions for the markdown field.

Default extensions:

MARKYMARK_EXTENSIONS = [
    'markymark.extensions.autolink',
]
MARKYMARK_FONTAWESOME_CSS

By default, the markdown editor uses FontAwesome to show some nice icons. FontAwesome is vendored and shipped with this library. You can override the path of the FontAwesome css file to use either the CDN version or something completly different.

# Override default setting.
MARKYMARK_FONTAWESOME_CSS = 'https://use.fontawesome.com/releases/v5.2.0/css/all.css'
MARKYMARK_ICONLIBRARY

By default, the markdown editor uses Font Awesome 5 compatible css classes for icons. In addition, you can change the used icon library to Font Awesome 4.

To change the icon classes to Fa4, use this setting:

MARKYMARK_ICONLIBRARY = 'fa4'

Extensions

Python Markdown

Regular extensions from Python Markdown for extra markdown features, can be used with markymark.

MARKYMARK_EXTENSIONS = [
    '..',
    'markdown.extensions.codehilite',
    'markdown.extensions.fenced_code',
    'markdown.extensions.tables',
]

Django-filer

An extension for django-filer a file and image management application for django.

The following needs to be added to the settings:

MARKYMARK_EXTENSIONS = [
    '..',
    'markymark.extensions.filer',
]

This extension has extra dependencies that need to be installed:

$ pip install django-markymark[filer]

Note

For this extension additional settings are required that can be found in the django-filer documentation

Warning

The JavaScript plugin overwrites the functionality of the “Link” button of the markdown editor with it’s own implementation. Please be aware of that.

Usage

Model Field

from django.db import models

from markymark.fields import MarkdownField


class Post(models.Model):
    content = MarkdownField()

Template filter

To display the rendered markdown in your template.

{% load markymark %}

{{ obj.content|markdown }}

Running tests

To run the tests, you need to install the test requirements with Poetry:

$ poetry install

Now you can run the tests from the root folder of the package:

$ make tests

Changelog

3.0.0 (2022-09-12)

  • Drop support for Django < 3.2, add support for 3.2

  • Drop support for Python < 3.8

2.1.0 (2018-09-11)

  • Add setting to switch icon library for markdown editor to FontAwesome 4

2.0.0 (2018-07-27)

  • THIS IS A BREAKING RELEASE, if you need Django < 1.11 please stick to the 1.1.0 release.

  • Drop Python < 3 support

  • Drop Django < 1.11 support

  • Cleanup code and add some more documentation

1.1.0 (2017-04-21)

  • add table support including new button in editor

1.0.5 (2017-04-20)

  • drop py26 support

  • Fix anylink pop up, make it more flexible for custom project urls

1.0.4 (2017-02-21)

  • Fix styling when using markymark in inlines with django-cms / djangocms-admin-style

  • Force Pillow version for py26

1.0.0 (2016-11-24)

  • Add support for Django 1.9

  • Fix styling when using markymark with django-cms / djangocms-admin-style

  • Improve autolink extension to prevent double-linking

  • Javascript bugfixes

0.9.3 (2016-10-05)

0.9.2 (2016-02-03)

  • fix javascript - js was renamed from dismissAddAnotherPopup to dismissAddRelatedObjectPopup

0.9.1 (2015-09-24)

  • fix MardownTextarea widget

0.9.0 (2015-09-24)

  • add django 1.8 and python 3.5 support

0.8.3 (2015-07-08)

  • fix - make “$” available in function only in markdown-editor.js

0.8.2 (2015-04-22)

  • Add python 2.6 support

  • Add support for south

0.8.1 (2015-03-16)

  • Small style fix for preview button

0.8.0 (2015-03-12)

  • Refactor JavaScript part of plugins to replace existing icons

  • Fix bug in anylink js integration to avoid blank window after save

0.7.2 (2015-03-09)

  • Fix anylink javascript to link to ‘add’ view

0.7.1 (2015-03-09)

  • Remove padding from textarea

0.7 (2015-03-09)

  • Fix release

0.6 (2015-03-09)

  • Rework extensions to allow js/css files to be defined directly on each extension

  • The return value of render_markdown() is now marked as safe

  • Allow template-names to be overwritten

  • Made settings easier to be overwritten, you can now import default settings from markymark.defaults

  • Fixed contrib.anylink to avoid name clashes with other extensions named “link”

  • Fix fullscreen icon integration

0.5 (2015-02-13)

  • Removed anylink and filer extensions from being autoloaded.

  • Removed dependency on floppyforms.

0.2..0.4 (2015-01-22)

  • General cleanups and bugfixes.

0.1 (2015-01-22)

  • Initial release.

Api documentation:

markymark.renderer

markymark.renderer.initialize_renderer(extensions=None)[source]

Initializes the renderer by setting up the extensions (taking a comma separated string or iterable of extensions). These extensions are added alongside with the configured always-on extensions.

Returns a markdown renderer instance.

markymark.renderer.render_markdown(value, extensions=None)[source]

Takes a text and a optional list of extensions and returns the rendered markdown text.

The result is marked safe.

markymark.fields

class markymark.fields.MarkdownFormField(*args, **kwargs)[source]

Bases: CharField

Form field to configure the markdown textarea widget if not already set.

__init__(*args, **kwargs)[source]

Update the kwargs to set the markdown widget. Special note: the provided widget should be a subclass of MarkdownTextarea, if not the provided widget will be ignored.

class markymark.fields.MarkdownField(*args, db_collation=None, **kwargs)[source]

Bases: TextField

Model field based on TextField with enabled markdown form field.

formfield(form_class=<class 'markymark.fields.MarkdownFormField'>, **kwargs)[source]

Return a django.forms.Field instance for this field.

markymark.widgets

class markymark.widgets.MarkdownTextarea(*args, **kwargs)[source]

Bases: Textarea

Extended forms Textarea which enables the javascript markdown editor.

__init__(*args, **kwargs)[source]

Sets the required data attributes to enable the markdown editor.

property media[source]

Returns a forms.Media instance with the basic editor media and media from all registered extensions.

markymark.templatetags

markymark.templatetags.markymark.markdown_filter(value, extensions=None)[source]

Template which converts a provided value using markdown to html. Accepts additional extensions when rendering with markdown.

markymark.extensions

markymark.extensions.base

class markymark.extensions.base.MarkymarkExtension(**kwargs)[source]

Bases: Extension

Base class for all markymark extensions.

Actually its just a markdown.Extension class with some media attached.

preprocessors = None[source]
inlinepatterns = None[source]
postprocessors = None[source]
extendMarkdown(md)[source]

Every extension requires a extendMarkdown method to tell the markdown renderer how use the extension.

property media[source]

markymark.extensions.clean

class markymark.extensions.clean.CleanExtension(**kwargs)[source]

Bases: MarkymarkExtension

Extension to enable the cleanup plugin for the markdown editor.

class Media[source]

Bases: object

js = ('markymark/extensions/clean.js',)[source]
property media[source]
markymark.extensions.clean.makeExtension(**kwargs)[source]

markymark.extensions.filer

class markymark.extensions.filer.FilerPostprocessor(md=None)[source]

Bases: Postprocessor

Filer markdown extension for django-filer to show files and images.

FILE_RE = re.compile('(\\[file\\:(?P<id>\\d+)\\])', re.IGNORECASE)[source]
run(text)[source]

Subclasses of Postprocessor should implement a run method, which takes the html document as a single text string and returns a (possibly modified) string.

class markymark.extensions.filer.FilerExtension(**kwargs)[source]

Bases: MarkymarkExtension

Extension to look for file tags, replaces them with html tags. In case of image, the image is added as img-tag, files are added as download links.

postprocessors = (<class 'markymark.extensions.filer.FilerPostprocessor'>,)[source]
class Media[source]

Bases: object

js = ('markymark/extensions/filer.js',)[source]
css = {'all': ('markymark/extensions/filer.css',)}[source]
property media[source]
markymark.extensions.filer.makeExtension(**kwargs)[source]

Who’s Marky Mark?

Marky Mark on Wikipedia

Indices and tables