我正在尝试让我的鹡鸰模板输出 <strong></strong>
而不是 <b></b>
和 <em></em>
而不是 <i></i>
.
我手动编辑了 wagtailcore_pagerevision 表记录中的 content_json 值,以便 <b>
标签是 <strong>
和 <i>
标签是 <em>
但输出 HTML 继续输出 <b>
和 <i>
分别标记。
在我的模板中我有 {{ block.value|richtext }}
对于 block 和 {{ self.body|richtext }}
对于非 block 。
完成这项工作的 wagtail 代码是:
@register.filter
def richtext(value):
if isinstance(value, RichText):
# passing a RichText value through the |richtext filter should have no effect
return value
elif value is None:
html = ''
else:
html = expand_db_html(value)
return mark_safe('<div class="rich-text">' + html + '</div>')
我的问题是.. 我如何告诉 Wagtail 或 Django 使用 <strong>
和 <em>
标签?
这似乎不是 hallo-js 所见即所得问题或设置,而是某种我似乎无法找到的配置或其他设置。
顺便说一句..我正在使用 Wagtail 1.13.1(带有默认的 Hallo 编辑器)、Django 1.11 和 MySQL 作为数据库。
为了解决我的问题,我用这段代码覆盖了..
# override the wagtail version and replace <b>, <i>
@register.filter(name='richtext')
def richtext(value):
if isinstance(value, RichText):
# passing a RichText value through the |richtext filter should have no effect
# return value
html = value.source
elif value is None:
html = ''
else:
html = expand_db_html(value)
html = html.replace('<b>', '<strong>').replace('</b>', '</strong>') \
.replace('<i>', '<em>').replace('</i>', '</em>')
return mark_safe('<div class="rich-text">' + html + '</div>')
但应该有更好、更高效的方法。
最佳答案
我遇到了同样的问题并在 Wagtail Slack 上询问支持 channel 。我得到了 register a new rich text feature 的建议.该文档显示了一个删除线示例。以下是粗体 (strong) 和斜体 (em):
import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import (
InlineStyleElementHandler,
)
@hooks.register('register_rich_text_features')
def register_strong_feature(features):
"""
Registering the `strong` feature. It will render bold text with `strong` tag.
Default Wagtail uses the `b` tag.
"""
feature_name = 'strong'
type_ = 'BOLD'
tag = 'strong'
# Configure how Draftail handles the feature in its toolbar.
control = {
'type': type_,
'icon': 'bold',
'description': 'Bold',
}
# Call register_editor_plugin to register the configuration for Draftail.
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
# Configure the content transform from the DB to the editor and back.
db_conversion = {
'from_database_format': {tag: InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: tag}},
}
# Call register_converter_rule to register the content transformation conversion.
features.register_converter_rule('contentstate', feature_name, db_conversion)
<em>
和斜体标签:
@hooks.register('register_rich_text_features')
def register_em_feature(features):
"""
Registering the `em` feature. It will render italic text with `em` tag.
Default Wagtail uses the `i` tag.
"""
feature_name = 'em'
type_ = 'ITALIC'
tag = 'em'
control = {
'type': type_,
'icon': 'italic',
'description': 'Italic',
}
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
db_conversion = {
'from_database_format': {tag: InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: tag}},
}
features.register_converter_rule('contentstate', feature_name, db_conversion)
指定富文本字段的功能。不要忘记删除旧的“粗体”和“斜体”:
from wagtail.core.fields import RichTextField
class FooPage(Page):
body = RichTextField(features=['strong', 'em'])
关于django - Wagtail:如何覆盖数据库中的 HTML 标记输出。使用 <strong> 或 <em> 而不是 <b> 或 <i> 作为富文本模板标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49902931/