From 6a5033aa88256d70b3615c0f07ec637f3ea2e3ed Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 2 Sep 2024 22:23:55 +0300 Subject: [PATCH] feat: add yandex video, vk video iframes --- src/transform/plugins/video/const.ts | 4 ++++ src/transform/plugins/video/index.ts | 16 +++++++--------- src/transform/plugins/video/parsers.ts | 18 ++++++++++++++++++ src/transform/plugins/video/types.ts | 2 ++ src/transform/plugins/video/utils.ts | 15 +++++++++------ 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/transform/plugins/video/const.ts b/src/transform/plugins/video/const.ts index c7cf0fd1..bfc8e12c 100644 --- a/src/transform/plugins/video/const.ts +++ b/src/transform/plugins/video/const.ts @@ -9,6 +9,8 @@ export enum VideoService { Vine = 'vine', Prezi = 'prezi', Osf = 'osf', + YandexVideo = 'yandexVideo', + VkVideo = 'vkVideo', } export const defaults: VideoFullOptions = { @@ -18,4 +20,6 @@ export const defaults: VideoFullOptions = { vine: {width: 600, height: 600, embed: 'simple'}, prezi: {width: 550, height: 400}, osf: {width: '100%', height: '100%'}, + yandexVideo: {width: 640, height: 390}, + vkVideo: {width: 640, height: 390}, }; diff --git a/src/transform/plugins/video/index.ts b/src/transform/plugins/video/index.ts index ae79dee0..de67187f 100644 --- a/src/transform/plugins/video/index.ts +++ b/src/transform/plugins/video/index.ts @@ -3,6 +3,8 @@ // Process @[vine](vineVideoID) // Process @[prezi](preziID) // Process @[osf](guid) +// Process @[yandexVideo](videoID) +// Process @[vkVideo](videoID) import type MarkdownIt from 'markdown-it'; // eslint-disable-next-line no-duplicate-imports @@ -69,15 +71,11 @@ function tokenizeVideo(md: MarkdownIt, options: VideoFullOptions): Renderer.Rend return videoID === '' ? '' - : '
'; + : `
`; }; } diff --git a/src/transform/plugins/video/parsers.ts b/src/transform/plugins/video/parsers.ts index b187d3b5..50c3adf1 100644 --- a/src/transform/plugins/video/parsers.ts +++ b/src/transform/plugins/video/parsers.ts @@ -33,6 +33,18 @@ export function mfrParser(url: string) { return match ? match[1] : url; } +const yandexVideoRegex = /^https:\/\/runtime.video.cloud.yandex.net\/player\/video\/([a-zA-Z0-9]+)/; +export function yandexVideoParser(url: string) { + const match = url.match(yandexVideoRegex); + return match ? match[1] : url; +} + +const vkVideoRegex = /^https:\/\/vk.com\/video_ext\.php?(oid=[-\d]+&id=[-\d]+)/; +export function vkVideoParser(url: string) { + const match = url.match(vkVideoRegex); + return match ? match[1] : url; +} + export function parseVideoUrl(service: string, url: string): string | false { let videoID = ''; @@ -52,6 +64,12 @@ export function parseVideoUrl(service: string, url: string): string | false { case VideoService.Osf: videoID = mfrParser(url); break; + case VideoService.YandexVideo: + videoID = yandexVideoParser(url); + break; + case VideoService.VkVideo: + videoID = vkVideoParser(url); + break; default: return false; } diff --git a/src/transform/plugins/video/types.ts b/src/transform/plugins/video/types.ts index 6588e901..5de58534 100644 --- a/src/transform/plugins/video/types.ts +++ b/src/transform/plugins/video/types.ts @@ -12,6 +12,8 @@ export type VideoServicesOptions = { [VideoService.Vine]: {width: number; height: number; embed: 'simple' | string}; [VideoService.Prezi]: {width: number; height: number}; [VideoService.Osf]: {width: string; height: string}; + [VideoService.YandexVideo]: {width: number; height: number}; + [VideoService.VkVideo]: {width: number; height: number}; }; export type VideoFullOptions = VideoServicesOptions & { diff --git a/src/transform/plugins/video/utils.ts b/src/transform/plugins/video/utils.ts index 38be315b..d25b8432 100644 --- a/src/transform/plugins/video/utils.ts +++ b/src/transform/plugins/video/utils.ts @@ -3,21 +3,24 @@ import type {VideoUrlFn} from './types'; export const videoUrl: VideoUrlFn = (service, videoID, options) => { switch (service) { case 'youtube': - return 'https://www.youtube.com/embed/' + videoID; + return `https://www.youtube.com/embed/${videoID}`; case 'vimeo': - return 'https://player.vimeo.com/video/' + videoID; + return `https://player.vimeo.com/video/${videoID}`; case 'vine': - return 'https://vine.co/v/' + videoID + '/embed/' + options.vine.embed; + return `https://vine.co/v/${videoID}/embed/${options.vine.embed}`; case 'prezi': return ( - 'https://prezi.com/embed/' + - videoID + + `https://prezi.com/embed/${videoID}` + '/?bgcolor=ffffff&lock_to_path=0&autoplay=0&autohide_ctrls=0&' + 'landing_data=bHVZZmNaNDBIWnNjdEVENDRhZDFNZGNIUE43MHdLNWpsdFJLb2ZHanI5N1lQVHkxSHFxazZ0UUNCRHloSXZROHh3PT0&' + 'landing_sign=1kD6c0N6aYpMUS0wxnQjxzSqZlEB8qNFdxtdjYhwSuI' ); case 'osf': - return 'https://mfr.osf.io/render?url=https://osf.io/' + videoID + '/?action=download'; + return `https://mfr.osf.io/render?url=https://osf.io/${videoID}/?action=download`; + case 'yandexVideo': + return `https://runtime.video.cloud.yandex.net/player/${videoID}`; + case 'vkVideo': + return `https://vk.com/video_ext.php?${videoID}`; default: return service; }