diff --git a/index.js b/index.js index 179c913..66e7f3c 100644 --- a/index.js +++ b/index.js @@ -11,14 +11,13 @@ http.createServer(async (req, res) => { const timestamp = Math.floor(Date.now() / 1000); let articles = []; - if (!username) { res.write(JSON.stringify({ error: 'Add your medium username as query string' })); res.end(); return; } - const responseArticles = await userArticles(`${username}?t=${timestamp}`); + const {articles: responseArticles, profileImgUrl} = await userArticles(`${username}?t=${timestamp}`); if (!responseArticles || responseArticles.length === 0) { @@ -44,7 +43,7 @@ http.createServer(async (req, res) => { let result = ``; await asyncForEach(articles, async (article, index) => { - const articleCard = await ArticleCard(article, colors); + const articleCard = await ArticleCard(article, colors,profileImgUrl); result += `${articleCard}`; }); diff --git a/src/ArticleCard.js b/src/ArticleCard.js index d4c00f4..1ea1432 100644 --- a/src/ArticleCard.js +++ b/src/ArticleCard.js @@ -1,7 +1,12 @@ const { readingTimeCalc, imgToDataURL } = require('./utils'); -const ArticleCard = async (data, colors) => { - const thumbnailBase64 = await imgToDataURL(data.thumbnail); +function trimText(text, threshold) { + if (text.length <= threshold) return text; + return text.substr(0, threshold).concat("..."); +} + +const ArticleCard = async (data, colors, placeHolderImgUrl) => { + const thumbnailBase64 = await imgToDataURL(data.thumbnail,placeHolderImgUrl); const articleDate = new Date(data.pubDate); const readingTime = readingTimeCalc(data.content); const re = /[0-9A-Fa-f]{6}/g; //hex code format @@ -31,7 +36,7 @@ const ArticleCard = async (data, colors) => { - ${data.title.replace(/&(?!#?[a-z0-9]+;)/g, '&')} + ${trimText(data.title.replace(/&(?!#?[a-z0-9]+;)/g, '&'),33)} diff --git a/src/mediumAPI.js b/src/mediumAPI.js index 5aae893..36a199f 100644 --- a/src/mediumAPI.js +++ b/src/mediumAPI.js @@ -7,6 +7,7 @@ const userArticles = async (username) => { const feed = await parser.parseURL(`https://medium.com/feed/@${username}`); let result = []; + const imgUrl = feed.image.url; feed.items.forEach((item) => { const imageObj = imageRegex.exec(item['content:encoded']); @@ -18,7 +19,7 @@ const userArticles = async (username) => { result = [...result, item]; }); - return result; + return {profileImgUrl: imgUrl, articles: result}; }; diff --git a/src/utils.js b/src/utils.js index 01b76b2..b8b272e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -31,8 +31,21 @@ const readingTimeCalc = (text) => { return `${displayed} min read`; } -const imgToDataURL = url => { - return axios.get(url, { responseType: 'arraybuffer' }).then(({ data }) => sharp(data).resize(200).toBuffer()).then(data => `data:image/png;base64,${data.toString('base64')}`); +const imgToDataURL = async (url,placeHolder) => { + async function processImage(url) { + let {data} = await axios.get(url, { responseType: 'arraybuffer' }); + let sharpedData = await sharp(data).resize(200).toBuffer(); + return `data:image/png;base64,${sharpedData.toString('base64')}` + } + try{ + return await processImage(url); + }catch(e){ + try{ + return await processImage(placeHolder); + }catch(e){ + return await processImage("https://lippianfamilydentistry.net/wp-content/uploads/2015/11/user-default.png"); + } + } }; const asyncForEach = async (array, callback) => {