Skip to content

Instantly share code, notes, and snippets.

@mistic100
Last active May 1, 2021 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistic100/794319529c410e15ecd6325131602d47 to your computer and use it in GitHub Desktop.
Save mistic100/794319529c410e15ecd6325131602d47 to your computer and use it in GitHub Desktop.
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