Skip to main content

Add a code block to StreamField of Wagtail with Shiki Syntax highlighter.

Project description

Wagtail Shiki

Wagtail Shiki is based on Wagtail Code Block.

Wagtail Code Block is a syntax highlighter block for source code for the Wagtail CMS. It features real-time highlighting in the Wagtail editor, the front end, line numbering, and support for PrismJS themes.

Wagtail Shiki uses the Shiki library instead of PrismJS library both in Wagtail Admin and the website. Required files for Shiki are loaded on demand using esm.run.

Additionally, Wagtail Shiki provides text decoration functions (underlining, borders, and more, extensible with CSS styles) within the syntax highlighting.

You can set each themes for light and dark modes.

Instalation

pip install wagtail-shiki

And add wagtail_shiki to INSTALLED_APPS in mysite/settings/base.py.

INSTALLED_APPS = [
    "home",
    "search",
    "wagtail.contrib.forms",
    "wagtail.contrib.redirects",
    "wagtail.embeds",
    "wagtail.sites",
    "wagtail.users",
    #... other packages
    "wagtail_shiki",   # <- add this.
]

Trial Run

Install new wagtail for trial run.

mkdir mysite
python -m venv mysite/env
source mysite/env/bin/activate

pip install wagtail
wagtail start mysite mysite

cd mysite
pip install -r requirements.txt
pip install wagtail-shiki



Edit files bellow:
mysite/settings/base.py

INSTALLED_APPS = [
    #... other packages
    "wagtail_shiki",   # <- add this.
]


home/models.py

from wagtail.blocks import TextBlock
from wagtail.fields import StreamField
from wagtail.models import Page
from wagtail.admin.panels import FieldPanel

from wagtail_shiki.blocks import CodeBlock


class HomePage(Page):
    body = StreamField([
        ("heading", TextBlock()),
        ("code", CodeBlock(label='Code')),
    ], blank=True)

    content_panels = Page.content_panels + [
        FieldPanel("body"),
    ]


home/templates/home/home_page.html

    ...

{% load wagtailcore_tags wagtailimages_tags %}

    ...

<!-- {% include 'home/welcome_page.html' %} -->
{% include_block page.body %}

    ...



run:

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

After the server starts, go to http://127.0.0.1:8000/admin" .
Clicking the "+" Add button in the body section, and click "Code" to add a code block.

add code block



Then you can edit the code block.

edit view

Various settings

WAGS_LINE_NUMBERS (default = True)

If true, line numbers will be displayed. You can set the starting line number by inputting "Start number" field in the code block editing screen.

WAGS_COPY_TO_CLIPBOARD (default = True)

If true, copy to clipboard button will be displayed.

WAGS_THEME (default = 'nord')

The theme for light mode.

WAGS_DARK_THEME (default = WAGS_THEME)

The theme for dark mode.If this is not set, it will map the light theme to the dark theme.
As a result, the same theme will be assigned to light mode and dark mode.

WAGS_SKIP_LEADING_SPACES (default = True)

If true, the decoration of the leading spaces will be skipped to show.

WAGS_DECORATONS_REMOVE_FRONT_SPAACE (default = True)

If true, the decoration of the front side leading spaces will be deleted.

WAGS_DECORATONS_REMOVE_REAR_SPAACE (default = True)

If true, the decoration of the rear side leading spaces will be deleted.

WAGS_SHOW_HIGHLIGHTWORDS_INPUT (default = False)

If true, the "Highlight Words" field(uneditable) will be shown.
This is only for debugging.

WAGS_CLASS_PREFIX (default = 'wags')

The prefix for the CSS class name for decorations.
This parameter and the following "-" will be prepended to the value of the "value" key in "WAGS_DECORATION_OPTIONS" and used as a CSS class.

WAGS_DECORATION_OPTIONS

default = [
    {
        'value': 'underline-red',
        'text': 'underline red',
        'style': 'text-decoration: red underline;'
    },
    {
        'value': 'underline-blue',
        'text': 'underline blue',
        'style': 'text-decoration: blue underline;'
    },
    {
        'value': 'underline-green',
        'text': 'underline green',
        'style': 'text-decoration: green underline;'
    },
    {
        'value': 'underline-yellow',
        'text': 'underline yellow',
        'style': 'text-decoration: yellow underline;'
    },
    {
        'value': 'wavyunderline-red',
        'text': 'wavy underline red',
        'style': 'text-decoration: red wavy underline;'
    },
    {
        'value': 'wavyunderline-blue',
        'text': 'wavy underline blue',
        'style': 'text-decoration: blue wavy underline;'
    },
    {
        'value': 'wavyunderline-green',
        'text': 'wavy underline green',
        'style': 'text-decoration: green wavy underline;'
    },
    {
        'value': 'wavyunderline-yellow',
        'text': 'wavy underline yellow',
        'style': 'text-decoration: red wavy underline;'
    },
    {
        'value': 'dashedborder-red',
        'text': 'dashed border red',
        'style': 'border: dashed red; border-width: 1px; border-radius: 3px; padding: 0px;'
    },
    {
        'value': 'dashedborder-blue',
        'text': 'dashed border blue',
        'style': 'border: dashed blue; border-width: 1px; border-radius: 3px; padding: 0px;'
    },
    {
        'value': 'dashedborder-green',
        'text': 'dashed border green',
        'style': 'border: dashed green; border-width: 1px; border-radius: 3px; padding: 0px;'
    },
    {
        'value': 'dashedborder-yellow',
        'text': 'dashed border yellow',
        'style': 'border: dashed yellow; border-width: 1px; border-radius: 3px; padding: 0px;'
    },
    {
        'value': '',
        'text': 'Remove decoration(s)',
        'style': ''
    }
]
  • These five kind ofcharacters <, >, ', ", & in the string of each value of keys 'value', 'text' and 'style' are removeed.
  • The last option {'value': '', 'text': 'Remove decoration(s)', 'style': ''} is for Remove decoration(s). If valu of 'value' is empty string, the decoration will be removed.(The value of 'value' will be the CSS class name for the selected span.)

Some utility functions for creating CSS styles are provided in the module to ease the creation of decoration options in basy.py.

To use these functions, import them from the module:

from wagtail_shiki.settings import (
    css_style_underline as underline,
    css_style_dashedborder as dashedborder,
    css_style_bg_colored as bg_colored,
)

And then use it like following:

WAGS_DECORATION_OPTIONS = [
    ...
    {'value': 'underline-red', 'text': 'underline red', 'style': underline('red')},
    ...
    {'value': 'wavyunderline-red', 'text': 'wavy underline red', 'style': underline('red', 'wavy')},
    ...
    {'value': 'dashedborder-red', 'text': 'dashed border red', 'style': dashedborder('red')},
    ...
    {'value': 'bg_colored-red', 'text': 'ba-colored', 'style': bg_colored('red')},
    ...
]

It will expanded to:

WAGS_DECORATION_OPTIONS = [
    ...
    {'value': 'underline-red', 'text': 'underline red', 'style': 'text-decoration: red underline;'},
    ...
    {'value': 'wavyunderline-red', 'text': 'wavy underline red', 'style': 'text-decoration: red wavy underline;'},
    ...
    {'value': 'dashedborder-red', 'text': 'dashed border red', 'style': 'border: dashed red; border-width: 1px; border-radius: 3px; padding: 0px;'},
    ...
    {'value': 'bg_colored-red', 'text': 'ba-colored', 'style': 'background-color: red;'},
    ...
]

Not only color names, you can also use color specifications that are generally available in style sheets, such as '#00a400', 'rgb(214, 122, 127)' for these utility functions.

customizing decoration settings

Add new options to WAGS_DECORATION_OPTIONS in your Django settings and add CSS styles for the new options.

If you want to add orange under line decoration, add the following option to WAGS_DECORATION_OPTIONS in your Django settings.(class name is for example)

WAGS_DECORATION_OPTIONS = [
    ...
    {'value': 'underline-orange', 'text': 'underline orange', 'style': underline('orange')},
    ...
]

[!NOTE] WAGS_DECORATION_OPTIONS overrides the default settings, if you want to keep them, you have to add default settings along with your custom settings.

base settings for customize

from wagtail_shiki.settings import (
    css_style_underline as underline,
    css_style_dashedborder as dashedborder,
    css_style_bg_colored as bg_colored,
)

WAGS_DECORATION_OPTIONS = [
    {
        'value': 'underline-red',
        'text': 'underline red',
        'style': underline('red')
    },
    {
        'value': 'underline-blue',
        'text': 'underline blue',
        'style': underline('blue')
    },
    {
        'value': 'underline-green',
        'text': 'underline green',
        'style': underline('green')
    },
    {
        'value': 'underline-yellow',
        'text': 'underline yellow',
        'style': underline('yellow')
    },
    {
        'value': 'wavyunderline-red',
        'text': 'wavy underline red',
        'style': underline('red', 'wavy')
    },
    {
        'value': 'wavyunderline-blue',
        'text': 'wavy underline blue',
        'style': underline('blue', 'wavy')
    },
    {
        'value': 'wavyunderline-green',
        'text': 'wavy underline green',
        'style': underline('green', 'wavy')
    },
    {
        'value': 'wavyunderline-yellow',
        'text': 'wavy underline yellow',
        'style': underline('yellow', 'wavy')},
    {
        'value': 'dashedborder-red',
        'text': 'dashed border red',
        'style': dashedborder('red')
    },
    {
        'value': 'dashedborder-blue',
        'text': 'dashed border blue',
        'style': dashedborder('blue')
    },
    {
        'value': 'dashedborder-green',
        'text': 'dashed border green',
        'style': dashedborder('green')
    },
    {
        'value': 'dashedborder-yellow',
        'text': 'dashed border yellow',
        'style': dashedborder('yellow')
    },
    {
        'value': '',
        'text': 'Remove decoration(s)',
        'style': ''
    }
]

WAGS_LANGUAGES

A list of languages ​​to enable. 'ansi' and 'text' are always enabled.

  default= (
    ("bash", "Bash/Shell"),
    ("css", "CSS"),
    ("diff", "diff"),
    ('jinja', 'Django/Jinja'),
    ("html", "HTML"),
    ("javascript", "Javascript"),
    ("json", "JSON"),
    ("python", "Python"),
    ("scss", "SCSS"),
    ("yaml", "YAML"),
  )

Usage

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_shiki-0.5.0b1.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_shiki-0.5.0b1-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file wagtail_shiki-0.5.0b1.tar.gz.

File metadata

  • Download URL: wagtail_shiki-0.5.0b1.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for wagtail_shiki-0.5.0b1.tar.gz
Algorithm Hash digest
SHA256 0322e84b6d38c0cb0ce9b5022a7ca0588383146843aced51ebcc7e341513fbee
MD5 0f0f1db621d8ad896f26c619422f8630
BLAKE2b-256 e365d4f5d290a4edb35fed88b746a1b3749dbed62c34d6b757b3e9dc344bab62

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_shiki-0.5.0b1.tar.gz:

Publisher: publish-to-pypi.yml on kawakin26/wagtail-shiki

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wagtail_shiki-0.5.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_shiki-0.5.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 f36c96c1751e3c1c7d26f2f0a407cd96b7f95a52e080fc84667dcc7b16929e33
MD5 3c29197fb43171217eadb984743b081d
BLAKE2b-256 13e09774962712ba1462b0e30cef8fadd413c808b5f0db48b42b6608bb1bca63

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_shiki-0.5.0b1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on kawakin26/wagtail-shiki

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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