front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter
フロントマター処理ライブラリ
front-mattergray-matterremark-frontmatteryaml-front-matter類似パッケージ:
フロントマター処理ライブラリ

フロントマター処理ライブラリは、Markdownファイルのメタデータを解析し、簡単にアクセスできる形式に変換するためのツールです。これにより、コンテンツ管理や静的サイト生成において、メタデータを効率的に管理することが可能になります。これらのライブラリは、Markdownファイルの先頭にあるYAML形式のメタデータを読み取り、オブジェクトとして返す機能を提供します。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
front-matter4,783,882690-326年前MIT
gray-matter2,816,4244,309-685年前MIT
remark-frontmatter1,911,11930921.2 kB02年前MIT
yaml-front-matter72,395194-215年前MIT
機能比較: front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter

フォーマットサポート

  • front-matter:

    YAML形式のフロントマターを解析し、シンプルなオブジェクトとして返します。

  • gray-matter:

    YAMLとJSONの両方の形式をサポートし、柔軟なデータ処理が可能です。

  • remark-frontmatter:

    MarkdownのフロントマターをRemarkプラグインとして処理し、他のRemarkプラグインと連携できます。

  • yaml-front-matter:

    YAML形式に特化しており、YAMLの構文を正確に解析します。

依存関係

  • front-matter:

    軽量で、他のライブラリに依存しないため、簡単にプロジェクトに組み込むことができます。

  • gray-matter:

    Node.js環境で動作し、他のライブラリとの互換性がありますが、やや重いです。

  • remark-frontmatter:

    Remarkに依存しているため、Remarkを使用するプロジェクトに最適です。

  • yaml-front-matter:

    YAMLパーサーに依存しており、YAMLの処理に特化しています。

使用シナリオ

  • front-matter:

    シンプルなMarkdownファイルのメタデータを扱う静的サイト生成に最適です。

  • gray-matter:

    複雑なメタデータを持つプロジェクトや、YAMLとJSONの両方を使用する場合に適しています。

  • remark-frontmatter:

    Markdownの解析とフロントマターの処理を同時に行いたい場合に便利です。

  • yaml-front-matter:

    YAML形式のメタデータを多く使用するプロジェクトに適しています。

学習曲線

  • front-matter:

    非常にシンプルで、使い方が直感的なため、学習コストが低いです。

  • gray-matter:

    YAMLとJSONの両方を扱うため、少し学習が必要ですが、柔軟性があります。

  • remark-frontmatter:

    Remarkの知識が必要ですが、他のRemarkプラグインとの統合が容易です。

  • yaml-front-matter:

    YAMLの構文に慣れている必要がありますが、特化しているため使いやすいです。

拡張性

  • front-matter:

    基本的な機能に特化しているため、拡張性は限られています。

  • gray-matter:

    柔軟なデータ処理が可能で、他の機能と組み合わせやすいです。

  • remark-frontmatter:

    Remarkのエコシステムに組み込むことで、さまざまなプラグインと連携できます。

  • yaml-front-matter:

    YAMLの特性を活かした拡張が可能ですが、他の形式には対応していません。

選び方: front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter
  • front-matter:

    シンプルなフロントマターの解析が必要な場合に選択してください。軽量で依存関係が少なく、基本的な機能を提供します。

  • gray-matter:

    YAMLとJSONの両方のフォーマットをサポートし、より柔軟なメタデータの処理が必要な場合に選択してください。

  • remark-frontmatter:

    RemarkプラグインとしてMarkdown処理の一環としてフロントマターを扱いたい場合に選択してください。Remarkのエコシステムと統合されているため、他のRemarkプラグインと組み合わせて使用することが容易です。

  • yaml-front-matter:

    YAML形式のフロントマターを特に扱う必要がある場合に選択してください。YAMLのパースに特化しており、YAMLの特性を活かしたメタデータの処理が可能です。

front-matter のREADME

front-matter

build coverage npm github

Sauce Test Status

Extract meta data (front-matter) from documents.

This modules does not do any IO (file loading or reading), only extracting and parsing front matter from strings.

This concept that was originally introduced to me through the jekyll blogging system and is pretty useful where you want to be able to easily add meta-data to content without the need for a database. YAML is extracted from the the top of a file between matching separators of "---" or "= yaml =". It will also extract YAML between a separator and "...".

Install

With npm do:

npm install front-matter

Example

So you have a file example.md:

---
title: Just hack'n
description: Nothing to see here
---

This is some text about some stuff that happened sometime ago

NOTE: As of front-matter@2.0.0 valid front matter is considered to have the starting separator on the first line.

Then you can do this:

var fs = require('fs')
  , fm = require('front-matter')

fs.readFile('./example.md', 'utf8', function(err, data){
  if (err) throw err

  var content = fm(data)

  console.log(content)
})

And end up with an object like this:

{
    attributes: {
        title: 'Just hack\'n',
        description: 'Nothing to see here'
    },
    body: 'This is some text about some stuff that happened sometime ago',
    bodyBegin: 6,
    frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'
}

Methods

var fm = require('front-matter')

fm(string, { allowUnsafe: false })

Return a content object with two properties:

  • content.attributes contains the extracted yaml attributes in json form
  • content.body contains the string contents below the yaml separators
  • content.bodyBegin contains the line number the body contents begins at
  • content.frontmatter contains the original yaml string contents

NOTE: By default fm() uses ys-yaml's safeLoad unless you set allowUnsafe in the options object to true.

fm.test(string)

Check if a string contains a front matter header of "---" or "= yaml =". Primarily used internally but is useful outside of the module.

Returns true or false

    fm.test(string) #=> true || false

Contributing

front-matter is an OPEN Source Project so please help out by reporting bugs or forking and opening pull requests when possible.

standard

All code is linted/formatted using standard style, any non-conforming code can be automatically formatted using the the fmt make task: make fmt.

Maintainers

Contributors

This module is awesome because of all the folks who submitted pull requests:

CHANGELOG

3.0.0

  • CI only tests Node versions >= v6 (drops v4, and v5)

LICENSE (MIT)

Copyright (c) Jason Campbell ("Author")

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.