Skip to content

Commit

Permalink
Fix broken image
Browse files Browse the repository at this point in the history
  • Loading branch information
pahlevikun committed Apr 7, 2024
1 parent 4a9a1d1 commit c93793a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/app.js"
}
]
}
5 changes: 3 additions & 2 deletions assets/card.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require("../common/config");
const { parseImgToDataURL: imgToDataURL, dateFormat, measureReadingTime } = require("../common/utils");
const { parseImgToDataURL: imgToDataURL, dateFormat, measureReadingTime, getFirstImageFromHTML } = require("../common/utils");

const mediumCard = async (data, settings, index) => {
const result = await imgToDataURL(data.thumbnail);
const firstImageURL = getFirstImageFromHTML(data.content);
const result = await imgToDataURL(firstImageURL);
const blogImage = "data:image/png;base64," + result.toString("base64");
const blogDate = await dateFormat(data.pubDate);
const blogLink = data.link;
Expand Down
9 changes: 8 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const axios = require("axios");
const sharp = require("sharp");
const cheerio = require('cheerio');

function getFirstImageFromHTML(html) {
const $ = cheerio.load(html);
const firstImage = $('img').first().attr('src');
return firstImage;
}

const ansiWordBound = (c) => (
(' ' === c) ||
Expand Down Expand Up @@ -75,4 +82,4 @@ const asyncForEach = async (array, settings, callback) => {
}
};

module.exports = { parseImgToDataURL: parseImgToDataURL, asyncForEach, dateFormat, measureReadingTime };
module.exports = { parseImgToDataURL: parseImgToDataURL, asyncForEach, dateFormat, measureReadingTime, getFirstImageFromHTML };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"axios": "^0.19.2",
"cheerio": "^1.0.0-rc.12",
"express": "^4.17.1",
"sharp": "^0.26.2"
}
Expand Down
2 changes: 0 additions & 2 deletions route/api/medium.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ async function getArticlesByUsername(username) {
const result = await axios.get(mediumURL + username);
const filteredResult = result.data.items.filter(
(item) =>
(!item.thumbnail.includes("stat?event") ||
!item.thumbnail.includes("&referrerSource")) &&
item.categories.length > 0
);
return filteredResult;
Expand Down

0 comments on commit c93793a

Please sign in to comment.