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

snippable

v0.1.1

Published

Parse human-writable multipart documents

Downloads

249

Readme

snippable.js

Snippable is a human-writable multipart document format. The main goal of Snippable is to be able to add structured metadata to an unstructured document while keeping it in a single file that a non-technical person can edit.

Part formats

The different parts of a Snippable document are each in a specific format. Snippable does not prescribe what format to use for the document parts, only how to include and separate them. Human-writable and readable formats such as Markdown and YAML are preferred, but not mandatory.

Separator

In a Snippable document, the different parts are separated by the following separator, on its own line of text.

-8<--------------

There may be arbitrary white space around this separator. The number of dashes can be anything above 2, so the following are equivalent:

-8<--
-8<--------------------------------------

Escaping the separator characters

As the separator needs to be on its own line, so you should rarely if ever need to escape it. It should usually be enough to add one or more spaces in front of the sequence so that it ceases to be interpreted as a separator.

Specifying part formats

Formats are usually specified through the file extensions. For example, a document.yaml.md file has a YAML header followed by a Markdown section.

It is also possible to specify the formats from the document itself, embedded in the separator. Formats specified this way take precedence over formats specified through other means. The separator can contain format specifications for the part before them, the part after them, or both:

-8<--^-yaml--------
-8<--v-md----------
-8<--^-yaml--v-md--

Typical Snippable file

A typical file consists in a YAML header, and a Markdown document. For that reason, if no formats are specified or known, YAML and Markdown are assumed for the first two parts of a document.

-document.yaml.md-

Title: A simple snippable document
Author: Bertrand Le Roy
Tags: snippable, yaml, markdown, multipart

-8<-----------------------------------------------------

A Snippable Document
====================

This is what a snippable document looks like.
This document has two parts:

* a YAML header
* this Markdown document

It should not look too terrible to a regular Markdown parser
and can be parsed to extract the header.

Using the library

First, add the library to your project:

npm install snippable --save

You can then require the library and parse files using it:

var Snippable = require('snippable');
var fs = require('fs');

var snippable = new Snippable();
var path = 'docs/document.yaml.md';
fs.readFile(path, function(err, data) {
  var parts = snippable.parse(data, path);
  var header = parts[0];
  var body = parts[1];
  // Do something with the header and body.
});

Adding a parser

If a format is not recognized, the part will be returned as an unparsed string. The library comes with parsers for JSON and YAML (Markdown is often assumed but its parsing must be done by your own code: it will be handed out as a string).

You may add your own parsers:

snippable.registerParser('csv', function(text) {
  var lines = splitLines(text);
  var parsed = lines.map(function(line) {
    return splitOnCommas(line);
  })
  return parsed;
});

License

Both the JavaScript library and the format specification are released under MIT license, which allows you to do pretty much as you like with it, including commercial and open-source applications.