Skip to content

Instantly share code, notes, and snippets.

@kahlil
Last active January 1, 2019 08:41
Show Gist options
  • Save kahlil/04ce691aca353f448a1806f49c6655b1 to your computer and use it in GitHub Desktop.
Save kahlil/04ce691aca353f448a1806f49c6655b1 to your computer and use it in GitHub Desktop.
Micro.blog import script
const fs = require('fs');
const { promisify } = require('util');
const matter = require('gray-matter');
const got = require('got');
const queryString = require('query-string');
const readFile = promisify(fs.readFile);
const readDir = promisify(fs.readdir);
(async () => {
try {
const files = await readDir('posts');
console.log({files});
files.forEach(async (fileName) => {
const str = await readFile(`posts/${fileName}`, 'utf8');
const { data, content } = matter(str);
if (data.draft) { return; }
const headers = {
Authorization: 'Bearer <YOUR_AUTH_TOKEN>'
};
const body = `h=entry&${queryString.stringify({
content,
published: data.date,
name: data.title
})}`;
const res = await got.post('https://micro.blog/micropub', {
headers,
body
});
console.log(res);
});
} catch (e) {
console.log({e})
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment