Commit f8559dc9 authored by Quoc Huy Nguyen Dinh's avatar Quoc Huy Nguyen Dinh
Browse files

Added comments

parent 3152b7bd
import React from 'react';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^https:\/\/emb.d.tube\/\#\!\/([a-zA-Z0-9\-\.\/]+)$/,
main: /https:\/\/(?:emb\.)?(?:d.tube\/\#\!\/(?:v\/)?)([a-zA-Z0-9\-\.\/]*)/,
......@@ -8,6 +12,14 @@ const regex = {
export default regex;
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param threespeakId
* @param w
* @param h
* @returns {*}
*/
export function genIframeMd(idx, dtubeId, w, h) {
const url = `https://emb.d.tube/#!/${dtubeId}`;
return (
......@@ -25,7 +37,12 @@ export function genIframeMd(idx, dtubeId, w, h) {
);
}
// <iframe title="DTube embedded player" src="https://emb.d.tube/#!/lemwong/QmQqxBCkoVusMRwP6D9oBMRQdASFzABdKQxE7xLysfmsR6" width="640" height="360" frameborder="0" allowfullscreen=""></iframe>
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe title="DTube embedded player" src="https://emb.d.tube/#!/lemwong/QmQqxBCkoVusMRwP6D9oBMRQdASFzABdKQxE7xLysfmsR6" width="640" height="360" frameborder="0" allowfullscreen=""></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......@@ -36,6 +53,11 @@ export function validateIframeUrl(url) {
return false;
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.contentId);
......@@ -46,6 +68,11 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
if (!data) return null;
......@@ -59,6 +86,12 @@ function extractContentId(data) {
};
}
/**
* Replaces the URL with a custom Markdown for embedded players
* @param child
* @param links
* @returns {*}
*/
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
......
......@@ -83,6 +83,11 @@ const supportedProviders = [
export default supportedProviders;
/**
* Allow iFrame in the Markdown if the source URL is allowed
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
for (let pi = 0; pi < supportedProviders.length; pi += 1) {
const provider = supportedProviders[pi];
......@@ -98,6 +103,11 @@ export function validateIframeUrl(url) {
return false;
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {boolean|*}
*/
export function normalizeEmbedUrl(url) {
for (let pi = 0; pi < supportedProviders.length; pi += 1) {
const provider = supportedProviders[pi];
......@@ -115,6 +125,13 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Replaces the URL with a custom Markdown for embedded players
* @param child
* @param links
* @param images
* @returns {*}
*/
export function embedNode(child, links, images) {
for (let pi = 0; pi < supportedProviders.length; pi += 1) {
const provider = supportedProviders[pi];
......@@ -127,6 +144,11 @@ export function embedNode(child, links, images) {
return child;
}
/**
* Returns the provider config by ID
* @param id
* @returns {null|{normalizeEmbedUrlFn, validateIframeUrlFn, id: string, genIframeMdFn, embedNodeFn}|{normalizeEmbedUrlFn, validateIframeUrlFn, id: string, genIframeMdFn, embedNodeFn}|{normalizeEmbedUrlFn: null, validateIframeUrlFn, id: string, genIframeMdFn: null, embedNodeFn: null}|{normalizeEmbedUrlFn, validateIframeUrlFn, id: string, genIframeMdFn, embedNodeFn}|{normalizeEmbedUrlFn, validateIframeUrlFn, id: string, genIframeMdFn, embedNodeFn}}
*/
function getProviderById(id) {
for (let pi = 0; pi < supportedProviders.length; pi += 1) {
const provider = supportedProviders[pi];
......@@ -139,12 +161,23 @@ function getProviderById(id) {
return null;
}
/**
* Returns all providers IDs
* @returns {(string)[]}
*/
function getProviderIds() {
return supportedProviders.map(o => {
return o.id;
});
}
/**
* Replaces ~~~ embed: Markdown code to an iframe MD
* @param section
* @param idx
* @param large
* @returns {null|{markdown: null, section: string}}
*/
export function generateMd(section, idx, large) {
let markdown = null;
const supportedProvidersIds = getProviderIds();
......@@ -186,6 +219,11 @@ export function generateMd(section, idx, large) {
return null;
}
/**
* Pre-process HTML codes from the Markdown before it gets transformed
* @param html
* @returns {*}
*/
export function preprocessHtml(html) {
html = preprocess3SpeakHtml(html);
return html;
......
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^https:\/\/w.soundcloud.com\/player\/.*?url=(.+?)&.*/i,
};
export default regex;
// <iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/257659076&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/257659076&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......
import React from 'react';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^https:\/\/3speak.online\/embed\?v=([A-Za-z0-9\_\-\/]+)(&.*)?$/,
main: /(?:https?:\/\/(?:(?:3speak.online\/watch\?v=)|(?:3speak.online\/embed\?v=)))([A-Za-z0-9\_\-\/]+)(&.*)?/i,
......@@ -8,6 +12,14 @@ const regex = {
export default regex;
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param threespeakId
* @param w
* @param h
* @returns {*}
*/
export function genIframeMd(idx, threespeakId, w, h) {
const url = `https://3speak.online/embed?v=${threespeakId}`;
return (
......@@ -25,6 +37,11 @@ export function genIframeMd(idx, threespeakId, w, h) {
);
}
/**
* Check if the iframe code in the post editor is to an allowed URL
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......@@ -35,6 +52,11 @@ export function validateIframeUrl(url) {
return false;
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.contentId);
......@@ -45,6 +67,11 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
if (!data) return null;
......@@ -62,6 +89,12 @@ function extractContentId(data) {
};
}
/**
* Replaces the URL with a custom Markdown for embedded players
* @param child
* @param links
* @returns {*}
*/
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
......@@ -81,6 +114,11 @@ export function embedNode(child, links /*images*/) {
return child;
}
/**
* Pre-process HTML codes from the Markdown before it gets transformed
* @param child
* @returns {string}
*/
export function preprocessHtml(child) {
try {
if (typeof child === 'string') {
......
import React from 'react';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^(https?:)?\/\/player.twitch.tv\/.*/i,
main: /https?:\/\/(?:www.)?twitch.tv\/(?:(videos)\/)?([a-zA-Z0-9][\w]{3,24})/i,
......@@ -7,7 +11,12 @@ const regex = {
export default regex;
// <iframe src="https://player.twitch.tv/?channel=tfue" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe src="https://player.twitch.tv/?channel=tfue" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......@@ -18,6 +27,11 @@ export function validateIframeUrl(url) {
return false;
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.main);
......@@ -34,6 +48,11 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
if (!data) return null;
......@@ -70,6 +89,14 @@ export function embedNode(child, links /*images*/) {
return child;
}
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param threespeakId
* @param w
* @param h
* @returns {*}
*/
export function genIframeMd(idx, id, w, h) {
const url = `https://player.twitch.tv/${id}`;
return (
......
import React from 'react';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^(https?:)?\/\/player.vimeo.com\/video\/([0-9]*)/i,
main: /https?:\/\/(?:vimeo.com\/|player.vimeo.com\/video\/)([0-9]+)\/?(#t=((\d+)s?))?\/?/,
......@@ -8,7 +12,12 @@ const regex = {
export default regex;
// <iframe src="https://player.vimeo.com/video/179213493" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe src="https://player.vimeo.com/video/179213493" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......@@ -19,6 +28,11 @@ export function validateIframeUrl(url) {
return 'https://player.vimeo.com/video/' + match[2];
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.contentId);
......@@ -29,6 +43,11 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
if (!data) return null;
const m = data.match(regex.main);
......@@ -45,6 +64,12 @@ function extractContentId(data) {
};
}
/**
* Replaces the URL with a custom Markdown for embedded players
* @param child
* @param links
* @returns {*}
*/
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
......@@ -72,6 +97,14 @@ export function embedNode(child, links /*images*/) {
return child;
}
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param threespeakId
* @param w
* @param h
* @returns {*}
*/
export function genIframeMd(idx, id, w, h, startTime) {
const url = `https://player.vimeo.com/video/${id}#t=${startTime}s`;
return (
......
import React from 'react';
import YoutubePreview from 'app/components/elements/YoutubePreview';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^(https?:)?\/\/www.youtube.com\/embed\/.*/i,
//main: new RegExp(urlSet({ domain: '(?:(?:.*.)?youtube.com|youtu.be)' }), flags),
......@@ -10,7 +14,12 @@ const regex = {
export default regex;
// <iframe width="560" height="315" src="https://www.youtube.com/embed/KOnk7Nbqkhs" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe width="560" height="315" src="https://www.youtube.com/embed/KOnk7Nbqkhs" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
......@@ -22,6 +31,11 @@ export function validateIframeUrl(url) {
return false;
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.contentId);
......@@ -32,6 +46,11 @@ export function normalizeEmbedUrl(url) {
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
export function extractContentId(data) {
if (!data) return null;
......@@ -55,6 +74,12 @@ export function extractContentId(data) {
};
}
/**
* Replaces the URL with a custom Markdown for embedded players
* @param child
* @param links
* @returns {*}
*/
export function embedNode(child, links, images) {
try {
const yt = extractContentId(child.data);
......@@ -82,6 +107,14 @@ export function embedNode(child, links, images) {
return child;
}
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param threespeakId
* @param w
* @param h
* @returns {*}
*/
export function genIframeMd(idx, id, w, h, startTime) {
return (
<YoutubePreview
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment