npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dsfm

v0.1.3

Published

delimiter seperated front matter parser / parser creator

Downloads

5

Readme

dsfm

Parse documents containing delimiter-separated front-matter

Build Status CircleCI Coverage Status

Install

$ npm install --save dsfm

Usage

The simplest use case is to parse documents containing yaml front-matter:

const parser = require('dsfm').yaml;

var post = parser('---\ntitle: abc\n---\npost');
// => {attributes: {title: 'abc'}, body: 'post'}

or json front-matter:

const parser = require('dsfm').json;

const post = jsonfm('{{{\ntitle: abc\n}}}\npost');
// => {attributes: {title: 'abc'}, body: 'post'}

This can also generate custom document parsers, and parse streams of text (see below).

Note: So far as I know, there is no specification for front-matter (yaml or otherwise). This modules 'spec' defines front-matter as a block of text at the beginning of a string seperated by two delimiters, each on their own line. The opening delimiter must be the first thing in the string. The closing delimiter must be immediately followed by a newline character (or the end of the string).

API

dsfm(delimiters, [loader])

Constructs a new front-matter parser. See parser documentation below.

delimiters

Type: string|array

The delimter(s) used for front-matter. Where both delims are the same you can use a string, such as '---'. If the opening and closing delims are different, use an array such as ['{{{', '}}}']

Note: The string(s) you provide will have any RegExp tokens escaped internally, so if you need to use backslashes inside delimiters, they must be escaped with another backslash. For example, the delimiter \fm\ should be supplied as \\fm\\

loader

Type: function

An optional function to transform the front-matter after it is extracted. For example, dsfm.yaml() uses js-yamls's safeload and dsfm.json() uses JSON.parse().

parser(doc)

doc

Type: string

Parses the supplied doc and returns a content object of form { attributes: ..., body: ... }. If no front-matter is present, arrtibutes will be null and body body will contain the original string. Where the whole document is a front-matter block, attributes will be populated and the content will be an empty string ''.

parser.test(doc)

doc

Type: string

Test for the presence of front-matter in doc

parser.through()

Returns a through / transform stream for parsing streaming documents. The stream will emit a single attributes event then start emitted data containing the document body.

var fs = require('fs');
var yamlfm = require('dsfm').yaml;

var post = fs.createReadStream('./post.md')
      .pipe(yaml.through());

post.on('attributes', function(attr) {
  console.log(attr);
});

If no front-matter is present, the attributes payload will be null.

dsfm.yaml

A parser for strings containing yaml front-matter, delimited by '---' and '---'. Generated internally using dsfm('---', yaml.safeLoad). It also implements the test(doc) and through() methods.

dsfm.json

A parser for strings containing json front-matter, delimited by '{{{' and '}}}'. Generated internally using:

dsfm(['{{{', '}}}'], function (str) {
  return JSON.parse('{' + str + '}');
});

It also implements the test(doc) and through() methods.

Note: json front-matter is a set of object key-value pairs. Values may themselves be objects or arrays nested however deeply, but at the top level, it must be an object (not an array).

License

MIT © 2016-2107, axdg ([email protected])