Skip to content

Instantly share code, notes, and snippets.

@nhoizey
Created March 11, 2020 16:48
Show Gist options
  • Save nhoizey/4a43cb465864e794895c77455e255965 to your computer and use it in GitHub Desktop.
Save nhoizey/4a43cb465864e794895c77455e255965 to your computer and use it in GitHub Desktop.
Node.js script to move Markdown files from previous Jekyll to new Eleventy setup
#!/usr/bin/env node
// Move Markdown files from previous Jekyll to new Eleventy setup
// Jekyll: 2018/06/15-users-do-change-font-size/2018-06-15-users-do-change-font-size.md
// Eleventy: 2018/06/15/users-do-change-font-size/index.md (with date added as YFM)
const fs = require('fs');
const path = require('path');
const insertLine = require('insert-line');
if (process.argv.length <= 2) {
console.log("Usage: " + __filename + " path/to/directory");
process.exit(-1);
}
var rootDir = process.argv[2];
fs.readdirSync(rootDir).forEach(year => {
if (year.match(/^[0-9]{4}$/)) {
fs.readdirSync(path.join(rootDir, year)).forEach(month => {
if (month.match(/^[0-9]{2}$/)) {
let fullMonthDir = path.join(rootDir, year, month);
fs.readdirSync(fullMonthDir).forEach(contentDir => {
if (parts = contentDir.match(/^([0-9]{2})-(.*)$/)) {
console.log(parts);
let day = parts[1];
let contentSlug = parts[2];
let fullDayDir = path.join(fullMonthDir, day);
let fileDest = path.join(fullDayDir, contentSlug, 'index.md');
if (!fs.existsSync(fullDayDir)) {
fs.mkdirSync(fullDayDir);
}
fs.renameSync(path.join(fullMonthDir, contentDir), path.join(fullDayDir, contentSlug));
fs.renameSync(path.join(fullDayDir, contentSlug, `${year}-${month}-${day}-${contentSlug}.md`), fileDest);
insertLine(fileDest).contentSync(`date: ${year}-${month}-${day} 12:00:00 +02:00`).at(3);
}
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment