Common Middleware

1.0.0 · maintenance · verified Wed Apr 22

common-middleware is a JavaScript utility library that provides a collection of reusable middleware functions designed for applications built upon the `base-methods` framework, particularly for projects like Assemble, Verb, Generate, and Update. It currently stands at version 1.0.0, last updated over two years ago, indicating a maintenance phase rather than active development. The library focuses on processing file objects in a pipeline, offering functionalities such as front-matter parsing, escaping and unescaping template delimiters, and managing JSON data on load and pre-write. Its key differentiator is its integration within the `base` and `assemble` ecosystem, providing standardized pre-processing capabilities for file-based templating and content management workflows common in static site generators or documentation builders. It is specifically designed for Node.js environments, historically supporting versions `0.10.0` and above, and primarily uses CommonJS modules.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `common-middleware` and register its suite of file processing functions with an `assemble-core` application. Once registered, it automatically handles tasks like front-matter parsing, template escaping, and JSON file processing within the application's file pipeline.

const middleware = require('common-middleware');
const assemble = require('assemble-core'); // Install via: npm install assemble-core

// Create your application instance (e.g., from assemble-core, verb, generate)
const app = assemble();

// Register the common middleware suite with the application.
// This attaches the various hooks (onLoad, preWrite, etc.) to the app's lifecycle.
app.use(middleware());

console.log('common-middleware registered with the application.');

// Now, as files are processed by 'app' (e.g., through app.src(), app.render(), app.dest()),
// this middleware will automatically intervene to perform tasks like:
// - Parsing YAML/JSON front-matter from files into `file.data`
// - Escaping or unescaping template delimiters (e.g., `<%= foo %>`)
// - Handling JSON parsing and stringifying for files matching `options.jsonRegex`

// Example of how options can be passed (optional):
// app.use(middleware({ jsonRegex: /\.(json|data)$/, extRegex: /\.(md|html|hbs)$/ }));

view raw JSON →