🙌 Awesome Django Markdown Editor, supported for Bootstrap & Semantic-UI
| Files | Last commit | Last update |
|---|---|---|
| 5 years ago | ||
| 4 months ago | ||
| 4 months ago | ||
| 2 months ago | ||
| 4 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 2 years ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 8 years ago | ||
| 1 year ago | ||
| 4 months ago | ||
| 2 years ago | ||
| 1 year ago | ||
| 4 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago |
martor

📖 文档: https://django-markdown-editor.readthedocs.io
Martor 是一个适用于 Django 的 Markdown 编辑器插件,支持 Bootstrap、Semantic-UI 和 Tailwind CSS。
特性
- 实时预览
- 集成 Ace Editor
- 支持 Bootstrap、Semantic-UI 和 Tailwind CSS
- 支持多字段 修复此问题
- 上传图片至 imgur.com (通过 API) 以及 自定义上传器
- 直接提及用户
@[username]- (要求用户已登录)。 - 支持嵌入/iframe 视频(来自 Youtube、Vimeo、Dailymotion、Yahoo、Veoh 和 Metacafe)
- 拼写检查(目前仅支持美式英语)
- 表情符号
:emoji_name:+ 速查表 - Martor 命令参考
- 支持 Django 管理后台
- 工具栏按钮
pre代码块高亮- 自定义 ID 属性(使用
{#custom-id}语法为任何文本元素添加自定义 ID,例如# Heading1 {#my-h1-id},以便轻松链接和导航。
预览


要求
Django>=3.2Markdown>=3.0requests>=2.12.4bleach
安装
Martor 可直接从 PyPI 获取:
1. 安装包。
$ pip install martor
2. 不要忘记将 'martor' 添加到您的 'INSTALLED_APPS' 设置中(无需迁移)。
# settings.py
INSTALLED_APPS = [
....
'martor',
]
3. 在您的 urls.py 中添加 URL 模式。
# urls.py
urlpatterns = [
...
path('martor/', include('martor.urls')),
]
4. 将 martor 的静态文件收集到您的 STATIC_ROOT 文件夹中。
./manage.py collectstatic
设置配置 settings.py
请在 https://api.imgur.com/oauth2/addclient 注册您的应用,以获取 IMGUR_CLIENT_ID 和 IMGUR_API_KEY。
# Choices are: "semantic", "bootstrap", "tailwind"
MARTOR_THEME = 'bootstrap'
# Global martor settings
# Input: string boolean, `true/false`
MARTOR_ENABLE_CONFIGS = {
'emoji': 'true', # to enable/disable emoji icons.
'imgur': 'true', # to enable/disable imgur/custom uploader.
'mention': 'false', # to enable/disable mention
'jquery': 'true', # to include/revoke jquery (require for admin default django)
'living': 'false', # to enable/disable live updates in preview
'spellcheck': 'false', # to enable/disable spellcheck in form textareas
'hljs': 'true', # to enable/disable hljs highlighting in preview
}
# To show the toolbar buttons
MARTOR_TOOLBAR_BUTTONS = [
'bold', 'italic', 'horizontal', 'heading', 'pre-code',
'blockquote', 'unordered-list', 'ordered-list',
'link', 'image-link', 'image-upload', 'emoji',
'direct-mention', 'toggle-maximize', 'help'
]
# To setup the martor editor with title label or not (default is False)
MARTOR_ENABLE_LABEL = False
# Disable admin style when using custom admin interface e.g django-grappelli (default is True)
MARTOR_ENABLE_ADMIN_CSS = True
# Imgur API Keys
MARTOR_IMGUR_CLIENT_ID = 'your-client-id'
MARTOR_IMGUR_API_KEY = 'your-api-key'
# Markdownify
MARTOR_MARKDOWNIFY_FUNCTION = 'martor.utils.markdownify' # default
MARTOR_MARKDOWNIFY_URL = '/martor/markdownify/' # default
# Delay in milliseconds to update editor preview when in living mode.
MARTOR_MARKDOWNIFY_TIMEOUT = 0 # update the preview instantly
# or:
MARTOR_MARKDOWNIFY_TIMEOUT = 1000 # default
# Markdown extensions (default)
MARTOR_MARKDOWN_EXTENSIONS = [
'markdown.extensions.extra',
'markdown.extensions.nl2br',
'markdown.extensions.smarty',
'markdown.extensions.fenced_code',
'markdown.extensions.sane_lists',
# Custom markdown extensions.
'martor.extensions.urlize',
'martor.extensions.del_ins', # ~~strikethrough~~ and ++underscores++
'martor.extensions.mention', # to parse markdown mention
'martor.extensions.emoji', # to parse markdown emoji
'martor.extensions.mdx_video', # to parse embed/iframe video
'martor.extensions.escape_html', # to handle the XSS vulnerabilities
"martor.extensions.mdx_add_id", # to parse id like {#this_is_id}
]
# Markdown Extensions Configs
MARTOR_MARKDOWN_EXTENSION_CONFIGS = {}
# Markdown urls
MARTOR_UPLOAD_URL = '' # Completely disable the endpoint
# or:
MARTOR_UPLOAD_URL = '/martor/uploader/' # default
MARTOR_SEARCH_USERS_URL = '' # Completely disables the endpoint
# or:
MARTOR_SEARCH_USERS_URL = '/martor/search-user/' # default
# Markdown Extensions
# MARTOR_MARKDOWN_BASE_EMOJI_URL = 'https://www.webfx.com/tools/emoji-cheat-sheet/graphics/emojis/' # from webfx
MARTOR_MARKDOWN_BASE_EMOJI_URL = 'https://github.githubassets.com/images/icons/emoji/' # default from github
# or:
MARTOR_MARKDOWN_BASE_EMOJI_URL = '' # Completely disables the endpoint
MARTOR_MARKDOWN_BASE_MENTION_URL = 'https://python.web.id/author/' # please change this to your domain
# If you need to use your own themed "bootstrap" or "semantic ui" dependency
# replace the values with the file in your static files dir
MARTOR_ALTERNATIVE_JS_FILE_THEME = "semantic-themed/semantic.min.js" # default None
MARTOR_ALTERNATIVE_CSS_FILE_THEME = "semantic-themed/semantic.min.css" # default None
MARTOR_ALTERNATIVE_JQUERY_JS_FILE = "jquery/dist/jquery.min.js" # default None
# URL schemes that are allowed within links
ALLOWED_URL_SCHEMES = [
"file", "ftp", "ftps", "http", "https", "irc", "mailto",
"sftp", "ssh", "tel", "telnet", "tftp", "vnc", "xmpp",
]
# https://gist.github.com/mrmrs/7650266
ALLOWED_HTML_TAGS = [
"a", "abbr", "b", "blockquote", "br", "cite", "code", "command",
"dd", "del", "dl", "dt", "em", "fieldset", "h1", "h2", "h3", "h4", "h5", "h6",
"hr", "i", "iframe", "img", "input", "ins", "kbd", "label", "legend",
"li", "ol", "optgroup", "option", "p", "pre", "small", "span", "strong",
"sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u", "ul"
]
# https://github.com/decal/werdlists/blob/master/html-words/html-attributes-list.txt
ALLOWED_HTML_ATTRIBUTES = [
"alt", "class", "color", "colspan", "datetime", # "data",
"height", "href", "id", "name", "reversed", "rowspan",
"scope", "src", "style", "title", "type", "width"
]
请检查此设置是否未被设置,否则跨站请求伪造(CSRF)令牌将不会通过 AJAX 调用发送:
CSRF_COOKIE_HTTPONLY = False
使用方法
模型
from django.db import models
from martor.models import MartorField
class Post(models.Model):
description = MartorField()
表单
from django import forms
from martor.fields import MartorFormField
class PostForm(forms.Form):
description = MartorFormField()
管理界面
from django.db import models
from django.contrib import admin
from martor.widgets import AdminMartorWidget
from yourapp.models import YourModel
class YourModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': AdminMartorWidget},
}
admin.site.register(YourModel, YourModelAdmin)
模板渲染器
只需从 martor/templatetags/martortags.py 加载模板标签,即可安全地将 Markdown 内容解析为 HTML 输出。
{% load martortags %}
{{ field_name|safe_markdown }}
# example
{{ post.description|safe_markdown }}
使用前请务必包含所需的 css 和 js 文件。
您可以查看 martor_demo/app/templates 文件夹了解更多详情。
以下示例是当您选择 MARTOR_THEME = 'bootstrap' 时的一种实现方式:
{% extends "bootstrap/base.html" %}
{% load static %}
{% load martortags %}
{% block css %}
<link href="{% static 'plugins/css/ace.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'martor/css/martor.bootstrap.min.css' %}" type="text/css" media="all" rel="stylesheet" />
{% endblock %}
{% block content %}
<div class="martor-preview">
<h1>Title: {{ post.title }}</h1>
<p><b>Description:</b></p>
<hr />
{{ post.description|safe_markdown }}
</div>
{% endblock %}
{% block js %}
<script type="text/javascript" src="{% static 'plugins/js/highlight.min.js' %}"></script>
<script>
$('.martor-preview pre').each(function(i, block){
hljs.highlightBlock(block);
});
</script>
{% endblock %}
模板编辑器表单
与 Template Renderer 不同,Template Editor Form 具有更多的 CSS 和 JavaScript 依赖项。
{% extends "bootstrap/base.html" %}
{% load static %}
{% block css %}
<link href="{% static 'plugins/css/ace.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'martor/css/martor.bootstrap.min.css' %}" type="text/css" media="all" rel="stylesheet" />
{% endblock %}
{% block content %}
<form class="form" method="post">{% csrf_token %}
<div class="form-group">
{{ form.title }}
</div>
<div class="form-group">
{{ form.description }}
</div>
<div class="form-group">
<button class="btn btn-success">
<i class="save icon"></i> Save Post
</button>
</div>
</form>
{% endblock %}
{% block js %}
<script type="text/javascript" src="{% static 'plugins/js/ace.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/mode-markdown.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/ext-language_tools.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/theme-github.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/typo.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/spellcheck.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/highlight.min.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/emojis.min.js' %}"></script>
<script type="text/javascript" src="{% static 'martor/js/martor.bootstrap.min.js' %}"></script>
{% endblock %}
Tailwind CSS 主题
当使用 MARTOR_THEME = 'tailwind' 时,请包含所需的 CSS 和 JavaScript 文件:
模板渲染器(Tailwind):
{% extends "base.html" %}
{% load static %}
{% load martortags %}
{% block css %}
<link href="{% static 'plugins/css/ace.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'plugins/css/tailwind.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'martor/css/martor.tailwind.min.css' %}" type="text/css" media="all" rel="stylesheet" />
{% endblock %}
{% block content %}
<div class="container mx-auto px-4 py-8">
<div class="martor-preview bg-white rounded-lg shadow-sm p-6">
<h1 class="text-3xl font-bold text-gray-900 mb-4">{{ post.title }}</h1>
<div class="prose prose-sm max-w-none">
{{ post.description|safe_markdown }}
</div>
</div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript" src="{% static 'plugins/js/highlight.min.js' %}"></script>
<script>
document.querySelectorAll('.martor-preview pre code').forEach((block) => {
hljs.highlightBlock(block);
});
</script>
{% endblock %}
模板编辑器表单(Tailwind):
{% extends "base.html" %}
{% load static %}
{% block css %}
<link href="{% static 'plugins/css/ace.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'plugins/css/tailwind.min.css' %}" type="text/css" media="all" rel="stylesheet" />
<link href="{% static 'martor/css/martor.tailwind.min.css' %}" type="text/css" media="all" rel="stylesheet" />
{% endblock %}
{% block content %}
<div class="container mx-auto px-4 py-8">
<form class="bg-white rounded-lg shadow-sm p-6" method="post">{% csrf_token %}
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">{{ form.title.label }}</label>
{{ form.title }}
</div>
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">{{ form.description.label }}</label>
{{ form.description }}
</div>
<div class="flex justify-end">
<button class="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-md transition-colors">
Save Post
</button>
</div>
</form>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript" src="{% static 'plugins/js/ace.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/mode-markdown.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/ext-language_tools.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/theme-github.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/typo.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/spellcheck.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/highlight.min.js' %}"></script>
<script type="text/javascript" src="{% static 'plugins/js/emojis.min.js' %}"></script>
<script type="text/javascript" src="{% static 'martor/js/martor.tailwind.min.js' %}"></script>
{% endblock %}
自定义上传器
如果您希望将上传的图片保存到自己的存储中,Martor 也提供了相应的处理方式。请查看此 WIKI
从本仓库测试 Martor
假设您已设置好虚拟环境(virtualenv):
$ git clone https://github.com/agusmakmun/django-markdown-editor.git
$ cd django-markdown-editor/ && pip install -e .
$ cd martor_demo/
$ python manage.py makemigrations && python manage.py migrate
$ python manage.py runserver
在浏览器中访问 http://127.0.0.1:8000/simple-form/。
文档
完整文档可在线查看:https://django-markdown-editor.readthedocs.io/
本地运行文档
要在本地构建和查看文档:
1. 克隆仓库并设置环境:
$ git clone https://github.com/agusmakmun/django-markdown-editor.git
$ cd django-markdown-editor/
$ python -m venv venv
$ source venv/bin/activate # On Windows: venv\Scripts\activate
2. 安装文档依赖项:
$ pip install -r docs/requirements.txt
3. 构建文档:
$ cd docs/
$ sphinx-build -b html . _build/html
或者使用随附的 Makefile:
$ cd docs/
$ make html
4. 在浏览器中打开文档:
$ open _build/html/index.html # On macOS
# Or on Linux/Windows, navigate to docs/_build/html/index.html
或者,使用本地 HTTP 服务器来提供文档:
$ make serve
# Then open http://localhost:8000 in your browser
文档内容包括:
- 安装和快速入门指南
- 完整的配置参考
- 模型、表单、小部件和管理界面的使用示例
- API 文档
- 故障排除和常见问题解答
文档贡献
文档使用 Sphinx 构建,并采用 reStructuredText 格式。如需贡献:
- 编辑
docs/目录下的.rst文件 - 本地构建文档以测试您的更改
- 提交包含改进内容的拉取请求
Martor 命令参考

说明
Martor 的灵感来源于以下优秀项目:django-markdownx、Python Markdown 和 Online reStructuredText editor。
Introduction
🙌 Awesome Django Markdown Editor, supported for Bootstrap & Semantic-UI