Skip to content

Commit f65c4aa

Browse files
committed
fix thumbnail images
1 parent 8732644 commit f65c4aa

File tree

5 files changed

+550
-16
lines changed

5 files changed

+550
-16
lines changed

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const http = require('http');
22
const url = require('url');
3-
const fetch = require('node-fetch');
43
const ArticleCard = require('./src/ArticleCard');
54
const { userArticles } = require('./src/mediumAPI');
65
const { asyncForEach } = require('./src/utils');

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-readme-medium",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -18,7 +18,8 @@
1818
},
1919
"homepage": "https://github.com/omidnikrah/github-readme-medium#readme",
2020
"dependencies": {
21-
"node-fetch": "^2.6.0",
22-
"rss-parser": "^3.9.0"
21+
"axios": "^0.24.0",
22+
"rss-parser": "^3.9.0",
23+
"sharp": "^0.29.3"
2324
}
24-
}
25+
}

src/ArticleCard.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
const { readingTimeCalc, imgToDataURL } = require('./utils');
22

33
const ArticleCard = async (data, colors) => {
4-
54
const thumbnailBase64 = await imgToDataURL(data.thumbnail);
65
const articleDate = new Date(data.pubDate);
76
const readingTime = readingTimeCalc(data.content);
8-
var re = /[0-9A-Fa-f]{6}/g; //hex code format
9-
var hexBg = null
10-
var hexText = null
11-
console.log(colors)
7+
const re = /[0-9A-Fa-f]{6}/g; //hex code format
8+
let hexBg = null;
9+
let hexText = null;
1210
if (re.test(colors.bg)) { //converted to hex format
1311
hexBg = `#${colors.bg}`
1412
}
@@ -18,7 +16,7 @@ const ArticleCard = async (data, colors) => {
1816

1917
return `
2018
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="390px" height="100px" version="1.2" baseProfile="tiny" style="margin: 10px">
21-
<rect width="100%" height="100%" fill="${hexBg ? hexBg : colors.bg}" style="padding:10px;"/>
19+
<rect width="100%" height="100%" fill="${hexBg ? hexBg : colors.bg}" style="padding:10px;"/>
2220
<g fill="none" stroke="black" stroke-width="1" fill-rule="evenodd" stroke-linecap="square" stroke-linejoin="bevel">
2321
2422
<g fill="#000000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="miter" stroke-miterlimit="2" transform="matrix(1,0,0,1,0,0)">
@@ -28,9 +26,7 @@ const ArticleCard = async (data, colors) => {
2826
</g>
2927
3028
<g fill="#000000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="miter" stroke-miterlimit="2" transform="matrix(1,0,0,1,0,0)">
31-
<pattern patternUnits="userSpaceOnUse" x="0" y="0" width="100%" height="100%">
32-
<image x="0" y="0" width="100" height="100" preserveAspectRatio="xMidYMid slice" xlink:href="${thumbnailBase64}"/>
33-
</pattern>
29+
<image x="0" y="0" width="100" height="100" preserveAspectRatio="xMidYMid slice" xlink:href="${thumbnailBase64}"/>
3430
</g>
3531
3632
<g fill="#000000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)">

src/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const fetch = require('node-fetch');
1+
const axios = require('axios');
2+
const sharp = require('sharp');
23

34
const ansiWordBound = (c) => (
45
(' ' === c) ||
@@ -31,7 +32,7 @@ const readingTimeCalc = (text) => {
3132
}
3233

3334
const imgToDataURL = url => {
34-
return fetch(url).then(response => response.buffer()).then(buffer => `data:image/png;base64,${buffer.toString('base64')}`);
35+
return axios.get(url, { responseType: 'arraybuffer' }).then(({ data }) => sharp(data).resize(200).toBuffer()).then(data => `data:image/png;base64,${data.toString('base64')}`);
3536
};
3637

3738
const asyncForEach = async (array, callback) => {

0 commit comments

Comments
 (0)