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

Added archive.org embed player

parent 1800e461
import React from 'react';
/**
* Regular expressions for detecting and validating provider URLs
* @type {{htmlReplacement: RegExp, main: RegExp, sanitize: RegExp}}
*/
const regex = {
sanitize: /^https:\/\/archive\.org\/embed\/(.*)/i,
main: /^https:\/\/archive\.org\/details\/(.*)/i,
};
export default regex;
/**
* Configuration for HTML iframe's `sandbox` attribute
* @type {useSandbox: boolean, sandboxAttributes: string[]}
*/
export const sandboxConfig = {
useSandbox: false,
sandboxAttributes: [],
};
/**
* Check if the iframe code in the post editor is to an allowed URL
* <iframe src="https://archive.org/embed/namaz-nasil-kilinir" width="640" height="480" frameborder="0" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe>
* <iframe src="https://archive.org/embed/geometry_dash_1.9" width="500" height="140" frameborder="0" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe>
* @param url
* @returns {boolean|*}
*/
export function validateIframeUrl(url) {
const match = url.match(regex.sanitize);
if (!match || match.length !== 2) {
return false;
}
return 'https://archive.org/embed/' + match[1];
}
/**
* Rewrites the embedded URL to a normalized format
* @param url
* @returns {string|boolean}
*/
export function normalizeEmbedUrl(url) {
const match = url.match(regex.sanitize);
if (match && match.length >= 2) {
return `https://archive.org/embed/${match[1]}`;
}
return false;
}
/**
* Extract the content ID and other metadata from the URL
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractMetadata(data) {
if (!data) return null;
const m = data.match(regex.main);
if (!m || m.length < 2) return null;
const startTime = m.input.match(/t=(\d+)s?/);
return {
id: m[1],
url: m[0],
startTime: startTime ? startTime[1] : 0,
canonical: `https://archive.org/embed/${m[1]}`,
};
}
/**
* 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;
const archiveorg = extractMetadata(data);
if (!archiveorg) return child;
child.data = data.replace(
archiveorg.url,
`~~~ embed:${archiveorg.id} archiveorg ~~~`
);
if (links) {
links.add(archiveorg.canonical);
}
if (links) links.add(archiveorg.canonical);
} catch (error) {
console.log(error);
}
return child;
}
/**
* Generates the Markdown/HTML code to override the detected URL with an iFrame
* @param idx
* @param id
* @param width
* @param height
* @param startTime
* @returns {*}
*/
export function genIframeMd(idx, id, width, height) {
const url = `https://archive.org/embed/${id}`;
let sandbox = sandboxConfig.useSandbox;
if (sandbox) {
if (
Object.prototype.hasOwnProperty.call(
sandboxConfig,
'sandboxAttributes'
)
) {
sandbox = sandboxConfig.sandboxAttributes.join(' ');
}
}
const iframeProps = {
src: url,
width,
height,
frameBorder: '0',
webkitallowfullscreen: 'webkitallowfullscreen',
mozallowfullscreen: 'mozallowfullscreen',
allowFullScreen: 'allowFullScreen',
};
if (sandbox) {
iframeProps.sandbox = sandbox;
}
return (
<div key={`archiveorg-${id}-${idx}`} className="videoWrapper">
<iframe
title="Archive.org embedded player"
// eslint-disable-next-line react/jsx-props-no-spreading
{...iframeProps}
/>
</div>
);
}
import _ from 'lodash';
import * as archiveorg from 'app/components/elements/EmbeddedPlayers/archiveorg';
import * as dapplr from 'app/components/elements/EmbeddedPlayers/dapplr';
import * as dtube from 'app/components/elements/EmbeddedPlayers/dtube';
import * as mixcloud from 'app/components/elements/EmbeddedPlayers/mixcloud';
......@@ -11,6 +12,7 @@ import * as vimeo from 'app/components/elements/EmbeddedPlayers/vimeo';
import * as youtube from 'app/components/elements/EmbeddedPlayers/youtube';
const supportedProviders = {
archiveorg,
dapplr,
dtube,
mixcloud,
......
......@@ -7,6 +7,7 @@ import threespeakRegex from 'app/components/elements/EmbeddedPlayers/threespeak'
import twitterRegex from 'app/components/elements/EmbeddedPlayers/twitter';
import spotifyRegex from 'app/components/elements/EmbeddedPlayers/spotify';
import mixcloudRegex from 'app/components/elements/EmbeddedPlayers/mixcloud';
import archiveorg from 'app/components/elements/EmbeddedPlayers/archiveorg';
describe('Links', () => {
it('all', () => {
......@@ -370,6 +371,13 @@ describe('Performance', () => {
'https://www.mixcloud.com/widget/iframe/?hide_cover=1&feed=%2FMagneticMagazine%2Fambient-meditations-vol-21-anane%2F'
);
});
it('archiveorg', () => {
match(archiveorg.main, 'https://archive.org/details/geometry_dash_1.9');
match(
archiveorg.sanitize,
'https://archive.org/embed/geometry_dash_1.9'
);
});
});
const match = (...args) => compare(true, ...args);
......
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