Simplified and fixed thumbnail issues

This commit is contained in:
Bashy 2025-09-24 20:23:23 +03:00
parent 600e9a5213
commit fa71a1c61f
3 changed files with 17 additions and 26 deletions

View file

@ -5,7 +5,6 @@ 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...');
@ -17,12 +16,6 @@ hexo.extend.filter.register('after_init', async function() {
fs.mkdirSync(thumbnails, { recursive: true });
}
// Thumbnail sizes
const sizes = [
{ name: 'small', width: 512 }, // Three-column layout
{ name: 'large', width: 1024 } // Normal post
];
const getAllFilesInclSubfolders = (dir) => {
let results = [];
fs.readdirSync(dir).forEach(file => {
@ -42,24 +35,22 @@ hexo.extend.filter.register('after_init', async function() {
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\//, '');
// 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));
}
});
const outputFile = path.join(thumbnails, 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(512)
.toFile(outputFile)
.then(() => console.log(`Generated: ${outputFile}`))
.catch(err => console.error(`Error processing ${inputFile}:`, err));
}
}
});
});