Socket
Socket
Sign inDemoInstall

permalinks

Package Overview
Dependencies
55
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    permalinks

Adds permalink or URL routing/URL rewriting logic to any node.js project. Can be used in static site generators, build systems, web applications or anywhere you need to do path transformation or prop-string replacements.


Version published
Weekly downloads
24
decreased by-33.33%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Adds permalink or URL routing/URL rewriting logic to any node.js project. Can be used in static site generators, build systems, web applications or anywhere you need to do path transformation or prop-string replacements.

Install

Install with npm:

$ npm install --save permalinks

Usage

You can add permalinks to any JavaScript project using node's require() system with the following line of code:

var permalinks = require('permalinks');

To create a permalink, pass a structure (strong) with placeholders (like :prop) to replace, and the file with path information to use:

var structure = ':category/:name/index.html';
var file = {path: 'src/about.hbs'};
var locals = {category: 'blog'};

console.log(permalinks(structure, file, locals));
//=> 'blog/about/index.html'
Constructor usage

The main export can be used as a constructor function. If you need to register helpers or use any of the Permalinks methods, you will need to first create an instance of Permalinks.

var Permalinks = require('permalinks');

var options = {};
var permalinks = new Permalinks(options);
var file = {path: 'src/about.hbs'};

console.log(permalinks.format(':stem/index.html', file));
//=> 'about/index.html'

Files

File conventions

For convenience, files can be defined as a string or an object. If defined as a string, permalinks will convert the filepath to the file.path property of an object.

In other words, both of the following paths:

var file = 'a/b/c.md';
var file = {path: 'a/b/c.md'};

...will convert to {path: 'a/b/c.md'}.

File path properties

Values on the provided file object are used to resolve placeholders in the permalink structure. File values can be overridden by locals or helpers.

As long as a file is provided with at least a file.path property, most of the following built-in file variables will be automatically available on the context.

variabledescription
file.cwdGets and sets current working directory. Will always be normalized and have trailing separators removed. Throws when set to any value other than non-empty strings.
file.baseGets and sets base directory. Used for created relative paths. When null or undefined, it simply proxies the file.cwd property. Will always be normalized and have trailing separators removed. Throws when set to any value other than non-empty strings or null/undefined.
file.pathGets and sets the absolute pathname string or undefined. This value is always normalized and trailing separators are removed. Throws when set to any value other than a string.
file.relativeGets the result of path.relative(file.base, file.path). This is a getter and will throws if set or when file.path is not set.
file.dirnameGets and sets the dirname of file.path. Will always be normalized and have trailing separators removed. Throws when file.dirname is not exlicitly defined and/or file.path is not set.
file.basenameGets and sets the basename of file.path. Throws when file.basename is not exlicitly defined and/or file.path is not set.
file.stemGets and sets stem (filename without suffix) of file.path. Throws when file.stem is not exlicitly defined and/or file.path is not set.
file.nameAlias for file.stem
file.extnameGets and sets extname of file.path.

Data

TODO

locals

TODO

file.data

TODO

options.data

TODO

Helpers

Helper functions can be used to resolve placeholders in permalink structures. For example:

var url = permalinks.format(':foo', {path: 'about.hbs'});

Registering helpers

Helpers are registered using the permalinks.helper() method.


permalinks.helper('foo', function() {

});
Helper example

Use a date helper to dynamically generate paths based on the date defined in YAML front matter of a file.

var moment = require('moment');
var Permalinks = require('permalinks');
var permalinks = new Permalinks();

var file = {
  path: 'src/about.hbs',
  data: {
    date: '2017-02-14'
  }
};

// "file.data" is merged onto "this.context" 
permalinks.helper('date', function(format) {
  return moment(this.context.date).format(format || 'YYYY/MM/DD');
});

console.log(permalinks.format(':date/:stem/index.html', file));
//=> '2017/02/14/about/index.html'

Helpers can also optionally take arguments:

console.log(permalinks.format(':date("YYYY")/:stem/index.html', file));
//=> '2017/about/index.html'

See the helper unit tests for more examples.

file helper

A special built-in file helper is called on every file and then removed from the context before rendering.

permalinks.helper('file', function(file, data, locals) {
  // do stuff with file, data and locals
});

This is useful for modifying the context or setting properties on files before generating permalinks.

`file` helper example

Use the file helper to increment a value for pagination or something similar:

var file = new File({path: 'foo/bar/baz.hbs'});
var permalinks = new Permalinks();
var count = 0;

permalinks.helper('file', function(file, data, locals) {
  data.num = ++count;
});

console.log(permalinks.format(':num-:basename', file));
//=> '1-baz.hbs'
console.log(permalinks.format(':num-:basename', file));
//=> '2-baz.hbs'
console.log(permalinks.format(':num-:basename', file));
//=> '3-baz.hbs'
console.log(count);
//=> 3

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. MIT


This file was generated by verb-generate-readme, v0.4.2, on February 15, 2017.

Keywords

FAQs

Last updated on 15 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc