@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
105 lines • 3.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseVideoUrl = exports.rutubeParser = exports.vkParser = exports.yandexParser = exports.mfrParser = exports.preziParser = exports.vineParser = exports.vimeoParser = exports.youtubeParser = void 0;
const ytRegex = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
function youtubeParser(url) {
const match = url.match(ytRegex);
return match && match[7].length === 11 ? match[7] : url;
}
exports.youtubeParser = youtubeParser;
const vimeoRegex = /https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/;
function vimeoParser(url) {
const match = url.match(vimeoRegex);
return match && typeof match[3] === 'string' ? match[3] : url;
}
exports.vimeoParser = vimeoParser;
const vineRegex = /^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13}).*/;
function vineParser(url) {
const match = url.match(vineRegex);
return match && match[1].length === 11 ? match[1] : url;
}
exports.vineParser = vineParser;
const preziRegex = /^https:\/\/prezi.com\/(.[^/]+)/;
function preziParser(url) {
const match = url.match(preziRegex);
return match ? match[1] : url;
}
exports.preziParser = preziParser;
// TODO: Write regex for staging and local servers.
const mfrRegex = /^http(?:s?):\/\/(?:www\.)?mfr\.osf\.io\/render\?url=http(?:s?):\/\/osf\.io\/([a-zA-Z0-9]{1,5})\/\?action=download/;
function mfrParser(url) {
const match = url.match(mfrRegex);
return match ? match[1] : url;
}
exports.mfrParser = mfrParser;
const yandexRegex = /^https:\/\/runtime.video.cloud.yandex.net\/player\/video\/([a-zA-Z0-9]+)/;
function yandexParser(url) {
const match = url.match(yandexRegex);
return match ? match[1] : url;
}
exports.yandexParser = yandexParser;
const vkRegex = /^https:\/\/vk\.com\/video_ext\.php\?(oid=[-\d]+&id=[-\d]+)/;
function vkParser(url) {
const match = url.match(vkRegex);
return match ? match[1] : url;
}
exports.vkParser = vkParser;
const rutubeRegex = /^https:\/\/rutube\.ru\/video\/([a-zA-Z0-9]+)\/?/;
function rutubeParser(url) {
const match = url.match(rutubeRegex);
return match ? match[1] : url;
}
exports.rutubeParser = rutubeParser;
const urlParser = (url) => url;
const supportedServices = {
osf: {
extract: mfrParser,
},
prezi: {
extract: preziParser,
},
vimeo: {
extract: vimeoParser,
},
vine: {
extract: vineParser,
},
yandex: {
extract: yandexParser,
},
youtube: {
extract: youtubeParser,
},
vk: {
extract: vkParser,
csp: {
'frame-src': ['https://vk.com/', 'https://login.vk.com/'],
},
},
rutube: {
extract: rutubeParser,
csp: {
'frame-src': ['https://rutube.ru/play/embed/'],
},
},
url: {
extract: urlParser,
},
};
function parseVideoUrl(service, url) {
let videoID = '';
const normalizedService = service.toLowerCase();
const parsed = supportedServices[normalizedService];
if (!parsed) {
return false;
}
const { extract, csp } = parsed;
videoID = extract(url);
// If the videoID field is empty, regex currently make it the close parenthesis.
if (videoID === ')') {
videoID = '';
}
return [videoID, csp];
}
exports.parseVideoUrl = parseVideoUrl;
//# sourceMappingURL=parsers.js.map
;