Simplified and fixed thumbnail issues
This commit is contained in:
parent
600e9a5213
commit
fa71a1c61f
3 changed files with 17 additions and 26 deletions
|
|
@ -4,7 +4,7 @@
|
|||
<h1 class="article"><%= page.title || "Untitled" %></h1>
|
||||
<% if(page.photos) { %>
|
||||
<div class="cover">
|
||||
<img class="gallery" src="<%- config.root %>thumbnails/large-<%- page.photos[0].replace(/^images\//, '')%>" alt="<%- page.title || "Untitled" %>">
|
||||
<img class="gallery" src="<%- config.root %><%- page.photos[0] %>" alt="<%- page.title || "Untitled" %>">
|
||||
</div>
|
||||
<% } %>
|
||||
<p><%= page.description %></p>
|
||||
|
|
|
|||
|
|
@ -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/<%- post.photos[0].replace(/^images\//, '') %>" alt="<%- post.title || 'Untitled' %>">
|
||||
</a>
|
||||
</div>
|
||||
<% } %>
|
||||
|
|
|
|||
|
|
@ -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,11 +35,10 @@ 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\//, '');
|
||||
|
||||
const outputFile = path.join(thumbnails, size.name + '-' + relativePath);
|
||||
const outputFile = path.join(thumbnails, relativePath);
|
||||
console.log(`PARTIAL: ${inputFile} - ${relativePath}`);
|
||||
|
||||
// Ensure the output directory structure exists
|
||||
|
|
@ -54,12 +46,11 @@ hexo.extend.filter.register('after_init', async function() {
|
|||
|
||||
if (!fs.existsSync(outputFile)) {
|
||||
sharp(inputFile)
|
||||
.resize(size.width)
|
||||
.resize(512)
|
||||
.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