0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

metalsmithでmetadataを一括指定する方法

Last updated at Posted at 2017-11-14

静的サイトジェネレータ(ここではmetalsmith)というのはそこまで頻繁にビルド用のコードを書かないせいか、どうしても毎回同じところでハマったりする。ここではmetadataを一括指定する方法を考える。

私が多く採用しているのはmetalsmith-collectionsでcollectionsを作成し、metalsmith-collection-metadataでmetadataを追加するというパターン。

metalsmith-collections について

metalsmith-collectionsというのはmetalsmithではかなりよく使う重要なプラグインで、他のプラグインがこのプラグインに依存していたり、あるいは追加した collections を利用したりするものがわりとよくある。metalsmith-collection-metadataもそうしたプラグインのうちの一つ。

metalsmith-collectionsがどういうものかというのはREADMEを見てもらえればわかると思うので、詳細な解説はここではしないが、簡単に説明すると、blogのpostsのようなものを作るときのために使うような日付順によるソートや、その前後記事のデータなどを含めたデータ一式を揃えてくれるものという認識でだいたい問題ないと思う。

blogでいうとpagerであるmetalsmith-paginationというプラグインもmetalsmith-collectionsに依存しているので、metalsmith-collectionsはそうしたある一連のコンテンツを生成するために重要なプラグインであることがわかる。

metalsmith-collectionsにはmetadataを追加する機能がある用に見えるが、以下の二点に注意する必要がある

  • metadataはcollectionsに追加されるデータにのみ追加される
  • metadataはすべて metadata というkey以下に設定される

つまり、blogの記事それぞれにmetalsmith-layout用の layout: post.pug のようなmetadataを追加するような用途では使うことができない。

metalsmith-collection-metadata

metalsmith-collection-metadataはその名の通り、作成したcollectionを指定してmetadataを追加するプラグイン。

このプラグインは指定したcollectionを持つすべてのファイルに対してmetadataを追加する。

他の一括メタデータ追加プラグイン

公式ではmetalsmith-writemetadataをプッシュしてるけど、metadataの追加という観点のみで考えると機能過多で(テストもないし)あんまり好きではない。試してはないけどもcollectionsの特定collectionをjsonとしてoutputできるような設定があるので、その機能がほしい場合では便利なんだと思う。

metadataの追加だけで使うのであればmetalsmith-filemetadataなんだけど、metalsmith-collectionsを使うにしてもmetalsmith-branchを使うにしてもpatternの設定がいずれも重複するのがちょっとモヤモヤっとする。

大体一括でメタデータを指定したいケースというのはその一括指定したいファイル群でcollectionを作ることがほとんどので、metalsmith-collection-metadataがまず最優先で、もしそうでない場合の候補としてmetalsmith-filemetadataというのが今のところ自分としてはベストの解。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?