forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Video.astro
33 lines (29 loc) · 1.01 KB
/
Video.astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
---
import { parse } from 'node:path';
import { getLanguageFromURL } from '../util';
import languages from '../i18n/languages';
export interface Props {
src: string;
type: string;
}
const { src, type } = Astro.props as Props;
const url = new URL(Astro.request.url);
const lang = getLanguageFromURL(url.pathname);
const filename = parse(src).name;
const allCaptions = import.meta.glob("../../public/captions/**/*.vtt", { as:'raw' });
const fileCaptions = Object.keys(allCaptions)
.map((caption) => {
const dir = '/captions/';
const path = caption.substring(caption.indexOf(dir));
const lang = path.substring(dir.length, path.indexOf(filename) - 1);
return { path, lang };
})
.filter(({ path }) => path.includes(filename));
const defaultLang = fileCaptions.find((caption) => caption.lang == lang) ? lang : 'en';
---
<video controls>
<source src={src} type={type}>
{fileCaptions.map(({lang, path}) => (
<track label={languages[lang]} src={path} kind="captions" srclang={lang} default={lang == defaultLang}>
))}
</video>