Skip to content

Commit 941a28b

Browse files
Add a script to resize the pictures for the blogposts page. Add the script in the package.json as prestart and prebuild. Update blogpostsDetails.json and BlogpostCard.tsx.
1 parent 66e6e04 commit 941a28b

File tree

301 files changed

+912
-12292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+912
-12292
lines changed

package-lock.json

Lines changed: 498 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
},
1010
"scripts": {
1111
"docusaurus": "docusaurus",
12+
"prestart": "node scripts/resize-images.js",
13+
"prebuild": "node scripts/resize-images.js.js",
1214
"start": "docusaurus start",
1315
"build": "docusaurus build",
1416
"swizzle": "docusaurus swizzle",
@@ -33,7 +35,7 @@
3335
"clsx": "^2.0.0",
3436
"curl": "^0.1.4",
3537
"fs": "^0.0.1-security",
36-
"image-size": "^1.1.1",
38+
"image-size": "^1.2.1",
3739
"jimp": "^0.22.12",
3840
"js-yaml": "^4.1.0",
3941
"nodejs": "^0.0.0",
@@ -48,6 +50,7 @@
4850
"react-slick": "^0.30.2",
4951
"reactjs-popup": "^2.0.6",
5052
"request": "^2.88.2",
53+
"sharp": "^0.34.2",
5154
"slick-carousel": "^1.8.1"
5255
},
5356
"devDependencies": {
@@ -72,4 +75,4 @@
7275
"engines": {
7376
"node": "18.x"
7477
}
75-
}
78+
}

scripts/resize-images.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const sharp = require('sharp');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const sizeOf = require('image-size');
5+
const inputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'full-size-images');
6+
const outputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'resized-images');
7+
const containerHeight = 180;
8+
const containerWidth = 273;
9+
10+
if (!fs.existsSync(outputDir)) {
11+
fs.mkdirSync(outputDir, { recursive: true });
12+
}
13+
14+
fs.readdirSync(inputDir).forEach((file) => {
15+
const inputPath = path.join(inputDir, file);
16+
if (fs.existsSync(inputPath)) {
17+
const dimensions = sizeOf(inputPath);
18+
19+
const aspectRatio = dimensions.width / dimensions.height;
20+
const outputPath = path.join(outputDir, file);
21+
let targetWidth;
22+
if (aspectRatio <= 1.0) {
23+
targetWidth = Math.round(containerHeight * aspectRatio);
24+
} else {
25+
targetWidth = Math.round(containerWidth) * 2;
26+
}
27+
if (/\.(png|jpg)$/i.test(file)) {
28+
sharp(inputPath)
29+
.resize({ width: targetWidth })
30+
.toFile(outputPath)
31+
}
32+
}
33+
});

src/components/blog/BlogpostCard.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export default function BlogpostCard({ blogpost, timeIndex }) {
3232
<img
3333
src={useBaseUrl(blogpost.image)}
3434
id={blogpost.imageID}
35-
width={blogpost.imageRenderedWidth}
36-
height={blogpost.imageRenderedHeight}
3735
alt={"Illustration for the blog post."}
3836
/>
3937
</div>

src/components/blog/_config.yml

Lines changed: 0 additions & 839 deletions
This file was deleted.

src/components/blog/blogpostsDetails.json

Lines changed: 268 additions & 804 deletions
Large diffs are not rendered by default.

src/components/blog/resize.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/components/blog/yml-to-json.js

Lines changed: 0 additions & 148 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)