Use lite-youtube-embed for YouTube embeds
This commit is contained in:
parent
c23c03ec33
commit
f0239b40fc
10 changed files with 88 additions and 2 deletions
13
package-lock.json
generated
13
package-lock.json
generated
|
@ -11,7 +11,8 @@
|
||||||
"@fortawesome/fontawesome-free": "^6.1.1",
|
"@fortawesome/fontawesome-free": "^6.1.1",
|
||||||
"bulma": "^0.9.4",
|
"bulma": "^0.9.4",
|
||||||
"darkreader": "^4.9.51",
|
"darkreader": "^4.9.51",
|
||||||
"elevator.js": "^1.0.1"
|
"elevator.js": "^1.0.1",
|
||||||
|
"lite-youtube-embed": "^0.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"esbuild": "^0.14.43",
|
"esbuild": "^0.14.43",
|
||||||
|
@ -1070,6 +1071,11 @@
|
||||||
"integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==",
|
"integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/lite-youtube-embed": {
|
||||||
|
"version": "0.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/lite-youtube-embed/-/lite-youtube-embed-0.2.0.tgz",
|
||||||
|
"integrity": "sha512-XXXAk5sbvtjjwbie3XG+6HppgTm1HTGL/Uk9z9NkJH53o7puZLur434heHzAjkS60hZB3vT4ls25zl5rMiX4EA=="
|
||||||
|
},
|
||||||
"node_modules/load-json-file": {
|
"node_modules/load-json-file": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz",
|
||||||
|
@ -2267,6 +2273,11 @@
|
||||||
"integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==",
|
"integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"lite-youtube-embed": {
|
||||||
|
"version": "0.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/lite-youtube-embed/-/lite-youtube-embed-0.2.0.tgz",
|
||||||
|
"integrity": "sha512-XXXAk5sbvtjjwbie3XG+6HppgTm1HTGL/Uk9z9NkJH53o7puZLur434heHzAjkS60hZB3vT4ls25zl5rMiX4EA=="
|
||||||
|
},
|
||||||
"load-json-file": {
|
"load-json-file": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz",
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
"@fortawesome/fontawesome-free": "^6.1.1",
|
"@fortawesome/fontawesome-free": "^6.1.1",
|
||||||
"bulma": "^0.9.4",
|
"bulma": "^0.9.4",
|
||||||
"darkreader": "^4.9.51",
|
"darkreader": "^4.9.51",
|
||||||
"elevator.js": "^1.0.1"
|
"elevator.js": "^1.0.1",
|
||||||
|
"lite-youtube-embed": "^0.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
static/src/js/lite-youtube-embed.js
Normal file
1
static/src/js/lite-youtube-embed.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
require("lite-youtube-embed");
|
|
@ -7,3 +7,11 @@ div.block-image {
|
||||||
.content > div:not(:last-child) {
|
.content > div:not(:last-child) {
|
||||||
margin-bottom: $content-block-margin-bottom;
|
margin-bottom: $content-block-margin-bottom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.block-embed {
|
||||||
|
lite-youtube {
|
||||||
|
max-width: $embed_width;
|
||||||
|
min-width: $embed_width;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
$embed_width: 85%;
|
1
static/src/scss/lite-youtube-embed.scss
Normal file
1
static/src/scss/lite-youtube-embed.scss
Normal file
|
@ -0,0 +1 @@
|
||||||
|
@import "lite-youtube-embed/src/lite-yt-embed";
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}">
|
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}">
|
||||||
<link rel="stylesheet" type="text/css" href="{% static 'contrib/fontawesome/css/all.min.css' %}">
|
<link rel="stylesheet" type="text/css" href="{% static 'contrib/fontawesome/css/all.min.css' %}">
|
||||||
|
<link rel="stylesheet" type="text/css" href="{% static 'css/lite-youtube-embed.css' %}">
|
||||||
<link rel="stylesheet" type="text/css" href="{% url 'static-pygments:styles' 'default' %}">
|
<link rel="stylesheet" type="text/css" href="{% url 'static-pygments:styles' 'default' %}">
|
||||||
|
|
||||||
{% block extra_css %}{% endblock %}
|
{% block extra_css %}{% endblock %}
|
||||||
|
@ -40,6 +41,7 @@
|
||||||
|
|
||||||
|
|
||||||
<script async defer type="text/javascript" src="{% static 'js/base.js' %}"></script>
|
<script async defer type="text/javascript" src="{% static 'js/base.js' %}"></script>
|
||||||
|
<script async defer type="text/javascript" src="{% static 'js/lite-youtube-embed.js' %}"></script>
|
||||||
|
|
||||||
{% block darkmode %}
|
{% block darkmode %}
|
||||||
<script type="text/javascript" src="{% static 'js/darkreader.js' %}"></script>
|
<script type="text/javascript" src="{% static 'js/darkreader.js' %}"></script>
|
||||||
|
|
38
website/common/embed.py
Normal file
38
website/common/embed.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
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
|
|
@ -1,5 +1,6 @@
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
from .embed import YouTubeLiteEmbedFinder
|
||||||
from .models import BasePage
|
from .models import BasePage
|
||||||
from .utils import get_page_models
|
from .utils import get_page_models
|
||||||
|
|
||||||
|
@ -15,3 +16,15 @@ class BasePageTestCase(SimpleTestCase):
|
||||||
issubclass(page_model, BasePage),
|
issubclass(page_model, BasePage),
|
||||||
f"{page_model} does not inherit from {BasePage}.",
|
f"{page_model} does not inherit from {BasePage}.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
|
@ -144,6 +144,16 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
||||||
|
|
||||||
WAGTAILIMAGES_IMAGE_MODEL = "images.CustomImage"
|
WAGTAILIMAGES_IMAGE_MODEL = "images.CustomImage"
|
||||||
|
|
||||||
|
WAGTAILEMBEDS_FINDERS = [
|
||||||
|
{
|
||||||
|
"class": "website.common.embed.YouTubeLiteEmbedFinder",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"class": "wagtail.embeds.finders.oembed",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
# Add django-browser-reload
|
# Add django-browser-reload
|
||||||
INSTALLED_APPS.append("django_browser_reload")
|
INSTALLED_APPS.append("django_browser_reload")
|
||||||
|
|
Loading…
Reference in a new issue