Last active
January 1, 2019 08:41
-
-
Save kahlil/04ce691aca353f448a1806f49c6655b1 to your computer and use it in GitHub Desktop.
Micro.blog import script
This file contains 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 { 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