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

@thefarce/loom

v1.0.2

Published

A module for weaving form data with content data

Downloads

59

Readme

@thefarce/loom

What is this?

Loom is a tool for weaving form data with content data in nodejs. It does this by using elements of the Unified.js toolkit for parsing text into abstract syntax trees (AST).

The fundamental idea of Loom is that presentation and content are orthogonal dimensions of data. A communication has values in these two dimensions integrated into a coherent whole.

When should I use this?

When you want to blend form (such as HTML) with content (such as JSON) into a single document. Unlike other systems, Loom does not require writing to or reading from files.

Installation

$ npm install @thefarce/loom

Usage Example

For more thorough usage examples, see the examples directory and refer to the transformers documentation and the interpolation documentation.

Simple Example

In this simple example, we will make two changes. First, we will replace the strong tag in our html fragment with a em tag. Second, we will replace the name variable in our template with the value Othello.

import { interpolateNode, transform } from '@thefarce/loom'
import { astToHtml, htmlToAst } from '@thefarce/loom-html5'

// Get our HTML fragment.
const ast = htmlToAst('<p>Hello, <strong>${name}</strong>!</p>', { fragment: true });

// Now we have an abstract syntax tree (AST) representation of our HTML
// fragment.  First, let's transform the <strong> tag to an <em> tag.

let updated = transform(
	// our initial syntax tree
  ast,
	// A transformer that checks if the node is an element and has a tag name
	// of "strong".  If so, we just change the tag name to "em".  It is
	// important that we return the updated node (or a new one).
	(node, context, next) => {
        if (node.type === 'element' && node.tagName === 'strong') {
          node.tagName = 'em';
          return { node, context };
		}
        next();
	},
);

// Now let's interpolate the value `name`.  There is another major mechanism
// for providing interpolation values; see the for more detailed information.
let interpolated = interpolateTree(
	updated, 
	{ name: 'Othello' }
);

console.log(astToHtml(interpolated));

Running this script produces the following output:

<p>Hello, <em>Othello</em>!</p>

API

Value Interpolation

Interpolation is the process of executing code within the AST's values. This uses a syntax identical to JavaScript's "backtick" interpolation. For example, in the following script, JavaScript will replace {name} with the value Othello:

See the interpolation documentation for more detailed information.

const name = 'Othello';
console.log(`Hello, ${name}!`);

This interpolation uses variables in scope to expand values. Loom interpolation works the same way: any string[^1] in the AST will be replaced with the value in the data.

[^1]: There are a few exceptions. The type, data, and position values of a node will not be interpolated. You may interpolate them manually, of course.

AST Transformation

Transformation is the process of making systematic changes to an AST. This is accomplished by creating "transformer functions" (or "transformers") that apply changes to a node based on its content.

See the transformations documentation for more detailed information.

Glossary

ast

The AST to be transformed. This can be any AST that conforms to the Unified.js specification.

Contributing to Loom

Contributions to Loom are welcome.

Community Guidelines

Contribute good code with full tests and docs, then submit a pull request. I don't care about any of your other behaviors.

VIM

This project contains a .vimrc file to help with development and maintainance. See that file for more information and instructions for use.