Skip to main content

Draftail Shortcode Entity, adds link-like shortcodes to Draftail.

Project description

Wagtail shortcode

Wagtail shortcode adds a custom Draftail enitity to provide a custom linktype ("shortcode"), giving you the ability to assign values in a similar way to normal links as well as decide how they should be rendered on the page.

License: BSD-3-Clause PyPI version shortcode CI

Installation

  • python -m pip install wagtail-shortcode
  • ...

Add 'wagtail_shortcode' to INSTALLED_APPS.

Register a LinkHandler, to control how the new linktype renders on the front-end (|richtext filter).

LinkHandlers must be registered within a wagtail_hooks.py file under an app directory and use an identifier matching SHORTCODE_ANCHOR_TARGET_IDENTIFIER, for example:

# ./utils_app/wwgtail_hooks.py
from django.utils.html import escape
from wagtail import hooks
from wagtail.rich_text import LinkHandler
from wagtail_shortcode.handlers import SHORTCODE_ANCHOR_TARGET_IDENTIFIER


class CustomShortcodeLinkHandler(LinkHandler):
    identifier = SHORTCODE_ANCHOR_TARGET_IDENTIFIER

    @classmethod
    def expand_db_attributes(cls, attrs):
        href = attrs["shortcode"]
        # Do something clever
        return '<a href="%s">' % escape(href)


@hooks.register("register_rich_text_features")
def register_shortcode_link(features):
    features.register_link_type(CustomShortcodeLinkHandler)

Add shortcode to the features argument of any rich text field where you have overridden the default feature list:

class Header(models.Model):
    content = RichTextField(
        features=["shortcode", "h2", "h3", "bold", "italic", "link"]
    )

Configuration

Updating the window prompt's help-text

When adding shortcode within the editor, a window prompt will appear, the text within this prompt can be customised by providing a value for SHORTCODE_HELP_TEXT within your project's settings file. This can be helpful as to inform editors of the expected behaviour of your custom LinkHandler.

SHORTCODE_HELP_TEXT="Enter a shortcode value"

The window.prompt function is a JavaScript method which supports basic text formatting using special characters, as such a newline character can be included using \n if needed.

Contributing

See docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_shortcode-1.0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

wagtail_shortcode-1.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file wagtail_shortcode-1.0.2.tar.gz.

File metadata

  • Download URL: wagtail_shortcode-1.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for wagtail_shortcode-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6c7a303cbbfb921a644804e8f1d404ed2424168e4f5a718a8b0df3dc8d5d9d8b
MD5 e552f0ea002232a6c0cb5ca945cdb9a4
BLAKE2b-256 24a1292a1b16a010319e8c4bbb913ba57a34e0f02755cccb3803b5fee81076a7

See more details on using hashes here.

Provenance

File details

Details for the file wagtail_shortcode-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_shortcode-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 092c139461ee3b8aaeb26aa31ea6b42904a79039715e18ece97a57ea07aab0c2
MD5 34310ef0758625c9ca495133acf72644
BLAKE2b-256 d210c38c412f0e9140dbe323445015038a0283ab1fb2cd86cbbc9d8d90ea0691

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page