From 7dd8e0470e16778c13b44a67d49807ffde9d6007 Mon Sep 17 00:00:00 2001 From: u-e Date: Sun, 31 Mar 2019 13:32:35 +0300 Subject: [PATCH 1/2] fixed youtube link handle --- src/utils/markdownToHtml.js | 310 +++++++++++++++++++----------------- 1 file changed, 164 insertions(+), 146 deletions(-) diff --git a/src/utils/markdownToHtml.js b/src/utils/markdownToHtml.js index f29ebefb6..cb5b46934 100644 --- a/src/utils/markdownToHtml.js +++ b/src/utils/markdownToHtml.js @@ -1,3 +1,4 @@ +/* eslint-disable */ import Remarkable from 'remarkable'; // TODO: Refactoring need! const md = new Remarkable({ html: true, breaks: true, linkify: true }); @@ -24,7 +25,7 @@ const imgTagRegex = /(]*>)/g; const iframeRegex = /(?:]*)(?:(?:\/>)|(?:>.*?<\/iframe>))/g; const hTagRegex = /(([^<]*)<\/h([1-6])>)/g; -export const markDown2Html = (input) => { +export const markDown2Html = input => { if (!input) { return ''; } @@ -95,177 +96,194 @@ export const markDown2Html = (input) => { return output; }; -const replaceAuthorNames = input => input.replace(authorNameRegex, (match, preceeding1, preceeding2, user) => { - const userLower = user.toLowerCase(); - const preceedings = (preceeding1 || '') + (preceeding2 || ''); - return `${preceedings} @${user}`; -}); +const replaceAuthorNames = input => + input.replace(authorNameRegex, (match, preceeding1, preceeding2, user) => { + const userLower = user.toLowerCase(); + const preceedings = (preceeding1 || '') + (preceeding2 || ''); + return `${preceedings} @${user}`; + }); -const replaceTags = input => input.replace(tagsRegex, (tag) => { - if (/#[\d]+$/.test(tag)) return tag; - const preceding = /^\s|>/.test(tag) ? tag[0] : ''; - tag = tag.replace('>', ''); - const tag2 = tag.trim().substring(1); - const tagLower = tag2.toLowerCase(); - return `${preceding}${tag.trim()}`; -}); +const replaceTags = input => + input.replace(tagsRegex, tag => { + if (/#[\d]+$/.test(tag)) return tag; + const preceding = /^\s|>/.test(tag) ? tag[0] : ''; + tag = tag.replace('>', ''); + const tag2 = tag.trim().substring(1); + const tagLower = tag2.toLowerCase(); + return `${preceding}${tag.trim()}`; + }); -const handleATag = input => input.replace(aTagRegex, (link) => { - if (dTubeRegex.test(link)) { - const dTubeMatch = link.match(dTubeRegex)[0]; - const execLink = dTubeRegex.exec(dTubeMatch); +const handleATag = input => + input.replace(aTagRegex, link => { + if (dTubeRegex.test(link)) { + const dTubeMatch = link.match(dTubeRegex)[0]; + const execLink = dTubeRegex.exec(dTubeMatch); - if (execLink[2] && execLink[3]) { - const embedLink = `https://emb.d.tube/#!/${execLink[2]}/${execLink[3]}`; + if (execLink[2] && execLink[3]) { + const embedLink = `https://emb.d.tube/#!/${execLink[2]}/${execLink[3]}`; - return iframeBody(embedLink); - } - if (dTubeMatch) { - return iframeBody(dTubeMatch); - } - return link; - } - - if (imgRegex.test(link)) { - const imgMatch = link.match(imgRegex)[0]; - - if (imgMatch) return `Image`; - } - - return link; -}); - -const handleHTag = input => input.replace(hTagRegex, tag => `
${tag}
`); - -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 `/${_permlink}`; - } -}); - -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 iframeBody(embedLink); + } + if (dTubeMatch) { + return iframeBody(dTubeMatch); } - return link; } - if (link.trim().indexOf('ipfs.busy.org') > 0) { - return imageBody(link); - } if (imgRegex.test(link)) { - return imageBody(link); + const imgMatch = link.match(imgRegex)[0]; + + if (imgMatch) return `Image`; } - } - return 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]; +const handleHTag = input => input.replace(hTagRegex, tag => `
${tag}
`); - return _link; - } - return link; -}); +const handleMarkdownLink = input => + input.replace(copiedPostRegex, link => { + const postMatch = link.match(copiedPostRegex); -const centerStyling = input => input.replace( - centerRegex, - () => '
', -); + if (postMatch) { + let tag = postMatch[1]; -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]; + if (tag === '/busy.org') { + tag = 'busy'; + } - return `/${permlink}`; -}); + const _permlink = + postMatch[3].indexOf(')') > 0 ? postMatch[3].replace(')', '') : postMatch[3]; -const handleImageTag = input => input.replace(imgTagRegex, (imgTag) => { - const _imgTag = imgTag.trim(); - const match = _imgTag.match(imgRegex); + return `/${_permlink}`; + } + }); - if (match && match[0]) { - return match[0]; - } +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); - return imgTag; -}); + if (imageMatch) { + if (imageMatch[0].indexOf('.gif') > 0) { + return gifBody(imageMatch[0]); + } -const createYoutubeIframe = input => input.replace(youTubeRegex, (link) => { - const execVideo = youTubeRegex.exec(link); - const match = link.match(youTubeRegex); + if (imageMatch[0]) { + return imageBody(imageMatch[0]); + } + } else if (link.trim().indexOf('ipfs.busy.org') > 0) { + return imageBody(link); + } - if (execVideo[1] && match) { - const videoLink = execVideo[1]; - const embedLink = `https://www.youtube.com/embed/${videoLink}`; + return link; + } + 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, + () => '
', + ); + +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 `/${permlink}`; + }); + +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 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 => ``; -const imageBody = link => `
`; +const imageBody = link => + `
`; const gifBody = link => ``; +const handleImageLink = input => input.replace(imgRegex, link => imageBody(link)); // const handleCodeTag = input => input.replace(codeTagRegex, (tag) => { // const stringsRegex = /(?<=>)(.*)(?=<)/g; From c45a8357f99544cb63032eddfae50fca9b30ba6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?u=C4=9Fur=20erdal?= Date: Sat, 6 Apr 2019 13:41:30 +0300 Subject: [PATCH 2/2] Update markdownToHtml.js --- src/utils/markdownToHtml.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/markdownToHtml.js b/src/utils/markdownToHtml.js index cb5b46934..a66b09ebf 100644 --- a/src/utils/markdownToHtml.js +++ b/src/utils/markdownToHtml.js @@ -1,4 +1,3 @@ -/* eslint-disable */ import Remarkable from 'remarkable'; // TODO: Refactoring need! const md = new Remarkable({ html: true, breaks: true, linkify: true });