Skip to content

Instantly share code, notes, and snippets.

@omgmog
Last active February 4, 2019 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omgmog/900d813ee37bb541574ef28566cfc7a5 to your computer and use it in GitHub Desktop.
Save omgmog/900d813ee37bb541574ef28566cfc7a5 to your computer and use it in GitHub Desktop.
Port Jekyll posts to Grav CMS
const fs = require('fs');
const glob = require('glob');
const fm = require('front-matter');
const path = require('path');
const write = require('write');
glob("!(node_modules)/**/*.md", {}, (err, files) => {
files.forEach((file) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) throw err;
let content = fm(data);
let fname = path.basename(file);
let fdate = fname.substring(0,10);
let slug = fname.substring(11).split('.md')[0];
// take the date from the filename and stick it in our yaml object
content.attributes.date = fdate;
// manually create the lines of our new md file with frontmatter
let lines = [];
lines.push('---');
Object.keys(content.attributes).forEach(key => {
let value = content.attributes[key];
if (typeof value === 'string') {
// tidy up titles that contain double quotes as we're gonna wrap them in the output
value = value.replace(/\"/g, '"');
}
lines.push(`${key}: "${value}"`);
});
lines.push('---');
lines.push(content.body);
// post.md because we're using a template called post.twig.html in Grav
write(`blog/${slug}/post.md`, lines.join('\n'), (err) => {
if (err) throw err;
console.log(`Wrote file: ${slug}/post.md`);
});
});
});
});
@omgmog
Copy link
Author

omgmog commented Oct 11, 2017

Stick it in your _posts directory, install the dependencies (npm install glob front-matter write) and then run it node jekyll-to-grav.js, it will create Grav compatible files in blog/, which can then be uploaded. If you use a different filename for your blog post template in Grav, change post.md to [whatever].md in this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment