Skip to content

Instantly share code, notes, and snippets.

@jankal
Created November 26, 2020 19:05
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 jankal/f039145d6285efb001bd60304e0a3445 to your computer and use it in GitHub Desktop.
Save jankal/f039145d6285efb001bd60304e0a3445 to your computer and use it in GitHub Desktop.
Need to rename a lot of markdown files to slugs? - Worry no more.
const fs = require('fs');
const path = require('path');
const slugify = require('slugify');
const directory = './content/articles';
const files = fs.readdirSync(path.resolve(__dirname, directory));
for (const file of files) {
if (file.endsWith('.md')) {
fs.rename(
path.resolve(__dirname, directory, file),
path.resolve(
__dirname,
directory,
slugify(file.substr(0, file.length - 3), {
lower: true,
strict: true,
locale: 'en'
}) + '.md'
),
(err) => {
console.error(err);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment