Skip to content

Commit 6381e2b

Browse files
authored
Add strikethrough processor (#158)
* Add custom Markdown extension for strikethrough * Simplify test matrix * Update README * Bump version * Bump ruff version
1 parent e3f1cf6 commit 6381e2b

File tree

10 files changed

+73
-24
lines changed

10 files changed

+73
-24
lines changed

.github/workflows/ruff.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ jobs:
2222
python-version: "3.13"
2323
- name: Install dependencies
2424
# keep in sync with .pre-commit-config.yaml
25-
run: python -Im pip install --user ruff==0.8.2
25+
run: python -Im pip install --user ruff==0.13.3
2626
- name: Run Ruff
2727
run: ruff check --output-format=github ./src/wagtailmarkdown

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020
- id: end-of-file-fixer
2121
- id: trailing-whitespace
2222
- repo: https://github.com/astral-sh/ruff-pre-commit
23-
rev: 'v0.8.2' # keep in sync with .github/workflows/ruff.yml
23+
rev: 'v0.13.3' # keep in sync with .github/workflows/ruff.yml
2424
hooks:
2525
- id: ruff
2626
args: [ --fix, --exit-non-zero-on-fix ]

CHANGELOG.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
# Changelog
22

3-
## [Unreleased]
3+
## [0.13.0] - 2025-10-06
44

5-
- Added support and testing for Wagtail 7.1, dropped support for Wagtail 6.4 (EOL) (@damwaingames)
6-
7-
- Add tox testing for wagtail 6.2 and 6.3 and include Django 5.1
8-
- Update the ruff github action which fixes the error seen in CI
9-
- Add tox testing for Django 5.2 and Wagtail 6.4, 7.0
10-
- Support only Wagtail >= 6.3
11-
12-
- Adds support/testing for Wagtail 6.4 and 7.0, and Django 5.2. It also drops support for Wagtail 5.x and 6.2.
5+
- Added support Wagtail 7.1, Django 5.2 (@damwaingames)
6+
- Dropped support for Wagtail < 6.3
7+
- Added custom inline pattern for strikethrough (`~TEXT~` and `--TEXT--`) (@zerolab)
138

149
## [0.12.1] - 2024-03-09
1510

@@ -19,7 +14,7 @@
1914

2015
### Changed
2116

22-
- The telepath adapter code is not longer used in Wagtail 6+
17+
- The telepath adapter code is no longer used in Wagtail 6+
2318

2419
## [0.12.0] - 2024-02-29
2520

@@ -137,7 +132,8 @@
137132
- Restructure app, refactor code. Add depreciation warnings.
138133

139134

140-
[unreleased]: https://github.com/torchbox/wagtail-markdown/compare/v0.12.1...HEAD
135+
[unreleased]: https://github.com/torchbox/wagtail-markdown/compare/v0.13.0...HEAD
136+
[0.13.0]: https://github.com/torchbox/wagtail-markdown/compare/v0.12.1...v0.13.0
141137
[0.12.1]: https://github.com/torchbox/wagtail-markdown/compare/v0.12.0...v0.12.1
142138
[0.12.0]: https://github.com/torchbox/wagtail-markdown/compare/v0.11.0...v0.12.0
143139
[0.11.1]: https://github.com/torchbox/wagtail-markdown/compare/v0.11.0...v0.11.1

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ Previously we supported custom link tags that used the target object title. They
250250

251251
⚠️ these types of tags are not reliable as titles can and will change. Support for will be removed in the future.
252252

253+
#### Inline tags
254+
255+
wagtail-markdown supports the following custom inline tags: `~TEXT~` and `~~TEXT~~` for strikethrough. The resulting
256+
markup is `<del>TEXT</del>`. Note that with this, the default allowed tags include `del` and `ins`.
257+
258+
253259
### Usage
254260

255261
You can use it as a `StreamField` block:
@@ -347,11 +353,11 @@ tox -p
347353
To run tests for a specific environment:
348354

349355
```shell
350-
tox -e py313-django5.1-wagtail6.3
356+
tox -e py313-django5.2-wagtail7.0
351357
```
352358

353359
or, a specific test
354360

355361
```shell
356-
tox -e py313-django5.1-wagtail6.3 -- tests.testapp.tests.test_admin.TestFieldsAdmin
362+
tox -e py313-django5.2-wagtail7.0 -- tests.testapp.tests.test_admin.TestFieldsAdmin
357363
```

src/wagtailmarkdown/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = (0, 12, 1)
1+
VERSION = (0, 13, 0)
22
__version__ = ".".join(map(str, VERSION))

src/wagtailmarkdown/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"ol",
3535
"hr",
3636
"br",
37+
"del",
38+
"ins",
3739
]
3840

3941
DEFAULT_ALLOWED_ATTRIBUTES = {

src/wagtailmarkdown/mdx/inlinepatterns.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import TYPE_CHECKING, Optional
2+
13
from django.apps import apps
24
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
35
from markdown import Extension
@@ -6,13 +8,18 @@
68
LINK_RE,
79
ImageInlineProcessor,
810
LinkInlineProcessor,
11+
SimpleTagInlineProcessor,
912
)
1013
from wagtail.documents import get_document_model
1114
from wagtail.images import get_image_model
1215
from wagtail.images.exceptions import InvalidFilterSpecError
1316
from wagtail.models import Page
1417

1518

19+
if TYPE_CHECKING:
20+
from markdown import Markdown
21+
22+
1623
def _options_to_dict(value: str) -> dict:
1724
"""
1825
Takes a "key=value,key2=value2" string and converts it to a dict
@@ -39,7 +46,7 @@ class ObjectLookupNegotiator:
3946
MEDIA_PREFIX = "media:"
4047

4148
@staticmethod
42-
def retrieve_page(lookup_field_value):
49+
def retrieve_page(lookup_field_value) -> Optional[Page]:
4350
try:
4451
return Page.objects.get(pk=lookup_field_value)
4552
except (Page.DoesNotExist, Page.MultipleObjectsReturned):
@@ -131,13 +138,17 @@ def getLink(self, data, index):
131138

132139

133140
class ImageExtension(Extension):
134-
def __init__(self, object_lookup_negotiator=None, **kwargs):
141+
def __init__(
142+
self,
143+
object_lookup_negotiator: Optional[ObjectLookupNegotiator] = None,
144+
**kwargs,
145+
):
135146
self.object_lookup_negotiator = (
136147
object_lookup_negotiator or ObjectLookupNegotiator
137148
)
138149
super().__init__(**kwargs)
139150

140-
def extendMarkdown(self, md):
151+
def extendMarkdown(self, md: "Markdown") -> None:
141152
md.inlinePatterns.register(
142153
ImageProcessor(
143154
pattern=IMAGE_LINK_RE,
@@ -150,13 +161,17 @@ def extendMarkdown(self, md):
150161

151162

152163
class LinkExtension(Extension):
153-
def __init__(self, object_lookup_negotiator=None, **kwargs):
164+
def __init__(
165+
self,
166+
object_lookup_negotiator: Optional[ObjectLookupNegotiator] = None,
167+
**kwargs,
168+
):
154169
self.object_lookup_negotiator = (
155170
object_lookup_negotiator or ObjectLookupNegotiator
156171
)
157172
super().__init__(**kwargs)
158173

159-
def extendMarkdown(self, md):
174+
def extendMarkdown(self, md: "Markdown") -> None:
160175
md.inlinePatterns.register(
161176
LinkProcessor(
162177
pattern=LINK_RE,
@@ -166,3 +181,11 @@ def extendMarkdown(self, md):
166181
"link",
167182
161,
168183
)
184+
185+
186+
class DelExtension(Extension):
187+
def extendMarkdown(self, md: "Markdown") -> None:
188+
"""Supports ~TEXT~ and ~~TEXT~~ syntax."""
189+
md.inlinePatterns.register(
190+
SimpleTagInlineProcessor(r"(\~{1,2})(.+?)\1", "del"), "tilde del", 170
191+
)

src/wagtailmarkdown/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
from django.utils.safestring import mark_safe
99

1010
from wagtailmarkdown.constants import DEFAULT_BLEACH_KWARGS, SETTINGS_MODE_OVERRIDE
11-
from wagtailmarkdown.mdx.inlinepatterns import ImageExtension, LinkExtension
11+
from wagtailmarkdown.mdx.inlinepatterns import (
12+
DelExtension,
13+
ImageExtension,
14+
LinkExtension,
15+
)
1216
from wagtailmarkdown.mdx.linker import LinkerExtension
1317

1418

@@ -91,6 +95,7 @@ def _get_default_markdown_kwargs():
9195
),
9296
LinkExtension(),
9397
ImageExtension(),
98+
DelExtension(),
9499
],
95100
"extension_configs": {
96101
"codehilite": [

tests/testapp/tests/test_templatetags.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,19 @@ def test_markdown_options_to_dict(self):
270270
_options_to_dict("class=left,filter=fill-200x200,foo =bar,baz="),
271271
{"class": "left", "filter": "fill-200x200", "foo": "bar", "baz": ""},
272272
)
273+
274+
def test_del_extension(self):
275+
self.assertEqual(markdown("~~removed~~"), "<p><del>removed</del></p>")
276+
self.assertEqual(
277+
markdown("pre~~removed~~post"), "<p>pre<del>removed</del>post</p>"
278+
)
279+
self.assertEqual(markdown("~removed~"), "<p><del>removed</del></p>")
280+
self.assertEqual(
281+
markdown("pre~removed~post"), "<p>pre<del>removed</del>post</p>"
282+
)
283+
284+
self.assertEqual(
285+
markdown("~~one~~~two~~~three~~"),
286+
"<p><del>one</del><del>two</del><del>three</del></p>",
287+
)
288+
self.assertEqual(markdown("~~~weird~~~"), "<p><del>~weird</del>~</p>")

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
min_version = 4.22
33

44
envlist =
5-
py{39,310,311,312}-django42-wagtail{63,70,71}
6-
py{310,311,312,313}-django{51,52}-wagtail{63,70,71}
5+
py{39}-django42-wagtail{63}
6+
py{310,311}-django{51}-wagtail{70}
7+
py{312,313}-django{52}-wagtail{71}
78

89
[gh-actions]
910
python =

0 commit comments

Comments
 (0)