diff --git a/requirements.txt b/requirements.txt index 7173890..2bc5147 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,6 +27,7 @@ django-csp==3.7 django-permissions-policy==4.17.0 django-enforce-host==1.1.0 django-proxy==1.2.2 +wagtail-lite-youtube-embed==0.1.0 # DRF OpenAPI dependencies uritemplate diff --git a/website/common/embed.py b/website/common/embed.py deleted file mode 100644 index a2c0863..0000000 --- a/website/common/embed.py +++ /dev/null @@ -1,38 +0,0 @@ -import re - -from django.utils.html import format_html -from wagtail.embeds.finders.oembed import OEmbedFinder -from wagtail.embeds.oembed_providers import youtube - - -class YouTubeLiteEmbedFinder(OEmbedFinder): - """ - A modified OEmbed finder uses lite-youtube-embed instead - - https://github.com/paulirish/lite-youtube-embed - """ - - EMBED_ID_RE = re.compile(r"\/embed\/(.*?)\?") - - def __init__( - self, providers: list[dict] | None = None, options: dict | None = None - ): - super().__init__(providers=[youtube], options=options) - - @classmethod - def _get_video_id(cls, html: str) -> str: - matched = cls.EMBED_ID_RE.search(html) - if matched is None: - raise ValueError(f"Unable to find video id in {html}") - return matched.group(1) - - def find_embed(self, *args: list, **kwargs: dict) -> dict: - result = super().find_embed(*args, **kwargs) - video_id = self._get_video_id(result["html"]) - result["html"] = format_html( - "", - video_id, - result["title"], - result["thumbnail_url"], - ) - return result diff --git a/website/common/tests/test_utils.py b/website/common/tests/test_utils.py index 5f92a38..eb899eb 100644 --- a/website/common/tests/test_utils.py +++ b/website/common/tests/test_utils.py @@ -2,7 +2,6 @@ from django.conf import settings from django.test import SimpleTestCase from wagtail.rich_text import features as richtext_feature_registry -from website.common.embed import YouTubeLiteEmbedFinder from website.common.utils import ( count_words, extract_text, @@ -11,18 +10,6 @@ from website.common.utils import ( ) -class YouTubeLiteEmbedFinderTestCase(SimpleTestCase): - def test_finds_video_id(self) -> None: - self.assertEqual( - YouTubeLiteEmbedFinder._get_video_id( - '' - ), - "dQw4w9WgXcQ", - ) - with self.assertRaises(ValueError): - YouTubeLiteEmbedFinder._get_video_id("something-else") - - class TableOfContentsTestCase(SimpleTestCase): def test_creates_table_of_contents(self) -> None: toc = get_table_of_contents( diff --git a/website/settings.py b/website/settings.py index 9dfc50f..34e8dea 100644 --- a/website/settings.py +++ b/website/settings.py @@ -233,7 +233,7 @@ WAGTAILIMAGES_IMAGE_MODEL = "images.CustomImage" WAGTAILEMBEDS_FINDERS = [ { - "class": "website.common.embed.YouTubeLiteEmbedFinder", + "class": "lite_youtube_embed.LiteYouTubeEmbedFinder", }, { "class": "wagtail.embeds.finders.oembed",