This module does something similar to URL rewriting, but for static paths at build-time. The goal is to consistently and easily generate correct destination paths during development, regardless of the source paths.
Examples
Let's say we have a blog, with post titled "How To Create Effective Permalinks", and we want to:
- automatically write the post to the root of our site
- use the slugified
title
from front-matter as the folder name (for "pretty" permalinks) - append
/index.html
to the path (also for "pretty" permalinks)
In other words, we want this source path:
src/content/posts/2017-02-14.md
To be written to a destination path that looks something like:
blog/how-to-create-effective-permalinks/index.html
You can either manually parse and reformat your destination paths, or use this library with simple rewrite rules.
Example rewrite rule
The following rule(s) will match any files in the posts
directory, and rewrite the path using the given structure.
rewriter.rule(/posts\//, 'blog/:slugify(title)/index.html');
// add extra validation if necessary
rewriter.rule(/posts\//, 'blog/:slugify(title)/index.html', function(file) {
return file.extname === '.md';
});
URL rewriting
URL rewriting is used for replacing semantic, user-friendly URLs with server-friendly URLs.For example, when a user enters a URL like the following to go to a page on wikipedia:
https://en.wikipedia.org/wiki/Business
The URL might be rewritten by wikipedia to something like:
https://en.wikipedia.org/w/index.php?title=Business
Add this library to your JavaScript application with the following line of code:
var Rewriter = require('{%= name %}');
{%= apidocs("index.js") %}