Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/youtube preview: fix some issues

Adorian Dorand84a338445ec
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.md
index bf61260c6d..e3e992a451 100644
--- a/.claude/skills/developing-content-embeds/SKILL.md
+++ b/.claude/skills/developing-content-embeds/SKILL.md
@@ -67,8 +67,10 @@ All three carry `boxSize`, `hideTitle`, `editable`, `displayMode` (`data-box-siz
The shape is the link preview field `aspectRatio` (`data-aspect-ratio="9/16"`), which the
server reads from oEmbed for the video's `/shorts/` address (the watch address of a Short
answers 16:9). `getVideoAspectRatio()` validates it; every box that draws a player (the
- editing figure, `applyVideoEmbeds()`, `renderLinkEmbedHtml()`) writes it as
- `--video-aspect-ratio`, which the player's `aspect-ratio` and the 70vh cap of Full read. A
+ editing figure, `applyVideoEmbeds()`, `renderLinkEmbedHtml()`, the share renderer's
+ `setVideoBox()`) writes it as `--video-aspect-ratio`, which the player's `aspect-ratio` and the
+ 70vh cap of Full read. Only Full is capped; Medium keeps its saved width. `setVideoBox()` also
+ writes `data-box-size` on the shared section, Full for a video saved before box sizes. A
preview without one is 16:9. A new link preview field goes into `LINK_PREVIEW_DATA_ATTRIBUTES`
and `readLinkPreviewData()`, both `LinkEmbedMetadata` types (commons and the editor's
`augmentation.ts`) and the client's `fetchMetadata()`; `META_KEYS`, the schemas and the
diff --git a/packages/ckeditor5/src/plugins/content_embed/video_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/video_embed.spec.ts
index 535ad63b5a..0c52cb6ece 100644
--- a/packages/ckeditor5/src/plugins/content_embed/video_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/video_embed.spec.ts
@@ -238,7 +238,8 @@ describe("ContentEmbedVideo", () => {
});
it("has the shape of its video, and at Medium the width of that shape 20em high", () => {
- const vertical = SAVED_PREVIEW.replace(">", ` data-aspect-ratio="9/16">`);
+ const vertical = `<section class="link-embed" ${PREVIEW_ATTRIBUTES} data-aspect-ratio="9/16">`
+ + `&nbsp;</section>`;
editor.setData(`<p>Text</p>${SAVED_PREVIEW}${vertical}`);
const figures = editor.editing.view.getDomRoot()
?.querySelectorAll<HTMLElement>("figure.include-note") ?? [];
diff --git a/packages/share-theme/src/content/content.css b/packages/share-theme/src/content/content.css
index e5cfddb3ab..c9d47e0984 100644
--- a/packages/share-theme/src/content/content.css
+++ b/packages/share-theme/src/content/content.css
@@ -357,6 +357,10 @@ body:not(.math-loaded) .math-tex {
width: var(--include-note-width, auto);
}
+ &.link-embed[data-box-size="full"] {
+ max-width: min(100%, 70vh * var(--video-aspect-ratio, 16 / 9));
+ }
+
&[data-position="left"] {
margin-right: auto;
margin-left: 0;
diff --git a/packages/share-theme/src/content/link_embed.css b/packages/share-theme/src/content/link_embed.css
index 125da9d3f2..ca9e875fdf 100644
--- a/packages/share-theme/src/content/link_embed.css
+++ b/packages/share-theme/src/content/link_embed.css
@@ -70,7 +70,6 @@ html.theme-light .link-embed-favicon-light {
display: grid;
grid-template-columns: minmax(0, 1fr);
position: relative;
- max-width: min(var(--include-note-width, 640px), 70vh * var(--video-aspect-ratio, 16 / 9));
margin: auto;
border-radius: 8px;
overflow: hidden;
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index 7cf8361142..64f4b81032 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1076,6 +1076,23 @@ describe("content_renderer", () => {
expect(content).toContain(`<img class="link-embed-video-thumbnail" src="data:image/jpeg;base64,BBB"`);
});
+ it("gives a video the box size and the shape that the app draws it with", () => {
+ const video = `<section class="link-embed" data-url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"`
+ + ` data-embed-type="youtube"`;
+ const note = buildShareNote({
+ content: `<p>Videos</p>`
+ + `${video} data-aspect-ratio="9/16" data-box-size="medium" style="--include-note-width:11.3em;"></section>`
+ + `${video}></section>`
+ });
+
+ const sections = parse(String(getContent(note).content)).querySelectorAll("section");
+ expect(sections.map((section) => [ section.getAttribute("data-box-size"), section.getAttribute("style") ]))
+ .toEqual([
+ [ "medium", "--include-note-width:11.3em;--video-aspect-ratio:0.5625" ],
+ [ "full", undefined ]
+ ]);
+ });
+
it("neuters a hostile scheme in the stored URL, on a page served to anyone", () => {
// `data-*` values pass through the save-time sanitizer verbatim, so a note that arrives
// by import, ETAPI or sync can carry `data-url="javascript:…"`. It must not become a
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index aea5525b97..d3ccbef94d 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -1,12 +1,13 @@
import type { PreviewThemeSlot } from "@triliumnext/codemirror/src/code_preview.js";
import {
BLOCK_ID_ATTRIBUTE, CANVAS_ATTACHMENT_MIME, CONTENT_EMBED_KIND_ATTRIBUTE, encodeBlockParameter,
- getAttachmentEmbedHref, getDrawingPictureTitle, getEmbedKey,
- getNestedEmbedOptions, getNoteEmbedHref, isHttpUrl, isImageAttachmentRole, MIME_TYPE_AUTO,
- type MimeType, normalizeMimeTypeForCKEditor, readLinkPreviewData, renderLinkEmbedHtml,
+ getAttachmentEmbedHref, getDrawingPictureTitle, getEmbedKey, getLinkPreviewVideoId,
+ getNestedEmbedOptions, getNoteEmbedHref, getVideoAspectRatio, isHttpUrl, isImageAttachmentRole,
+ MIME_TYPE_AUTO, type MimeType, normalizeMimeTypeForCKEditor, readLinkPreviewData,
+ readVideoEmbedBox, renderLinkEmbedHtml,
renderLinkMentionHtml, resolveContentEmbed, resolveEnabledMimeTypes, SHARE_CODE_PREVIEW_THEMES,
resolveBlockReference, SHARE_HOSTED_NOTE_TYPES, SHARE_HOSTED_VIEW_TYPES, shouldSyntaxHighlight,
- sliceToBlockReference
+ sliceToBlockReference, VIDEO_ASPECT_RATIO_PROPERTY
} from "@triliumnext/commons";
import { renderToHtml as renderMarkdownToHtml } from "@triliumnext/commons/src/lib/markdown_renderer.js";
import { renderSpreadsheetToHtml } from "@triliumnext/commons/src/lib/spreadsheet/render_to_html.js";
@@ -639,6 +640,9 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
const data = readLinkPreviewData((name) => embedEl.getAttribute(name));
if (data) {
embedEl.innerHTML = renderLinkEmbedHtml(data, previewOptions);
+ if (getLinkPreviewVideoId(data)) {
+ setVideoBox(embedEl, getVideoAspectRatio(data));
+ }
}
}
@@ -787,6 +791,20 @@ function replaceWithUnavailable(embedEl: HTMLElement, parseOpts: Partial<Options
embedEl.replaceWith(...parse(html, parseOpts).childNodes);
}
+/**
+ * Writes the box size and the shape of a video on its section, which the share theme sizes it by,
+ * as `applyVideoEmbeds()` does for the app's read-only view. An older video is Full.
+ */
+function setVideoBox(section: HTMLElement, aspectRatio: number | null) {
+ const { boxSize } = readVideoEmbedBox((name) => section.getAttribute(name));
+ section.setAttribute("data-box-size", boxSize);
+ if (aspectRatio) {
+ const style = section.getAttribute("style")?.replace(/;\s*$/, "");
+ const shape = `${VIDEO_ASPECT_RATIO_PROPERTY}:${aspectRatio}`;
+ section.setAttribute("style", style ? `${style};${shape}` : shape);
+ }
+}
+
/**
* What an embedded attachment shows: a picture as an image, a canvas drawing as the picture that
* its note keeps of it, at the size of the picture, and anything else, or a drawing without a