minimized images and handled different kind image url

This commit is contained in:
u-e 2018-12-20 13:18:42 +03:00
parent 694ae454fd
commit 65c92982b1

View File

@ -12,7 +12,7 @@ export const parsePost = (post, currentUserName) => {
} }
post.json_metadata = JSON.parse(post.json_metadata); post.json_metadata = JSON.parse(post.json_metadata);
post.image = postImage(post.json_metadata); post.image = postImage(post.json_metadata, post.body);
post.pending_payout_value = parseFloat(post.pending_payout_value).toFixed(2); post.pending_payout_value = parseFloat(post.pending_payout_value).toFixed(2);
post.created = getTimeFromNow(post.created); post.created = getTimeFromNow(post.created);
post.vote_count = post.active_votes.length; post.vote_count = post.active_votes.length;
@ -52,11 +52,30 @@ export const parsePost = (post, currentUserName) => {
const isVoted = (activeVotes, currentUserName) => activeVotes.some(v => v.voter === currentUserName && v.percent > 0); const isVoted = (activeVotes, currentUserName) => activeVotes.some(v => v.voter === currentUserName && v.percent > 0);
const postImage = (metaData) => { const postImage = (metaData, body) => {
const markdownImageRegex = /!\[[^\]]*\]\((.*?)\s*("(?:.*[^"])")?\s*\)/g;
const urlRegex = /(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/gm;
const imageRegex = /(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png)/g;
let imageLink;
if (metaData && metaData.image && metaData.image[0]) { if (metaData && metaData.image && metaData.image[0]) {
return `https://cdn.steemitimages.com/300x0/${metaData.image[0]}`; imageLink = metaData.image[0];
} else if (markdownImageRegex.test(body)) {
const markdownMatch = body.match(markdownImageRegex);
if (markdownMatch) {
const firstMarkdownMatch = markdownMatch[0];
imageLink = firstMarkdownMatch.match(urlRegex)[0];
}
} }
if(!imageLink && imageRegex.test(body)) {
const imageMatch = body.match(imageRegex);
imageLink = imageMatch[0];
}
if (imageLink) {
return `https://cdn.steemitimages.com/300x0/${imageLink}`;
}
return ''; return '';
}; };