Added thumbnails script

This commit is contained in:
Bashy 2025-09-23 19:56:13 +02:00
parent 81bc936f81
commit b1f096102b
3 changed files with 74 additions and 8 deletions

View file

@ -21,13 +21,13 @@ copyright: true # Display copyright
# Defaults into FontAwesome if no image provided
# Disables scarf if no image, and font awesome not configured
scarf:
- url: / # Example Usecase for image
- url: tags/baking # Example Usecase for image
image: images/assets/scarves/scarf.png
icon: false
enable: true
color: #fff
color: false
alt: Example
- url: / # Example Usecase for FontAwesome
- url: tags/photo # Example Usecase for FontAwesome
image: false
icon: fa-solid fa-heart
enable: true
@ -45,9 +45,9 @@ social:
- url: https://www.twitch.tv/
icon: fab fa-twitch
enable: false
- url: https://www.goodreads.com/
icon: fab fa-goodreads
enable: false
- url: https://mastodon.social
icon: fab fa-mastodon
enable: true
- url: https://www.linkedin.com/
icon: fab fa-linkedin
enable: true

View file

@ -3,7 +3,7 @@
<% if(post.photos) { %>
<div class="cover">
<a href='<%= url_for(post.path) %>'>
<img class="gallery" src="<%- config.root %>thumbnails/small-<%- post.photos[0].replace(/^images\//, '') %>" alt="<%- post.title || "Untitled" %>">
<img class="gallery" src="<%- config.root %>thumbnails/<%- (theme.column == 2 ? 'medium' : 'small') + '-' + post.photos[0].replace(/^images\//, '') %>" alt="<%- post.title || 'Untitled' %>">
</a>
</div>
<% } %>

66
scripts/thumbnails.js Normal file
View file

@ -0,0 +1,66 @@
// This script was made with help of ChatGPT and adjusted to correct working state.
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
hexo.extend.filter.register('after_init', async function() {
if (!hexo.theme.config.thumbnails) return;
if ( (hexo.env.cmd !== 'generate') && (hexo.env.cmd !== 'g') ) return;
console.log('Generating thumbnails...');
const source = path.join(hexo.base_dir, 'source/images');
const thumbnails = path.join(hexo.base_dir, 'source/thumbnails');
if (!fs.existsSync(thumbnails)) {
fs.mkdirSync(thumbnails, { recursive: true });
}
// Thumbnail sizes
const sizes = [
{ name: 'small', width: 333 }, // Three-column layout
{ name: 'medium', width: 666 }, // Two-column layout
{ name: 'large', width: 1000 } // Normal post
];
const getAllFilesInclSubfolders = (dir) => {
let results = [];
fs.readdirSync(dir).forEach(file => {
if (file === '.' || file === '..') return;
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
results = results.concat(getAllFilesInclSubfolders(fullPath));
} else {
results.push(fullPath);
}
});
return results;
};
const imageFiles = getAllFilesInclSubfolders(source);
imageFiles.forEach(inputFile => {
const ext = path.extname(inputFile).toLowerCase();
if (['.jpg', '.jpeg', '.png', '.webp'].includes(ext)) {
sizes.forEach(size => {
// Maintain folder structure in thumbnails
const relativePath = path.relative(thumbnails, inputFile).replace(/^(\.\.\/)+images\//, '');
const outputFile = path.join(thumbnails, size.name + '-' + relativePath);
console.log(`PARTIAL: ${inputFile} - ${relativePath}`);
// Ensure the output directory structure exists
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
if (!fs.existsSync(outputFile)) {
sharp(inputFile)
.resize(size.width)
.toFile(outputFile)
.then(() => console.log(`Generated: ${outputFile}`))
.catch(err => console.error(`Error processing ${inputFile}:`, err));
}
});
}
});
});