Wagtail:如何覆盖数据库的 HTML 标签输出。使用 <strong> 或 <em> 而不是 <b> 或 <i> 作为富文本模板标签
Wagtail: How to override HTML tag output from database. Use <strong> or <em> instead of <b> or <i> for richtext template tag
我正在尝试让我的 wagtail 模板输出 <strong></strong>
而不是 <b></b>
和 <em></em>
而不是 <i></i>
。
我手动编辑了 wagtailcore_pagerevision table 记录中的 content_json 值,因此 <b>
标签是 <strong>
和 <i>
标签是 <em>
但输出 HTML 继续分别输出 <b>
和 <i>
标签。
在我的模板中,我有 {{ block.value|richtext }}
块和 {{ self.body|richtext }}
非块。
完成这项工作的 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 support channel. I got the advice to 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)
指定富文本字段的功能。不要忘记删除旧的 'bold' 和 'italic':
from wagtail.core.fields import RichTextField
class FooPage(Page):
body = RichTextField(features=['strong', 'em'])
我正在尝试让我的 wagtail 模板输出 <strong></strong>
而不是 <b></b>
和 <em></em>
而不是 <i></i>
。
我手动编辑了 wagtailcore_pagerevision table 记录中的 content_json 值,因此 <b>
标签是 <strong>
和 <i>
标签是 <em>
但输出 HTML 继续分别输出 <b>
和 <i>
标签。
在我的模板中,我有 {{ block.value|richtext }}
块和 {{ self.body|richtext }}
非块。
完成这项工作的 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 support channel. I got the advice to 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)
指定富文本字段的功能。不要忘记删除旧的 'bold' 和 'italic':
from wagtail.core.fields import RichTextField
class FooPage(Page):
body = RichTextField(features=['strong', 'em'])