- はまったこと
<content type="text/x-markdown">
を指定してもplain textで投稿されてしまう
- 対策
- ブログの設定画面からデフォルトの編集モードをMarkdownモードに設定する。
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)
//http.get(APIURL + '/entry', {auth: KEY}, (res) => {
// let data = ''
// res.on('data', (chunk) => data += chunk)
// res.on('end', () => console.log(data))
//})
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,
//headers: {
// 'Content-Length': Buffer.byteLength(xml)
//}
},
(res) => {
console.log(res.statusCode)
}
)
req.on('error', (e) => console.error('hatenaPost error: ' + e))
req.write(xml, 'utf8')
req.end()