Last active
February 4, 2019 09:05
Port Jekyll posts to Grav CMS
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
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`); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stick it in your
_posts
directory, install the dependencies (npm install glob front-matter write
) and then run itnode jekyll-to-grav.js
, it will create Grav compatible files inblog/
, which can then be uploaded. If you use a different filename for your blog post template in Grav, changepost.md
to[whatever].md
in this script.