Extract lite-youtube embed into `wagtail-lite-youtube-embed` package

This commit is contained in:
Jake Howard 2023-10-27 17:11:53 +01:00
parent fcbdb248fa
commit 366dc6a123
Signed by: jake
GPG Key ID: 57AFB45680EDD477
4 changed files with 2 additions and 52 deletions

View File

@ -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

View File

@ -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(
"<lite-youtube videoid='{}' playlabel='{}' backgroundImage='{}'></lite-youtube>",
video_id,
result["title"],
result["thumbnail_url"],
)
return result

View File

@ -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(
'<iframe width="200" height="113" src="https://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen title=""></iframe>'
),
"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(

View File

@ -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",