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

storyfillup

v1.0.0

Published

Simple markup language for text with placeholders

Downloads

2

Readme

StoryFillup

StoryFillup is a simple markup language for writing text with placeholders to be filled in by a user. It was originally designed for "fill in the blank" stories but can be used in other applications.

This package provides a simple parser (generated using Peggy) for the StoryFillup language, which returns an array of text and placeholder objects which can be used by an application.

Usage

import * as StoryFillup from 'storyfillup';

const text = 'This is a {{ description }}. {{ interjection | exclamation }}';
const parsed = StoryFillup.parse(text);
console.log(parsed);

/*
Result:

[
	{ type: 'text', content: 'This is a ' },
	{ type: 'placeholder', description: 'description', special: null },
	{ type: 'text', content: '.' },
	{ type: 'placeholder', description: 'interjection', special: 'exclamation' },
]
*/

Error Handling

An error is thrown if the input has invalid syntax. The error object contains additional information about the error such as the location within the text (refer to the Peggy documentation).

The format method on the error object produces a string with information. This requires a mapping from sources (such as filenames or even objects) to text inputs. The source for parsing is passed as an optional second argument to parse:

import * as StoryFillup from 'storyfillup';

const input = 'This is a {{';
const name = 'example';
try {
	StoryFillup.parse(input, name);
} catch (e) {
	console.log(e.format([{ source: name, text: input }]));
}

/*
Result:

Error: Expected "|", "}}", or placeholder description but end of input found.
 --> example:1:13
  |
1 | This is a {{
  |             ^
*/

The SyntaxError class is exported for applications to handle different types of errors:

import * as StoryFillup from 'storyfillup';

try {
	// ...
} catch (e) {
	if (e instanceof StoryFillup.SyntaxError) {
		// Handle parsing error
	}
	// Handle other error
}

Language Syntax

Placeholders are indicated by double braces ({{ }}). Single braces are allowed outside placeholders, but not inside them. (The restriction on braces inside placeholders may change in the future.)

The text inside the braces is a description followed by an optional "special" code. The special code is separated from the description by a vertical bar (|). This means that the description cannot contain a vertical bar, but the special code can.

For example, the placeholder {{example|with|a|special|code}} has a description of example and a special code of with|a|special|code.

This specification does not mandate any particular interpretation of the description and special code; this is left to the application. The intended use is for the description to serve as a human-readable description of what to fill in, and the special code to serve as a signal to the application for some sort of special processing.

The parser trims leading and trailing whitespace in the description and special code, so {{ x | y }} has a single-character description and a single-character special code. Note, however, that space around any additional vertical bars in the special code is not trimmed: {{ x | y | z }} has a special code of "y | z". Descriptions and special codes may be empty, so {{ }}, {{}}, and {{|}} are all considered placeholders. Applications may apply special processing in these situations (for example, rejecting empty descriptions), but this specification does not require any such processing.

(Note that the last two examples in the previous paragraph are subtly different: {{}} is a placeholder with no special code, while {{|}} is a placeholder with an empty special code. The parser represents the former as null and the latter as an empty string. Applications may choose to treat these two placeholders as identical.)

License

This package is available under the MIT License. Refer to LICENSE.txt for details.