torchbox / wagtail-markdown

Markdown support for Wagtail
zlib License
198 stars 66 forks source link

How to limit MarkdownBlock to specific toolbar options (e.g. 'strong' only) in Wagtail admin? #146

Closed jim-acb closed 1 week ago

jim-acb commented 1 month ago

I am using the wagtail-markdown package with a MarkdownBlock in Wagtail’s StreamField. I would like to restrict the Markdown toolbar in the Wagtail admin interface to only allow the specific options (for example 'strong' only), disabling all other Markdown formatting options.

I attempted the following:

•   Configured the WAGTAILMARKDOWN settings to only allow the <strong> tag using allowed_tags.
WAGTAILMARKDOWN = {
    "allowed_tags": ["strong"],
    "allowed_attributes": {},
    "allowed_styles": [],
    "allowed_settings_mode": "override",
    "extensions": [],
    "extensions_settings_mode": "override",
}

Despite these changes, the editor continues to display all toolbar options, and users can still see other Markdown features.

I would like to request a feature or guidance on how to limit the editor to specific Markdown formatting options.

Thank you!

zerolab commented 1 month ago

Please refer to https://github.com/torchbox/wagtail-markdown?tab=readme-ov-file#easymde-configuration and https://github.com/Ionaru/easy-markdown-editor?tab=readme-ov-file#toolbar-customization

jim-acb commented 1 week ago

Thank you, this is how I ended up doing it.

# apps/core/wagtail_hooks.py
from django.templatetags.static import static
from django.utils.html import format_html
from wagtail import hooks
@hooks.register("insert_global_admin_js", order=100)
def global_admin_js():
    return format_html('<script src="{}"></script>', static("js/easymde_custom.js"))
# apps/core/static/js/easymde_custom.js
window.wagtailMarkdown = window.wagtailMarkdown || {};
window.wagtailMarkdown.options = {
    toolbar: ["bold", "link"],
    spellChecker: false,
    shortcuts: {
        // Disable all shortcuts except for bold and link.
        // https://github.com/Ionaru/easy-markdown-editor?tab=readme-ov-file#keyboard-shortcuts

        // toggleBold: null,
        // drawLink: null,
        cleanBlock: null,
        toggleHeadingSmaller: null,
        toggleItalic: null,
        toggleUnorderedList: null,
        togglePreview: null,
        toggleCodeBlock: null,
        drawImage: null,
        toggleOrderedList: null,
        toggleHeadingBigger: null,
        toggleSideBySide: null,
        toggleFullScreen: null,
        toggleHeading1: null,
        toggleHeading2: null,
        toggleHeading3: null,
        toggleHeading4: null,
        toggleHeading5: null,
        toggleHeading6: null,
    },
};