対象
- AtomでMarkdown形式の文書を書くのにMarkdown-Writer for Atomを使用している。
- 上記プラグインの機能からHexoのタグ/カテゴリ/ポストを参照したい。
- 公式サイトのページで紹介(Settings for Front Matters)されている、hexo-generator-atom-markdown-writer-metaはHexoがversion 3.xでAPIが変わった経緯で動かない。
という人向けのエントリです。
実装
以下のスクリプトを scripts/meta.js
に配置します。これで http://exmaple.net/meta.json
でアクセスできるようになります。
meta.js
// See: https://github.com/zhuochun/md-writer/wiki/Settings-for-Front-Matters
// Path: scripts/meta.js
// Author: Masayuki Higashino
// License: MIT
hexo.extend.generator.register('meta', function(locals) {
var meta = {
tags: [],
posts: [],
categories: []
}
locals.tags.sort('name').each(function(tag) {
meta.tags.push(tag.name)
})
locals.categories.sort('name').each(function(category) {
meta.categories.push(category.name)
})
locals.posts.sort('name').each(function(post) {
meta.posts.push({
title: post.title,
url: encodeURI(post.permalink),
date: post.updated.toDate().toISOString() || post.date.toDate().toISOString()
})
})
return {path: 'meta.json', data: JSON.stringify(meta)}
}
Gistはこちらにあります。