Added thumbnails script
This commit is contained in:
parent
81bc936f81
commit
b1f096102b
3 changed files with 74 additions and 8 deletions
|
|
@ -21,13 +21,13 @@ copyright: true # Display copyright
|
||||||
# Defaults into FontAwesome if no image provided
|
# Defaults into FontAwesome if no image provided
|
||||||
# Disables scarf if no image, and font awesome not configured
|
# Disables scarf if no image, and font awesome not configured
|
||||||
scarf:
|
scarf:
|
||||||
- url: / # Example Usecase for image
|
- url: tags/baking # Example Usecase for image
|
||||||
image: images/assets/scarves/scarf.png
|
image: images/assets/scarves/scarf.png
|
||||||
icon: false
|
icon: false
|
||||||
enable: true
|
enable: true
|
||||||
color: #fff
|
color: false
|
||||||
alt: Example
|
alt: Example
|
||||||
- url: / # Example Usecase for FontAwesome
|
- url: tags/photo # Example Usecase for FontAwesome
|
||||||
image: false
|
image: false
|
||||||
icon: fa-solid fa-heart
|
icon: fa-solid fa-heart
|
||||||
enable: true
|
enable: true
|
||||||
|
|
@ -45,9 +45,9 @@ social:
|
||||||
- url: https://www.twitch.tv/
|
- url: https://www.twitch.tv/
|
||||||
icon: fab fa-twitch
|
icon: fab fa-twitch
|
||||||
enable: false
|
enable: false
|
||||||
- url: https://www.goodreads.com/
|
- url: https://mastodon.social
|
||||||
icon: fab fa-goodreads
|
icon: fab fa-mastodon
|
||||||
enable: false
|
enable: true
|
||||||
- url: https://www.linkedin.com/
|
- url: https://www.linkedin.com/
|
||||||
icon: fab fa-linkedin
|
icon: fab fa-linkedin
|
||||||
enable: true
|
enable: true
|
||||||
|
|
@ -81,4 +81,4 @@ sidebar_menu:
|
||||||
- enable: true
|
- enable: true
|
||||||
name: The Hackman Club
|
name: The Hackman Club
|
||||||
url: https://blog.hackman.club
|
url: https://blog.hackman.club
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
<% if(post.photos) { %>
|
<% if(post.photos) { %>
|
||||||
<div class="cover">
|
<div class="cover">
|
||||||
<a href='<%= url_for(post.path) %>'>
|
<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>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
|
||||||
66
scripts/thumbnails.js
Normal file
66
scripts/thumbnails.js
Normal 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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue