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

Closes #45

- rename extractContentId() to extractMetadata()
- some eslint compliance fixes
- added missing 'canonical' property to the object returned by extractMetadata()
- added thumbnail extraction edge case from ThreeSpeak generated post body
parent dbe4ab93
......@@ -73,7 +73,7 @@ export function normalizeEmbedUrl(url) {
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
function extractMetadata(data) {
if (!data) return null;
const m = data.match(regex.main);
......@@ -94,8 +94,8 @@ function extractContentId(data) {
*/
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
const dtube = extractContentId(data);
const { data } = child;
const dtube = extractMetadata(data);
if (!dtube) return child;
child.data = data.replace(dtube.url, `~~~ embed:${dtube.id} dtube ~~~`);
......
......@@ -8,6 +8,7 @@ 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,
htmlReplacement: /<a href="(https?:\/\/3speak.online\/watch\?v=([A-Za-z0-9\_\-\/]+))".*<img.*?><\/a>/i,
embedShorthand: /~~~ embed:(.*?)\/(.*?) threespeak ~~~/,
};
export default regex;
......@@ -72,7 +73,7 @@ export function normalizeEmbedUrl(url) {
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
export function extractMetadata(data) {
if (!data) return null;
const match = data.match(regex.main);
......@@ -85,6 +86,7 @@ function extractContentId(data) {
id,
fullId,
url,
canonical: url,
thumbnail: `https://img.3speakcontent.online/${id}/post.png`,
};
}
......@@ -95,18 +97,36 @@ function extractContentId(data) {
* @param links
* @returns {*}
*/
export function embedNode(child, links /*images*/) {
export function embedNode(child, links, images) {
try {
const data = child.data;
const threespeak = extractContentId(data);
if (!threespeak) return child;
const { data } = child;
const threespeak = extractMetadata(data);
child.data = data.replace(
threespeak.url,
`~~~ embed:${threespeak.id} threespeak ~~~`
);
if (threespeak) {
child.data = data.replace(
threespeak.url,
`~~~ embed:${threespeak.id} threespeak ~~~`
);
if (links) links.add(threespeak.canonical);
if (links) {
links.add(threespeak.canonical);
}
if (images) {
images.add(threespeak.thumbnail);
}
} else {
// Because we are processing 3speak embed player with the preprocessHtml() method below
// extractMetadata won't be able to extract the thumbnail from the shorthand.
// So we are handling thumbnail URL extraction differently.
const match = data.match(regex.embedShorthand);
if (match && images) {
const imageUrl = `https://img.3speakcontent.online/${
match[2]
}/post.png`;
images.add(imageUrl);
}
}
} catch (error) {
console.log(error);
}
......@@ -124,7 +144,7 @@ export function preprocessHtml(child) {
if (typeof child === 'string') {
// If typeof child is a string, this means we are trying to process the HTML
// to replace the image/anchor tag created by 3Speak dApp
const threespeak = extractContentId(child);
const threespeak = extractMetadata(child);
if (threespeak) {
child = child.replace(
regex.htmlReplacement,
......
......@@ -53,7 +53,7 @@ export function normalizeEmbedUrl(url) {
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
function extractMetadata(data) {
if (!data) return null;
const m = data.match(regex.main);
......@@ -72,8 +72,8 @@ function extractContentId(data) {
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
const twitch = extractContentId(data);
const { data } = child;
const twitch = extractMetadata(data);
if (!twitch) return child;
child.data = data.replace(
......
......@@ -48,7 +48,7 @@ export function normalizeEmbedUrl(url) {
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
function extractContentId(data) {
function extractMetadata(data) {
if (!data) return null;
const m = data.match(regex.main);
if (!m || m.length < 2) return null;
......@@ -72,8 +72,8 @@ function extractContentId(data) {
*/
export function embedNode(child, links /*images*/) {
try {
const data = child.data;
const vimeo = extractContentId(data);
const { data } = child;
const vimeo = extractMetadata(data);
if (!vimeo) return child;
const vimeoRegex = new RegExp(`${vimeo.url}(#t=${vimeo.startTime}s?)?`);
......
......@@ -51,7 +51,7 @@ export function normalizeEmbedUrl(url) {
* @param data
* @returns {null|{id: *, canonical: string, url: *}}
*/
export function extractContentId(data) {
export function extractMetadata(data) {
if (!data) return null;
const m1 = data.match(regex.main);
......@@ -69,6 +69,7 @@ export function extractContentId(data) {
return {
id,
url,
canonical: url,
startTime: startTime ? startTime[1] : 0,
thumbnail: 'https://img.youtube.com/vi/' + id + '/0.jpg',
};
......@@ -82,7 +83,7 @@ export function extractContentId(data) {
*/
export function embedNode(child, links, images) {
try {
const yt = extractContentId(child.data);
const yt = extractMetadata(child.data);
if (!yt) return child;
......
import remarkableStripper from 'app/utils/RemarkableStripper';
import links from 'app/utils/Links';
import sanitize from 'sanitize-html';
import { htmlDecode } from 'app/utils/Html';
import HtmlReady from 'shared/HtmlReady';
......@@ -37,7 +36,9 @@ export function extractImageLink(json_metadata, body = null) {
'(html comment removed: $1)'
)
);
console.log('htmlText', htmlText);
rtags = HtmlReady(htmlText, { mutate: false });
console.log('rtags', rtags);
}
[image_link] = Array.from(rtags.images);
......
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