Merge pull request #737 from esteemapp/bugfix/621

fixed youtube link handle
This commit is contained in:
uğur erdal 2019-04-07 13:58:08 +03:00 committed by GitHub
commit f027bf9cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,7 +24,7 @@ const imgTagRegex = /(<img[^>]*>)/g;
const iframeRegex = /(?:<iframe[^>]*)(?:(?:\/>)|(?:>.*?<\/iframe>))/g; const iframeRegex = /(?:<iframe[^>]*)(?:(?:\/>)|(?:>.*?<\/iframe>))/g;
const hTagRegex = /(<h([1-6])>([^<]*)<\/h([1-6])>)/g; const hTagRegex = /(<h([1-6])>([^<]*)<\/h([1-6])>)/g;
export const markDown2Html = (input) => { export const markDown2Html = input => {
if (!input) { if (!input) {
return ''; return '';
} }
@ -95,177 +95,194 @@ export const markDown2Html = (input) => {
return output; return output;
}; };
const replaceAuthorNames = input => input.replace(authorNameRegex, (match, preceeding1, preceeding2, user) => { const replaceAuthorNames = input =>
const userLower = user.toLowerCase(); input.replace(authorNameRegex, (match, preceeding1, preceeding2, user) => {
const preceedings = (preceeding1 || '') + (preceeding2 || ''); const userLower = user.toLowerCase();
return `${preceedings}<a class="markdown-author-link" href="${userLower}" data-author="${userLower}"> @${user}</a>`; const preceedings = (preceeding1 || '') + (preceeding2 || '');
}); return `${preceedings}<a class="markdown-author-link" href="${userLower}" data-author="${userLower}"> @${user}</a>`;
});
const replaceTags = input => input.replace(tagsRegex, (tag) => { const replaceTags = input =>
if (/#[\d]+$/.test(tag)) return tag; input.replace(tagsRegex, tag => {
const preceding = /^\s|>/.test(tag) ? tag[0] : ''; if (/#[\d]+$/.test(tag)) return tag;
tag = tag.replace('>', ''); const preceding = /^\s|>/.test(tag) ? tag[0] : '';
const tag2 = tag.trim().substring(1); tag = tag.replace('>', '');
const tagLower = tag2.toLowerCase(); const tag2 = tag.trim().substring(1);
return `${preceding}<a class="markdown-tag-link" href="${tagLower}" data-tag="${tagLower}">${tag.trim()}</a>`; const tagLower = tag2.toLowerCase();
}); return `${preceding}<a class="markdown-tag-link" href="${tagLower}" data-tag="${tagLower}">${tag.trim()}</a>`;
});
const handleATag = input => input.replace(aTagRegex, (link) => { const handleATag = input =>
if (dTubeRegex.test(link)) { input.replace(aTagRegex, link => {
const dTubeMatch = link.match(dTubeRegex)[0]; if (dTubeRegex.test(link)) {
const execLink = dTubeRegex.exec(dTubeMatch); const dTubeMatch = link.match(dTubeRegex)[0];
const execLink = dTubeRegex.exec(dTubeMatch);
if (execLink[2] && execLink[3]) { if (execLink[2] && execLink[3]) {
const embedLink = `https://emb.d.tube/#!/${execLink[2]}/${execLink[3]}`; const embedLink = `https://emb.d.tube/#!/${execLink[2]}/${execLink[3]}`;
return iframeBody(embedLink); return iframeBody(embedLink);
} }
if (dTubeMatch) { if (dTubeMatch) {
return iframeBody(dTubeMatch); return iframeBody(dTubeMatch);
}
return link;
}
if (imgRegex.test(link)) {
const imgMatch = link.match(imgRegex)[0];
if (imgMatch) return `<a src="${imgMatch}">Image</a>`;
}
return link;
});
const handleHTag = input => input.replace(hTagRegex, tag => `<div>${tag}</div>`);
const handleMarkdownLink = input => input.replace(copiedPostRegex, (link) => {
const postMatch = link.match(copiedPostRegex);
if (postMatch) {
let tag = postMatch[1];
if (tag === '/busy.org') {
tag = 'busy';
}
const _permlink = postMatch[3].indexOf(')') > 0 ? postMatch[3].replace(')', '') : postMatch[3];
return `<a class="markdown-post-link" href="${_permlink}" data_tag={${tag.trim()}} data_author="${postMatch[2].replace(
'@',
'',
)}">/${_permlink}</a>`;
}
});
const handleLinks = input => input.replace(linkRegex, (link) => {
if (link) {
if (
link
.toLowerCase()
.trim()
.indexOf('https://steemitimages.com/0x0/') === 0
|| imgRegex.test(link)
) {
const imageMatch = link.match(imgRegex);
if (imageMatch) {
if (imageMatch[0].indexOf('.gif') > 0) {
return gifBody(imageMatch[0]);
}
if (imageMatch[0]) {
return imageBody(imageMatch[0]);
}
} else if (link.trim().indexOf('ipfs.busy.org') > 0) {
return imageBody(link);
} }
return link; return link;
} }
if (link.trim().indexOf('ipfs.busy.org') > 0) {
return imageBody(link);
}
if (imgRegex.test(link)) { if (imgRegex.test(link)) {
return imageBody(link); const imgMatch = link.match(imgRegex)[0];
if (imgMatch) return `<a src="${imgMatch}">Image</a>`;
} }
}
return link; return link;
}); });
const changeMarkdownImage = input => input.replace(markdownImageRegex, (link) => { const handleHTag = input => input.replace(hTagRegex, tag => `<div>${tag}</div>`);
const markdownMatch = link.match(markdownImageRegex);
if (markdownMatch[0]) {
const firstMarkdownMatch = markdownMatch[0];
const _link = firstMarkdownMatch.match(urlRegex)[0];
return _link; const handleMarkdownLink = input =>
} input.replace(copiedPostRegex, link => {
return link; const postMatch = link.match(copiedPostRegex);
});
const centerStyling = input => input.replace( if (postMatch) {
centerRegex, let tag = postMatch[1];
() => '<center style="text-align: center; align-items: center; justify-content: center;">',
);
const steemitUrlHandle = input => input.replace(postRegex, (link) => { if (tag === '/busy.org') {
const postMatch = link.match(postRegex); tag = 'busy';
const tag = postMatch[2]; }
const author = postMatch[3].replace('@', '');
const permlink = postMatch[4];
return `<a class="markdown-post-link" href="${permlink}" data_tag={${tag}} data_author="${author}">/${permlink}</a>`; const _permlink =
}); postMatch[3].indexOf(')') > 0 ? postMatch[3].replace(')', '') : postMatch[3];
const handleImageTag = input => input.replace(imgTagRegex, (imgTag) => { return `<a class="markdown-post-link" href="${_permlink}" data_tag={${tag.trim()}} data_author="${postMatch[2].replace(
const _imgTag = imgTag.trim(); '@',
const match = _imgTag.match(imgRegex); '',
)}">/${_permlink}</a>`;
}
});
if (match && match[0]) { const handleLinks = input =>
return match[0]; input.replace(linkRegex, link => {
} if (link) {
if (
link
.toLowerCase()
.trim()
.indexOf('https://steemitimages.com/0x0/') === 0 ||
imgRegex.test(link)
) {
const imageMatch = link.match(imgRegex);
return imgTag; if (imageMatch) {
}); if (imageMatch[0].indexOf('.gif') > 0) {
return gifBody(imageMatch[0]);
}
const createYoutubeIframe = input => input.replace(youTubeRegex, (link) => { if (imageMatch[0]) {
const execVideo = youTubeRegex.exec(link); return imageBody(imageMatch[0]);
const match = link.match(youTubeRegex); }
} else if (link.trim().indexOf('ipfs.busy.org') > 0) {
return imageBody(link);
}
if (execVideo[1] && match) { return link;
const videoLink = execVideo[1]; }
const embedLink = `https://www.youtube.com/embed/${videoLink}`; if (link.trim().indexOf('ipfs.busy.org') > 0) {
return imageBody(link);
}
if (imgRegex.test(link)) {
return imageBody(link);
}
}
return link;
});
const changeMarkdownImage = input =>
input.replace(markdownImageRegex, link => {
const markdownMatch = link.match(markdownImageRegex);
if (markdownMatch[0]) {
const firstMarkdownMatch = markdownMatch[0];
const _link = firstMarkdownMatch.match(urlRegex)[0];
return _link;
}
return link;
});
const centerStyling = input =>
input.replace(
centerRegex,
() => '<center style="text-align: center; align-items: center; justify-content: center;">',
);
const steemitUrlHandle = input =>
input.replace(postRegex, link => {
const postMatch = link.match(postRegex);
const tag = postMatch[2];
const author = postMatch[3].replace('@', '');
const permlink = postMatch[4];
return `<a class="markdown-post-link" href="${permlink}" data_tag={${tag}} data_author="${author}">/${permlink}</a>`;
});
const handleImageTag = input =>
input.replace(imgTagRegex, imgTag => {
const _imgTag = imgTag.trim();
const match = _imgTag.match(imgRegex);
if (match && match[0]) {
return match[0];
}
return imgTag;
});
const createYoutubeIframe = input =>
input.replace(youTubeRegex, link => {
if (link.indexOf(')') || link.indexOf('(')) {
return link;
}
const execVideo = youTubeRegex.exec(link);
const match = link.match(youTubeRegex);
if (execVideo[1] && match) {
const videoLink = execVideo[1];
const embedLink = `https://www.youtube.com/embed/${videoLink}`;
return iframeBody(embedLink);
}
return link;
});
const handleIframe = input =>
input.replace(iframeRegex, link => {
const match = link.match(linkRegex);
if (match && match[0]) {
return iframeBody(match[0]);
}
return link;
});
const createVimeoIframe = input =>
input.replace(vimeoRegex, link => {
const execLink = vimeoRegex.exec(link);
const embedLink = `https://player.vimeo.com/video/${execLink[3]}`;
return iframeBody(embedLink); return iframeBody(embedLink);
} });
return link;
});
const handleIframe = input => input.replace(iframeRegex, (link) => {
const match = link.match(linkRegex);
if (match && match[0]) {
return iframeBody(match[0]);
}
return link;
});
const createVimeoIframe = input => input.replace(vimeoRegex, (link) => {
const execLink = vimeoRegex.exec(link);
const embedLink = `https://player.vimeo.com/video/${execLink[3]}`;
return iframeBody(embedLink);
});
const handleImageLink = input => input.replace(imgRegex, link => imageBody(link));
const iframeBody = link => `<iframe frameborder='0' allowfullscreen src='${link}'></iframe>`; const iframeBody = link => `<iframe frameborder='0' allowfullscreen src='${link}'></iframe>`;
const imageBody = link => `<center style="text-align: center; align-items: center; justify-content: center;"><img src="${`https://steemitimages.com/600x0/${link}`}" /></center>`; const imageBody = link =>
`<center style="text-align: center; align-items: center; justify-content: center;"><img src="${`https://steemitimages.com/600x0/${link}`}" /></center>`;
const gifBody = link => `<img src="${`https://steemitimages.com/0x0/${link}`}" />`; const gifBody = link => `<img src="${`https://steemitimages.com/0x0/${link}`}" />`;
const handleImageLink = input => input.replace(imgRegex, link => imageBody(link));
// const handleCodeTag = input => input.replace(codeTagRegex, (tag) => { // const handleCodeTag = input => input.replace(codeTagRegex, (tag) => {
// const stringsRegex = /(?<=>)(.*)(?=<)/g; // const stringsRegex = /(?<=>)(.*)(?=<)/g;