Last active
March 28, 2024 23:19
Make links to files with specific extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* global process */ | |
var fs = require('fs'); | |
if (process.argv.length < 4) { | |
console.error( | |
'Usage: node make-index.js <comma-separated desired file exts, e.g. mp4,gif> <directory with files>' | |
); | |
process.exit(1); | |
} | |
const fileExts = process.argv[2].split(','); | |
const mediaDirPath = process.argv[3]; | |
var files = fs.readdirSync(mediaDirPath).filter(fileIsWanted); | |
const linksPerPage = 48; | |
const pageCount = Math.ceil(files.length/linksPerPage); | |
console.log('Need to make', pageCount, 'pages'); | |
var pages = []; | |
for (let page = 0; page < pageCount; ++page) { | |
pages.push(files.slice(page * linksPerPage, (page + 1) * linksPerPage)); | |
} | |
pages.forEach(writePage); | |
function writePage(pageFiles, i) { | |
console.log('Writing', pageFiles.length, 'to page', i); | |
const html = `<html> | |
<head> | |
<title>Links page ${i}</title> | |
</head> | |
<body> | |
<ul> | |
${pageFiles.map(file => `<li><a href="${file}">${file}</a></li>`).join('\n')} | |
</ul> | |
<a href="page-${i + 1}.html">Next page: ${i + 1}</a> | |
</body> | |
</html>`; | |
fs.writeFile(`${mediaDirPath}/page-${i}.html`, html, { encoding: 'utf8' }, console.error); | |
} | |
function fileIsWanted(file) { | |
return fileExts.some((ext) => file.endsWith('.' + ext)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment