- はまったこと
<content type="text/x-markdown">
を指定してもplain textで投稿されてしまう
- 対策
const http = require('https')
const fs = require('fs')
const process = require('process')
const matter = require('gray-matter')
const escapeHtml = require('escape-html')
const USER = 'hoge'
const APIKEY = 'fuga'
const APIURL = 'https://blog.hatena.ne.jp/$user/$user_blog_url/atom'
const KEY = USER + ":" + APIKEY
const post = fs.readFileSync(process.argv[2], 'utf8')
const gray = matter(post)
const date = (new Date())).toLocaleString()
const category = gray.data.category.split(/\s+/).map(c => `<category term="${c}" />`).join('\n')
const xml = `\
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app">
<title>${gray.data.title}</title>
<author><name>bercriber</name></author>
<content type="text/x-markdown">
${escapeHtml(gray.content)}
</content>
<updated>${date}</updated>
${category}
</entry>
`
let req = http.request(
APIURL + '/entry',
{
method: 'POST',
auth: KEY,
},
(res) => {
console.log(res.statusCode)
}
)
req.on('error', (e) => console.error('hatenaPost error: ' + e))
req.write(xml, 'utf8')
req.end()