Skip to content

Instantly share code, notes, and snippets.

@jimkang
Last active August 16, 2023 03:20
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 jimkang/82769e2d58c26ddd47517f9ad199e1f6 to your computer and use it in GitHub Desktop.
Save jimkang/82769e2d58c26ddd47517f9ad199e1f6 to your computer and use it in GitHub Desktop.
Create html indexes for images in a folder. Classic! Puts the index files in a directory named `indexes`; you may want to change that.
#!/usr/bin/env node
/* global process */
var fs = require('fs');
var imgFileExts = ['png', 'jpg'];
if (process.argv.length < 3) {
console.error(
'Usage: node make-index.js <directory with images>'
);
process.exit(1);
}
const imgDirPath = process.argv[2];
var files = fs.readdirSync(imgDirPath).filter(probablyAnImageFile);
const imagesPerPage = 48;
const pageCount = Math.ceil(files.length/imagesPerPage);
console.log('Need to make', pageCount, 'pages');
var pages = [];
for (let page = 0; page < pageCount; ++page) {
pages.push(files.slice(page * imagesPerPage, (page + 1) * imagesPerPage));
}
pages.forEach(writePage);
function writePage(pageFiles, i) {
console.log('Writing', pageFiles.length, 'to page', i);
const html = `<html>
<head>
<title>Images page ${i}</title>
</head>
<body>
<ul>
${pageFiles.map(file => '<li><img src="' + file + '"></li>').join('\n')}
</ul>
<a href="page-${i + 1}.html">Next page: ${i + 1}</a>
</body>
</html>`;
fs.writeFile(`indexes/page-${i}.html`, html, { encoding: 'utf8' }, console.error);
}
function probablyAnImageFile(file) {
return imgFileExts.some((ext) => file.endsWith('.' + ext));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment