Video: YouTube and Twitch
The built-in youtube and twitch Tools put video by link in the Editor and RichTextView, with server-side sanitizing, CSP and load on click.
The author pastes a link to a video, and the Document keeps exactly that: a link. The kit draws the
player, both in the Editor and in RichTextView. You write no code for it. You only name the youtube
and twitch Tools in tools.
The Document HTML
<p>Insert a video from the “+” menu, or paste a bare link into an empty line.</p><div data-block="video" data-provider="youtube" data-src="https://www.youtube.com/watch?v=aqz-KE-bpKQ"><a href="https://www.youtube.com/watch?v=aqz-KE-bpKQ" rel="noopener noreferrer">https://www.youtube.com/watch?v=aqz-KE-bpKQ</a></div><div data-block="video" data-provider="twitch" data-src="https://www.twitch.tv/aws"><a href="https://www.twitch.tv/aws" rel="noopener noreferrer">https://www.twitch.tv/aws</a></div><p></p>
Try it
The Document on the left holds one YouTube video and one Twitch recording. To add another, press “+” on an
empty line and choose “Video”, or paste a bare link into an empty line. Open “The Document HTML” under the
field: the Document holds a link, and there is no iframe in it.
Turn it on
The Tools are in no Preset: the player pulls in a third-party service, and a kit update must not do that
where nobody asked for it. Name them in tools next to a Preset:
import { RICH_TEXT_PRESETS, RichTextEditor, RichTextView } from "@olddevs/ui/editor";
const TOOLS = [...RICH_TEXT_PRESETS.full, "youtube", "twitch"];
<RichTextEditor aria-label="Post" tools={TOOLS} value={html} onChange={setHtml} />;
<RichTextView html={html} tools={TOOLS} />;The set decides which services are allowed: only "youtube", only "twitch", or both. A link to a
service that isn't in tools stays a plain link. All the rules are in the Reference, in the
“Video by link” section.
On the server
The Document comes from the client, so the server sanitizes it on save with the same tools array:
"use server";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
import { sanitizeRichText } from "@olddevs/ui/editor/schema";
import { EDITOR_TOOLS } from "@/editor/tools"; // [...RICH_TEXT_PRESETS.full, "youtube", "twitch"]
const DOMPurify = createDOMPurify(new JSDOM("").window);
export async function savePost(id: string, dirty: string) {
await db.posts.update(id, { html: sanitizeRichText(dirty, DOMPurify, EDITOR_TOOLS) });
}The sanitizer still strips iframe, with these Tools or without them. A video Block passes only if its
service and URL are a canonical link, exactly what the Editor writes. Otherwise the Block loses its
wrapper and the link stays.
The page that shows the player has to allow its iframe. Add a line to your Content-Security-Policy, and
list only the hosts of the services you use:
frame-src https://www.youtube-nocookie.com https://player.twitch.tv https://clips.twitch.tv;Without it the browser blocks the iframe. The Document and the link are unaffected.
Privacy: load on click
By default the player appears in place of the Block at once: an iframe with loading="lazy", in the
Editor and in the View. When the page must not contact the service until the reader asks, turn
autoloading off with the autoload: false Option:
import { RICH_TEXT_PRESETS, tool } from "@olddevs/ui/editor";
const TOOLS = [
...RICH_TEXT_PRESETS.full,
tool("youtube", { autoload: false }),
tool("twitch", { autoload: false }),
];The video is then a placeholder with the service name, the link and a “Play” button, and no request goes
to the service before the click. The View reads the Option from its own tools, so pass it the same array.
The Option doesn't change the Document: it still holds only a link.
How it works
The Document holds a link, not a player
The Block stores the service (data-provider), the canonical video link (data-src) and a plain link
inside. The player URL and the page domain are not in the Document. That is why a Document saved in an
admin panel works on a public site with a different domain, and why player parameters can change without
rewriting Documents.
Without JavaScript the reader sees the link
Without the engine, RichTextView shows the Block as a link: it is server HTML. The kit's Island puts the
player in place, and it mounts only when the HTML contains a video Block, so a page without video doesn't
pay for it. Markdown and any other display see the link too: getMarkdown() writes it on its own line.
How to insert a video
Use the “Video” item of the Insert menu (“+” or /video): a popover with a link field, and the service is
detected from the URL. A bare link pasted into an empty line becomes a Block right away. A link among
words stays a link. The usual forms are recognized: youtu.be/…, youtube.com/watch?v=…, /shorts/,
twitch.tv/videos/…, clips and channels.
The player URL is built at display time
YouTube always uses youtube-nocookie.com; Twitch uses player.twitch.tv with parent set to the domain
of the current page. Neither is stored in the Document.
When you need your own Tool
The built-in Tools cover YouTube and Twitch. Another service, such as Vimeo, needs a
custom Tool: a Block with the video id and an
Island that draws the player in RichTextView. The “Spoiler” Recipe is a
ready model of a Block with an Island.
Insert a video from the “+” menu, or paste a bare link into an empty line.