astro-livid 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +212 -2
- package/index.ts +5 -0
- package/package.json +49 -4
- package/src/Livid.astro +193 -0
- package/src/oembed.ts +64 -0
- package/src/params.ts +109 -0
- package/src/url.ts +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jason Chance
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,213 @@
|
|
|
1
|
-
#
|
|
1
|
+
# astro-livid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An Astro component for embedding [Livid](https://livid.com) videos. Livid is ad-free video hosting and a good Vimeo alternative. `<Livid />` renders a responsive, lazy-loaded iframe with typed props for every one of Livid's [advanced embedding parameters](https://support.livid.com/article/46-advanced-embedding-parameters). There's nothing to configure, and no client JavaScript unless you turn on the click-to-load `facade` (see below).
|
|
4
|
+
|
|
5
|
+
It works with Astro 6 and 7. If your site runs on Jekyll, see the sister project, [jekyll-livid](https://github.com/jchance/jekyll-livid).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install astro-livid
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or with pnpm:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add astro-livid
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
There's no integration to add to `astro.config.mjs`.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Basic embed
|
|
24
|
+
|
|
25
|
+
```astro
|
|
26
|
+
---
|
|
27
|
+
import { Livid } from 'astro-livid';
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
<Livid id="YEsVONDwAoi8" />
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`id` takes the video ID or the full share URL, so both of these work:
|
|
34
|
+
|
|
35
|
+
```astro
|
|
36
|
+
<Livid id="YEsVONDwAoi8" />
|
|
37
|
+
<Livid id="https://livid.com/watch/YEsVONDwAoi8" />
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### With parameters
|
|
41
|
+
|
|
42
|
+
Props are the camelCase versions of Livid's parameters (`livid_logo` becomes `lividLogo`, `quality_selector` becomes `qualitySelector`). They're typed, so your editor autocompletes them. Booleans work bare:
|
|
43
|
+
|
|
44
|
+
```astro
|
|
45
|
+
<Livid id="YEsVONDwAoi8" autoplay muted loop />
|
|
46
|
+
<Livid id="YEsVONDwAoi8" pip speed title={false} />
|
|
47
|
+
<Livid id="YEsVONDwAoi8" color="ff0000" controls={false} />
|
|
48
|
+
<Livid id="YEsVONDwAoi8" dnt />
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### In MDX
|
|
52
|
+
|
|
53
|
+
The same import works in MDX files:
|
|
54
|
+
|
|
55
|
+
```mdx
|
|
56
|
+
import { Livid } from 'astro-livid';
|
|
57
|
+
|
|
58
|
+
Here's the walkthrough:
|
|
59
|
+
|
|
60
|
+
<Livid id="YEsVONDwAoi8" />
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Start time
|
|
64
|
+
|
|
65
|
+
`t` is the start position in seconds. It's added to the URL as a hash fragment (`#t=30`). Every other prop goes in the query string.
|
|
66
|
+
|
|
67
|
+
```astro
|
|
68
|
+
<Livid id="YEsVONDwAoi8" t={30} />
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Aspect ratio
|
|
72
|
+
|
|
73
|
+
The wrapper uses a `16/9` CSS `aspect-ratio` by default. Pass `aspect` to change it, for example for vertical video:
|
|
74
|
+
|
|
75
|
+
```astro
|
|
76
|
+
<Livid id="YEsVONDwAoi8" aspect="9/16" />
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Click to load
|
|
80
|
+
|
|
81
|
+
A Livid iframe loads the whole player when the page loads. With `facade`, the page renders a poster image and a play button instead, and swaps in the player when the visitor clicks. Pages stay lighter, especially with several videos.
|
|
82
|
+
|
|
83
|
+
```astro
|
|
84
|
+
<Livid id="YEsVONDwAoi8" facade />
|
|
85
|
+
<Livid id="YEsVONDwAoi8" poster="/images/walkthrough.jpg" />
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`poster` is an image URL or path you supply. Setting it turns facade mode on, so you don't need `facade` as well. Without it, the component asks Livid's oEmbed endpoint for the video's title and thumbnail. That lookup happens at build time, once per video, and the poster image then loads from Livid's servers. Pass `poster` to host the image yourself. With both `poster` and `iframeTitle`, there's no lookup at all.
|
|
89
|
+
|
|
90
|
+
If the lookup fails (a private or domain-restricted video, or a build that's offline) and you haven't passed a `poster`, you get a warning in the build log and a plain iframe, the same as without `facade`. In SSR, the lookup runs the first time the page renders and is cached in memory.
|
|
91
|
+
|
|
92
|
+
A few details:
|
|
93
|
+
|
|
94
|
+
- The play button is a real `<button>` labeled "Play video: {title}". The title is `iframeTitle`, else Livid's title, else "Livid video".
|
|
95
|
+
- The click loads the player with `autoplay=true`, since the visitor just pressed play. Pass `autoplay={false}` to turn that off.
|
|
96
|
+
- A `<noscript>` iframe covers visitors without JavaScript.
|
|
97
|
+
- The click script is a small custom element, bundled by Astro and shared by every facade on the page. Pages with no facade don't get it.
|
|
98
|
+
|
|
99
|
+
Set the play badge color with the `--livid-facade-accent` CSS custom property. The `color` prop sets it for you.
|
|
100
|
+
|
|
101
|
+
```css
|
|
102
|
+
.livid-embed--facade {
|
|
103
|
+
--livid-facade-accent: #c2410c;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Accessible name
|
|
108
|
+
|
|
109
|
+
`iframeTitle` sets the iframe's accessible name. It defaults to "Livid video". It's separate from `title`, which shows or hides the video's title inside the player.
|
|
110
|
+
|
|
111
|
+
```astro
|
|
112
|
+
<Livid id="YEsVONDwAoi8" iframeTitle="Board meeting, October 2026" />
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Props
|
|
116
|
+
|
|
117
|
+
Besides the embedding parameters below, the component takes `id` (required), `iframeTitle` (default `"Livid video"`), `aspect` (default `16/9`), `class`, `facade` and `poster`.
|
|
118
|
+
|
|
119
|
+
Parameters marked Pro/Premium only take effect on videos from a Livid Pro or Premium account. The Livid default is the value the player uses when you leave the prop out. Livid's own [reference](https://support.livid.com/article/46-advanced-embedding-parameters) is the source of truth if something changes on their end.
|
|
120
|
+
|
|
121
|
+
| Prop | Values | Livid default | Notes |
|
|
122
|
+
|------|--------|---------------|-------|
|
|
123
|
+
| `airplay` | boolean | true | AirPlay button (Safari only). Pro/Premium. |
|
|
124
|
+
| `autoplay` | boolean | false | Start playing on load. Browsers usually require `muted` too. |
|
|
125
|
+
| `background` | boolean | false | Background mode: no controls, looped, autoplaying and muted. Pro/Premium. |
|
|
126
|
+
| `cc` | boolean | true | Closed captions button. Pro/Premium. |
|
|
127
|
+
| `chromecast` | boolean | true | Chromecast button. Pro/Premium. |
|
|
128
|
+
| `color` | hex code | | Player accent color, with or without the leading `#`. Pro/Premium. |
|
|
129
|
+
| `colors` | up to 4 hex codes | | An array or a comma-separated string. Pro/Premium. |
|
|
130
|
+
| `controls` | boolean | true | Show the player controls. Pro/Premium. |
|
|
131
|
+
| `customLogo` | boolean | the video's setting | Show your custom logo. Pro/Premium. |
|
|
132
|
+
| `dnt` | boolean | false | Do not track. Turns off analytics for this embed. |
|
|
133
|
+
| `fullscreen` | boolean | true | Fullscreen button. Pro/Premium. |
|
|
134
|
+
| `initialQuality` | `240p` to `4k` | | Quality the player starts at. Pro/Premium. |
|
|
135
|
+
| `keyboard` | boolean | true | Keyboard shortcuts. |
|
|
136
|
+
| `lividLogo` | boolean | true | Show the Livid logo. Pro/Premium. |
|
|
137
|
+
| `loop` | boolean | false | Loop the video. |
|
|
138
|
+
| `maxQuality` | `240p` to `4k` | | Highest quality the player may use. Pro/Premium. |
|
|
139
|
+
| `minQuality` | `240p` to `4k` | | Lowest quality the player may use. Pro/Premium. |
|
|
140
|
+
| `muted` | boolean | false | Start muted. |
|
|
141
|
+
| `pip` | boolean | true | Picture-in-picture button. Pro/Premium. |
|
|
142
|
+
| `playButtonPosition` | `auto`, `bottom`, `center` | auto | Where the play button sits. Pro/Premium. |
|
|
143
|
+
| `playsinline` | boolean | true | Play inline on mobile instead of going fullscreen. |
|
|
144
|
+
| `preload` | `auto`, `metadata`, `none` | none | How much of the video to load before play. |
|
|
145
|
+
| `progressBar` | boolean | true | Progress bar. Pro/Premium. |
|
|
146
|
+
| `quality` | `240p` to `4k` | | Fixed quality for the player. Pro/Premium. |
|
|
147
|
+
| `qualitySelector` | boolean | true | Quality selector menu. Pro/Premium. |
|
|
148
|
+
| `share` | boolean | true | Share button. Pro/Premium. |
|
|
149
|
+
| `speed` | boolean | true | Playback speed control. Pro/Premium. |
|
|
150
|
+
| `t` | seconds | 0 | Start position. Added to the URL as `#t=N`. |
|
|
151
|
+
| `timeDisplay` | boolean | false | Show the time display. Pro/Premium. |
|
|
152
|
+
| `title` | boolean | true | Show the video's title in the player. Pro/Premium. |
|
|
153
|
+
| `transparent` | boolean | true | Transparent player background. |
|
|
154
|
+
| `volume` | boolean | true | Volume control. Pro/Premium. |
|
|
155
|
+
|
|
156
|
+
The quality values are `240p`, `360p`, `540p`, `720p`, `1080p`, `2k` and `4k`.
|
|
157
|
+
|
|
158
|
+
## Styling
|
|
159
|
+
|
|
160
|
+
The component outputs a `div` with the class `livid-embed` around a responsive iframe. It adds no margin, on purpose: every site has its own spacing scale.
|
|
161
|
+
|
|
162
|
+
If the embed sits too close to the content below it, add a rule to your CSS:
|
|
163
|
+
|
|
164
|
+
```css
|
|
165
|
+
.livid-embed {
|
|
166
|
+
margin-bottom: 32px; /* adjust to match your site's paragraph spacing */
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Astro scopes component styles, so put this in a global stylesheet or a `<style is:global>` block.
|
|
171
|
+
|
|
172
|
+
## URL Helpers
|
|
173
|
+
|
|
174
|
+
If you want the embed URL without the component, the package also exports two functions, plus `fetchOembed`, which the facade uses to look up a video's title and thumbnail:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { buildEmbedUrl, parseVideoId } from 'astro-livid';
|
|
178
|
+
|
|
179
|
+
parseVideoId('https://livid.com/watch/YEsVONDwAoi8'); // 'YEsVONDwAoi8'
|
|
180
|
+
|
|
181
|
+
buildEmbedUrl('YEsVONDwAoi8', { autoplay: true, muted: true, t: 30 });
|
|
182
|
+
// 'https://livid.com/embed/YEsVONDwAoi8?autoplay=true&muted=true#t=30'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`buildEmbedUrl(id, params)` takes the same params as the component's props.
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { fetchOembed } from 'astro-livid';
|
|
189
|
+
|
|
190
|
+
await fetchOembed('YEsVONDwAoi8');
|
|
191
|
+
// { title: 'Showcases', thumbnailUrl: 'https://api.livid.com/…jpg', thumbnailWidth: 1920, thumbnailHeight: 1080 }
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`fetchOembed(id)` returns `null` if the lookup fails, and caches each video in memory. The types `LividParams`, `LividQuality` and `LividOembed` are exported too.
|
|
195
|
+
|
|
196
|
+
## Development
|
|
197
|
+
|
|
198
|
+
The project uses Node 24 (see `.nvmrc`) and pnpm.
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
pnpm install
|
|
202
|
+
pnpm test
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Run the demo site in `demo/`:
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
pnpm demo
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
MIT. astro-livid isn't affiliated with Livid.
|
package/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as Livid } from './src/Livid.astro';
|
|
2
|
+
export { buildEmbedUrl, parseVideoId } from './src/url';
|
|
3
|
+
export type { LividParams, LividQuality } from './src/params';
|
|
4
|
+
export { fetchOembed } from './src/oembed';
|
|
5
|
+
export type { LividOembed } from './src/oembed';
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-livid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "An Astro component for embedding Livid videos in a responsive iframe, with typed props for all of Livid's advanced embedding parameters.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Jason Chance (https://jasonchance.com)",
|
|
8
|
+
"homepage": "https://jasonchance.com/projects/astro-livid/",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/jchance/astro-livid.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/jchance/astro-livid/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"astro-component",
|
|
18
|
+
"withastro",
|
|
19
|
+
"astro",
|
|
20
|
+
"livid",
|
|
21
|
+
"video",
|
|
22
|
+
"embed",
|
|
23
|
+
"iframe"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.ts",
|
|
27
|
+
"./Livid.astro": "./src/Livid.astro"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.ts",
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"check": "astro check",
|
|
36
|
+
"demo": "pnpm --dir demo dev"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"astro": "^6.0.0 || ^7.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@astrojs/check": "^0.9.12",
|
|
43
|
+
"astro": "^7.4.13",
|
|
44
|
+
"typescript": "^5.9.0",
|
|
45
|
+
"vitest": "^5.1.2"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.12.0"
|
|
49
|
+
},
|
|
50
|
+
"packageManager": "pnpm@11.24.0"
|
|
51
|
+
}
|
package/src/Livid.astro
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { buildEmbedUrl, parseVideoId } from './url';
|
|
3
|
+
import { fetchOembed } from './oembed';
|
|
4
|
+
import type { LividParams } from './params';
|
|
5
|
+
|
|
6
|
+
export interface Props extends LividParams {
|
|
7
|
+
/** A Livid video ID, or a livid.com watch or embed URL. */
|
|
8
|
+
id: string;
|
|
9
|
+
/** Accessible name for the iframe. Not the same as `title`, which shows or hides the title inside the player. */
|
|
10
|
+
iframeTitle?: string;
|
|
11
|
+
/** CSS aspect-ratio value for the player. */
|
|
12
|
+
aspect?: string;
|
|
13
|
+
/** Extra class names for the wrapper. */
|
|
14
|
+
class?: string;
|
|
15
|
+
/** Show a poster and play button, and load the player on click. */
|
|
16
|
+
facade?: boolean;
|
|
17
|
+
/** Poster image URL or path for the facade. Setting it turns facade mode on. */
|
|
18
|
+
poster?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const {
|
|
22
|
+
id,
|
|
23
|
+
iframeTitle,
|
|
24
|
+
aspect = '16/9',
|
|
25
|
+
class: className,
|
|
26
|
+
facade = false,
|
|
27
|
+
poster: posterProp,
|
|
28
|
+
...params
|
|
29
|
+
} = Astro.props;
|
|
30
|
+
|
|
31
|
+
const videoId = parseVideoId(id);
|
|
32
|
+
const facadeOn = Boolean(facade || posterProp);
|
|
33
|
+
|
|
34
|
+
let posterUrl: string | undefined = posterProp || undefined;
|
|
35
|
+
let posterWidth: number | undefined;
|
|
36
|
+
let posterHeight: number | undefined;
|
|
37
|
+
let accessibleTitle = iframeTitle;
|
|
38
|
+
|
|
39
|
+
if (facadeOn && (!posterUrl || !iframeTitle)) {
|
|
40
|
+
const oembed = await fetchOembed(videoId);
|
|
41
|
+
if (oembed) {
|
|
42
|
+
if (!posterUrl && oembed.thumbnailUrl) {
|
|
43
|
+
posterUrl = oembed.thumbnailUrl;
|
|
44
|
+
posterWidth = oembed.thumbnailWidth;
|
|
45
|
+
posterHeight = oembed.thumbnailHeight;
|
|
46
|
+
}
|
|
47
|
+
accessibleTitle ??= oembed.title;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
accessibleTitle ||= 'Livid video';
|
|
51
|
+
|
|
52
|
+
const useFacade = facadeOn && Boolean(posterUrl);
|
|
53
|
+
|
|
54
|
+
const iframeStyle = 'position:absolute;top:0;left:0;width:100%;height:100%;border:0;';
|
|
55
|
+
const iframeAttrs: astroHTML.JSX.IframeHTMLAttributes = {
|
|
56
|
+
src: buildEmbedUrl(videoId, params),
|
|
57
|
+
title: accessibleTitle,
|
|
58
|
+
loading: 'lazy',
|
|
59
|
+
allow: 'autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture; web-share',
|
|
60
|
+
allowfullscreen: true,
|
|
61
|
+
referrerpolicy: 'strict-origin-when-cross-origin',
|
|
62
|
+
style: iframeStyle,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Click-time URL: the visitor already pressed play, so autoplay on unless they said otherwise.
|
|
66
|
+
const playSrc = buildEmbedUrl(videoId, { ...params, autoplay: params.autoplay ?? true });
|
|
67
|
+
|
|
68
|
+
const accent = typeof params.color === 'string' && params.color.trim()
|
|
69
|
+
? params.color.trim().replace(/^#/, '')
|
|
70
|
+
: undefined;
|
|
71
|
+
const accentValid = accent && /^[0-9a-fA-F]{3,8}$/.test(accent) ? `#${accent}` : undefined;
|
|
72
|
+
const wrapperStyle = `position:relative;width:100%;aspect-ratio:${aspect};`;
|
|
73
|
+
const facadeStyle = accentValid ? `${wrapperStyle}--livid-facade-accent:${accentValid};` : wrapperStyle;
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
{
|
|
77
|
+
useFacade ? (
|
|
78
|
+
<div class:list={['livid-embed', 'livid-embed--facade', className]} style={facadeStyle}>
|
|
79
|
+
<livid-facade
|
|
80
|
+
data-src={playSrc}
|
|
81
|
+
data-title={accessibleTitle}
|
|
82
|
+
data-allow={iframeAttrs.allow}
|
|
83
|
+
data-referrerpolicy={iframeAttrs.referrerpolicy}
|
|
84
|
+
>
|
|
85
|
+
<button type="button" class="livid-facade__button" aria-label={`Play video: ${accessibleTitle}`}>
|
|
86
|
+
<img
|
|
87
|
+
class="livid-facade__poster"
|
|
88
|
+
src={posterUrl}
|
|
89
|
+
alt=""
|
|
90
|
+
loading="lazy"
|
|
91
|
+
decoding="async"
|
|
92
|
+
width={posterWidth}
|
|
93
|
+
height={posterHeight}
|
|
94
|
+
/>
|
|
95
|
+
<svg class="livid-facade__icon" viewBox="0 0 68 48" aria-hidden="true" focusable="false">
|
|
96
|
+
<rect class="livid-facade__badge" width="68" height="48" rx="12" />
|
|
97
|
+
<path d="M27 14v20l18-10z" fill="#fff" />
|
|
98
|
+
</svg>
|
|
99
|
+
</button>
|
|
100
|
+
</livid-facade>
|
|
101
|
+
<noscript>
|
|
102
|
+
<iframe {...iframeAttrs} />
|
|
103
|
+
</noscript>
|
|
104
|
+
<script>
|
|
105
|
+
class LividFacade extends HTMLElement {
|
|
106
|
+
connectedCallback() {
|
|
107
|
+
const button = this.querySelector('button');
|
|
108
|
+
if (!button || button.dataset.lividBound) return;
|
|
109
|
+
button.dataset.lividBound = 'true';
|
|
110
|
+
button.addEventListener('click', () => this.activate(button), { once: true });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
activate(button: HTMLButtonElement) {
|
|
114
|
+
if (this.querySelector('iframe')) return;
|
|
115
|
+
const iframe = document.createElement('iframe');
|
|
116
|
+
iframe.src = this.dataset.src ?? '';
|
|
117
|
+
iframe.title = this.dataset.title ?? 'Livid video';
|
|
118
|
+
iframe.allow = this.dataset.allow ?? '';
|
|
119
|
+
iframe.allowFullscreen = true;
|
|
120
|
+
iframe.referrerPolicy = (this.dataset.referrerpolicy ?? '') as ReferrerPolicy;
|
|
121
|
+
iframe.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;border:0;';
|
|
122
|
+
button.replaceWith(iframe);
|
|
123
|
+
iframe.focus();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!customElements.get('livid-facade')) customElements.define('livid-facade', LividFacade);
|
|
128
|
+
</script>
|
|
129
|
+
</div>
|
|
130
|
+
) : (
|
|
131
|
+
<div class:list={['livid-embed', className]} style={wrapperStyle}>
|
|
132
|
+
<iframe {...iframeAttrs} />
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
<style is:global>
|
|
138
|
+
.livid-embed--facade livid-facade {
|
|
139
|
+
display: block;
|
|
140
|
+
position: absolute;
|
|
141
|
+
inset: 0;
|
|
142
|
+
}
|
|
143
|
+
.livid-facade__button {
|
|
144
|
+
all: unset;
|
|
145
|
+
box-sizing: border-box;
|
|
146
|
+
position: absolute;
|
|
147
|
+
inset: 0;
|
|
148
|
+
display: block;
|
|
149
|
+
cursor: pointer;
|
|
150
|
+
overflow: hidden;
|
|
151
|
+
background: #000;
|
|
152
|
+
}
|
|
153
|
+
.livid-facade__poster {
|
|
154
|
+
position: absolute;
|
|
155
|
+
inset: 0;
|
|
156
|
+
width: 100%;
|
|
157
|
+
height: 100%;
|
|
158
|
+
object-fit: cover;
|
|
159
|
+
display: block;
|
|
160
|
+
}
|
|
161
|
+
.livid-facade__icon {
|
|
162
|
+
position: absolute;
|
|
163
|
+
top: 50%;
|
|
164
|
+
left: 50%;
|
|
165
|
+
width: 68px;
|
|
166
|
+
height: 48px;
|
|
167
|
+
transform: translate(-50%, -50%);
|
|
168
|
+
transition: transform 0.15s ease;
|
|
169
|
+
}
|
|
170
|
+
.livid-facade__badge {
|
|
171
|
+
fill: var(--livid-facade-accent, rgb(0 0 0 / 0.7));
|
|
172
|
+
transition: opacity 0.15s ease;
|
|
173
|
+
}
|
|
174
|
+
.livid-facade__button:hover .livid-facade__icon,
|
|
175
|
+
.livid-facade__button:focus-visible .livid-facade__icon {
|
|
176
|
+
transform: translate(-50%, -50%) scale(1.1);
|
|
177
|
+
}
|
|
178
|
+
.livid-facade__button:focus-visible {
|
|
179
|
+
outline: 3px solid #fff;
|
|
180
|
+
outline-offset: -6px;
|
|
181
|
+
box-shadow: inset 0 0 0 6px #1a73e8;
|
|
182
|
+
}
|
|
183
|
+
@media (prefers-reduced-motion: reduce) {
|
|
184
|
+
.livid-facade__icon,
|
|
185
|
+
.livid-facade__badge {
|
|
186
|
+
transition: none;
|
|
187
|
+
}
|
|
188
|
+
.livid-facade__button:hover .livid-facade__icon,
|
|
189
|
+
.livid-facade__button:focus-visible .livid-facade__icon {
|
|
190
|
+
transform: translate(-50%, -50%);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
</style>
|
package/src/oembed.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { parseVideoId } from './url';
|
|
2
|
+
|
|
3
|
+
export interface LividOembed {
|
|
4
|
+
title?: string;
|
|
5
|
+
thumbnailUrl?: string;
|
|
6
|
+
thumbnailWidth?: number;
|
|
7
|
+
thumbnailHeight?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const TIMEOUT_MS = 5000;
|
|
11
|
+
const cache = new Map<string, Promise<LividOembed | null>>();
|
|
12
|
+
|
|
13
|
+
function positiveInt(value: unknown): number | undefined {
|
|
14
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function request(id: string): Promise<LividOembed> {
|
|
18
|
+
const endpoint = `https://livid.com/oembed?url=${encodeURIComponent(`https://livid.com/watch/${id}`)}`;
|
|
19
|
+
const response = await fetch(endpoint, { signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
20
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
21
|
+
const data: unknown = await response.json();
|
|
22
|
+
if (!data || typeof data !== 'object') throw new Error('unexpected response');
|
|
23
|
+
const raw = data as Record<string, unknown>;
|
|
24
|
+
|
|
25
|
+
const result: LividOembed = {};
|
|
26
|
+
if (typeof raw.title === 'string' && raw.title.trim()) result.title = raw.title.trim();
|
|
27
|
+
if (typeof raw.thumbnail_url === 'string') {
|
|
28
|
+
try {
|
|
29
|
+
if (new URL(raw.thumbnail_url).protocol === 'https:') {
|
|
30
|
+
result.thumbnailUrl = raw.thumbnail_url;
|
|
31
|
+
result.thumbnailWidth = positiveInt(raw.thumbnail_width);
|
|
32
|
+
result.thumbnailHeight = positiveInt(raw.thumbnail_height);
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
// Not a URL: ignore the thumbnail.
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Look up a video's title and thumbnail through Livid's oEmbed endpoint.
|
|
43
|
+
* Returns null if the lookup fails. Each ID is fetched once per process.
|
|
44
|
+
*/
|
|
45
|
+
export function fetchOembed(id: string): Promise<LividOembed | null> {
|
|
46
|
+
const key = parseVideoId(id);
|
|
47
|
+
let pending = cache.get(key);
|
|
48
|
+
if (!pending) {
|
|
49
|
+
pending = request(key).catch((error: unknown) => {
|
|
50
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
51
|
+
console.warn(
|
|
52
|
+
`astro-livid: could not load oEmbed data for video "${key}" (${reason}). Falling back to a plain iframe embed.`,
|
|
53
|
+
);
|
|
54
|
+
return null;
|
|
55
|
+
});
|
|
56
|
+
cache.set(key, pending);
|
|
57
|
+
}
|
|
58
|
+
return pending;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Empty the oEmbed cache. For tests. */
|
|
62
|
+
export function clearOembedCache(): void {
|
|
63
|
+
cache.clear();
|
|
64
|
+
}
|
package/src/params.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Livid's advanced embedding parameters, as typed props.
|
|
2
|
+
// https://support.livid.com/article/46-advanced-embedding-parameters
|
|
3
|
+
//
|
|
4
|
+
// Props are camelCase. PARAM_NAMES maps each one to the snake_case name
|
|
5
|
+
// Livid's player expects in the embed URL.
|
|
6
|
+
|
|
7
|
+
export type LividQuality = '240p' | '360p' | '540p' | '720p' | '1080p' | '2k' | '4k';
|
|
8
|
+
|
|
9
|
+
export interface LividParams {
|
|
10
|
+
/** Show the AirPlay button (Safari only). Pro/Premium. Livid default: true. */
|
|
11
|
+
airplay?: boolean;
|
|
12
|
+
/** Start playing on load. Browsers usually require `muted` too. Livid default: false. */
|
|
13
|
+
autoplay?: boolean;
|
|
14
|
+
/** Background mode: no controls, looped, autoplaying and muted. Pro/Premium. Livid default: false. */
|
|
15
|
+
background?: boolean;
|
|
16
|
+
/** Show the closed captions button. Pro/Premium. Livid default: true. */
|
|
17
|
+
cc?: boolean;
|
|
18
|
+
/** Show the Chromecast button. Pro/Premium. Livid default: true. */
|
|
19
|
+
chromecast?: boolean;
|
|
20
|
+
/** Player accent color as a hex code, with or without the leading `#`. Pro/Premium. */
|
|
21
|
+
color?: string;
|
|
22
|
+
/** Up to four player colors as hex codes: an array, or a comma-separated string. Pro/Premium. */
|
|
23
|
+
colors?: string | string[];
|
|
24
|
+
/** Show the player controls. Pro/Premium. Livid default: true. */
|
|
25
|
+
controls?: boolean;
|
|
26
|
+
/** Show your custom logo. Pro/Premium. Livid default: the video's own setting. */
|
|
27
|
+
customLogo?: boolean;
|
|
28
|
+
/** Do not track: turns off analytics for this embed. Livid default: false. */
|
|
29
|
+
dnt?: boolean;
|
|
30
|
+
/** Show the fullscreen button. Pro/Premium. Livid default: true. */
|
|
31
|
+
fullscreen?: boolean;
|
|
32
|
+
/** Allow keyboard shortcuts. Livid default: true. */
|
|
33
|
+
keyboard?: boolean;
|
|
34
|
+
/** Show the Livid logo. Pro/Premium. Livid default: true. */
|
|
35
|
+
lividLogo?: boolean;
|
|
36
|
+
/** Loop the video. Livid default: false. */
|
|
37
|
+
loop?: boolean;
|
|
38
|
+
/** Start muted. Livid default: false. */
|
|
39
|
+
muted?: boolean;
|
|
40
|
+
/** Show the picture-in-picture button. Pro/Premium. Livid default: true. */
|
|
41
|
+
pip?: boolean;
|
|
42
|
+
/** Where the play button sits. Pro/Premium. Livid default: auto. */
|
|
43
|
+
playButtonPosition?: 'auto' | 'bottom' | 'center';
|
|
44
|
+
/** Play inline on mobile instead of going fullscreen. Livid default: true. */
|
|
45
|
+
playsinline?: boolean;
|
|
46
|
+
/** How much of the video to load before play. Livid default: none. */
|
|
47
|
+
preload?: 'auto' | 'metadata' | 'none';
|
|
48
|
+
/** Show the progress bar. Pro/Premium. Livid default: true. */
|
|
49
|
+
progressBar?: boolean;
|
|
50
|
+
/** Show the quality selector. Pro/Premium. Livid default: true. */
|
|
51
|
+
qualitySelector?: boolean;
|
|
52
|
+
/** Show the share button. Pro/Premium. Livid default: true. */
|
|
53
|
+
share?: boolean;
|
|
54
|
+
/** Show the playback speed control. Pro/Premium. Livid default: true. */
|
|
55
|
+
speed?: boolean;
|
|
56
|
+
/** Start position in seconds. Added to the URL as `#t=N`. */
|
|
57
|
+
t?: number;
|
|
58
|
+
/** Show the time display. Pro/Premium. Livid default: false. */
|
|
59
|
+
timeDisplay?: boolean;
|
|
60
|
+
/** Show the video's title in the player. Pro/Premium. Livid default: true. */
|
|
61
|
+
title?: boolean;
|
|
62
|
+
/** Transparent player background. Livid default: true. */
|
|
63
|
+
transparent?: boolean;
|
|
64
|
+
/** Show the volume control. Pro/Premium. Livid default: true. */
|
|
65
|
+
volume?: boolean;
|
|
66
|
+
/** Lowest quality the player may use. Pro/Premium. */
|
|
67
|
+
minQuality?: LividQuality;
|
|
68
|
+
/** Highest quality the player may use. Pro/Premium. */
|
|
69
|
+
maxQuality?: LividQuality;
|
|
70
|
+
/** Quality the player starts at. Pro/Premium. */
|
|
71
|
+
initialQuality?: LividQuality;
|
|
72
|
+
/** Fixed quality for the player. Pro/Premium. */
|
|
73
|
+
quality?: LividQuality;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Prop name to the query parameter Livid expects. `t` is not here: it goes in the URL hash. */
|
|
77
|
+
export const PARAM_NAMES = {
|
|
78
|
+
airplay: 'airplay',
|
|
79
|
+
autoplay: 'autoplay',
|
|
80
|
+
background: 'background',
|
|
81
|
+
cc: 'cc',
|
|
82
|
+
chromecast: 'chromecast',
|
|
83
|
+
color: 'color',
|
|
84
|
+
colors: 'colors',
|
|
85
|
+
controls: 'controls',
|
|
86
|
+
customLogo: 'custom_logo',
|
|
87
|
+
dnt: 'dnt',
|
|
88
|
+
fullscreen: 'fullscreen',
|
|
89
|
+
keyboard: 'keyboard',
|
|
90
|
+
lividLogo: 'livid_logo',
|
|
91
|
+
loop: 'loop',
|
|
92
|
+
muted: 'muted',
|
|
93
|
+
pip: 'pip',
|
|
94
|
+
playButtonPosition: 'play_button_position',
|
|
95
|
+
playsinline: 'playsinline',
|
|
96
|
+
preload: 'preload',
|
|
97
|
+
progressBar: 'progress_bar',
|
|
98
|
+
qualitySelector: 'quality_selector',
|
|
99
|
+
share: 'share',
|
|
100
|
+
speed: 'speed',
|
|
101
|
+
timeDisplay: 'time_display',
|
|
102
|
+
title: 'title',
|
|
103
|
+
transparent: 'transparent',
|
|
104
|
+
volume: 'volume',
|
|
105
|
+
minQuality: 'min_quality',
|
|
106
|
+
maxQuality: 'max_quality',
|
|
107
|
+
initialQuality: 'initial_quality',
|
|
108
|
+
quality: 'quality',
|
|
109
|
+
} as const satisfies Record<Exclude<keyof LividParams, 't'>, string>;
|
package/src/url.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PARAM_NAMES, type LividParams } from './params';
|
|
2
|
+
|
|
3
|
+
const ID_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
4
|
+
const LIVID_HOSTS = new Set(['livid.com', 'www.livid.com']);
|
|
5
|
+
const MAX_COLORS = 4;
|
|
6
|
+
|
|
7
|
+
/** Extract a Livid video ID from a bare ID or a livid.com watch/embed URL. */
|
|
8
|
+
export function parseVideoId(input: string): string {
|
|
9
|
+
const value = String(input ?? '').trim();
|
|
10
|
+
if (!value) throw new Error('astro-livid: a video ID or Livid URL is required.');
|
|
11
|
+
|
|
12
|
+
let id = value;
|
|
13
|
+
if (/^https?:\/\//i.test(value)) {
|
|
14
|
+
let url: URL;
|
|
15
|
+
try {
|
|
16
|
+
url = new URL(value);
|
|
17
|
+
} catch {
|
|
18
|
+
throw new Error(`astro-livid: "${value}" is not a valid URL.`);
|
|
19
|
+
}
|
|
20
|
+
if (!LIVID_HOSTS.has(url.hostname.toLowerCase())) {
|
|
21
|
+
throw new Error(`astro-livid: "${value}" is not a livid.com URL.`);
|
|
22
|
+
}
|
|
23
|
+
const match = url.pathname.match(/^\/(?:watch|embed)\/([^/]+)\/?$/);
|
|
24
|
+
if (!match) {
|
|
25
|
+
throw new Error(`astro-livid: "${value}" is not a Livid /watch/ or /embed/ URL.`);
|
|
26
|
+
}
|
|
27
|
+
id = match[1];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!ID_PATTERN.test(id)) {
|
|
31
|
+
throw new Error(`astro-livid: "${id}" is not a valid Livid video ID.`);
|
|
32
|
+
}
|
|
33
|
+
return id;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseColors(colors: string | string[]): string[] {
|
|
37
|
+
const list = (Array.isArray(colors) ? colors : colors.split(','))
|
|
38
|
+
.map((c) => c.trim().replace(/^#/, ''))
|
|
39
|
+
.filter(Boolean);
|
|
40
|
+
if (list.length > MAX_COLORS) {
|
|
41
|
+
throw new Error(`astro-livid: colors accepts at most ${MAX_COLORS} values, got ${list.length}.`);
|
|
42
|
+
}
|
|
43
|
+
return list;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Build the https://livid.com/embed/ID URL, with params as query string and `t` as a #t=N hash. */
|
|
47
|
+
export function buildEmbedUrl(id: string, params: LividParams = {}): string {
|
|
48
|
+
const base = `https://livid.com/embed/${parseVideoId(id)}`;
|
|
49
|
+
const query: string[] = [];
|
|
50
|
+
|
|
51
|
+
for (const [prop, name] of Object.entries(PARAM_NAMES)) {
|
|
52
|
+
const value = (params as Record<string, unknown>)[prop];
|
|
53
|
+
if (value === undefined || value === null) continue;
|
|
54
|
+
|
|
55
|
+
let encoded: string;
|
|
56
|
+
if (prop === 'colors') {
|
|
57
|
+
const list = parseColors(value as string | string[]);
|
|
58
|
+
if (!list.length) continue;
|
|
59
|
+
// Commas stay literal; Livid expects colors=aaa,bbb.
|
|
60
|
+
encoded = list.map(encodeURIComponent).join(',');
|
|
61
|
+
} else if (prop === 'color') {
|
|
62
|
+
encoded = encodeURIComponent(String(value).trim().replace(/^#/, ''));
|
|
63
|
+
} else {
|
|
64
|
+
encoded = encodeURIComponent(String(value));
|
|
65
|
+
}
|
|
66
|
+
query.push(`${name}=${encoded}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let url = query.length ? `${base}?${query.join('&')}` : base;
|
|
70
|
+
|
|
71
|
+
const t = params.t;
|
|
72
|
+
if (t !== undefined && t !== null) {
|
|
73
|
+
if (typeof t !== 'number' || !Number.isFinite(t) || t < 0) {
|
|
74
|
+
throw new Error(`astro-livid: t must be a finite number of seconds, 0 or more (got ${String(t)}).`);
|
|
75
|
+
}
|
|
76
|
+
// Floors to whole seconds; anything under 1 second is treated as no start time.
|
|
77
|
+
if (Math.floor(t) > 0) url += `#t=${Math.floor(t)}`;
|
|
78
|
+
}
|
|
79
|
+
return url;
|
|
80
|
+
}
|