Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gallery generator for Phugo
const fs = require('fs');
fs.readdirSync('content')
.filter(entry => fs.statSync(`content/${entry}`).isDirectory())
.forEach(dir => {
let files = fs.readdirSync(`content/${dir}`)
.filter(file => file.endsWith('.jpg'))
.filter(file => !file.startsWith('_th_'));
let content;
if (fs.existsSync(`content/${dir}/_index.md`)) {
const lines = fs.readFileSync(`content/${dir}/_index.md`, {encoding : 'utf8'}).split('\n');
if (lines.indexOf('manual: true') !== -1) {
return;
}
const headerEnd = lines.lastIndexOf('---');
content = lines.slice(0, headerEnd + 1).join('\n');
} else {
content = `---
title: "${dir}"
albumthumb: "${dir}/_th_${files[0]}"
date: ${new Date().toISOString()}
---`;
}
content+= '\n\n';
content+= files
.map(file => `{{< photo full="${file}" thumb="_th_${file}" >}}`)
.join('\n');
content+= '\n';
fs.writeFileSync(`content/${dir}/_index.md`, content, {encoding : 'utf8'});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment