mongoose-slug-maker
Mongoose plugin for creating slugs based on mongoose schema fields. For example you can create a slug based on a document's title and author's name: my-post-title-kevin-roosevelt, or unique slugs based on just the title: my-post-title-Nyiy4wW9l. support persian ( farsi ) , Arabic and other language url slug
Optimize
Installation
The best way to install it is using npm
npm install mongoose-slug-maker --save
Loading
var slug = ;
Initialization
var mongoose = ;mongoose;
Usage
This plugin is based on the idea of using the mongoose schema as the way to check the use of slug fields.
The plugin checks and updates automatically the slug field with the correct slug.
Basic Usage
If you only want to create the slug based on a simple field.
var mongoose = slug = mongoose Schema = mongooseSchema schema = title: String slug: type: String slug: "title" ;
Multiple slug fields
You can add as many slug fields as you wish
var mongoose = slug = mongoose Schema = mongooseSchema schema = title: String subtitle: String slug: type: String slug: "title" slug2: type: String slug: "title" slug3: type: String slug: "subtitle" ;
Multiple fields to create the slug
If you want, you can use more than one field in order to create a new slug field.
var mongoose = slug = mongoose Schema = mongooseSchema schema = title: String subtitle: String slug: type: String slug: "title" "subtitle" ;
Unique slug field
To create a unique slug field, you must only add add the unique: true parameter in the path (also, this way the default mongo unique index gets created)
var mongoose = slug = mongoose Schema = mongooseSchema schema = title: String subtitle: String slug: type: String slug: "title" "subtitle" unique: true ;
If unique is set, the plugin searches in the mongo database, and if the slug already exists in the collection, it appends to the slug a separator (default: "-") and a random string (generated with the shortid module).
example random
mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you' mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you-Nyiy4wW9l' mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you-NJeskEPb5e'
Alternatively you can modify this behaviour and instead of appending a random string, an incremental counter will be used. For that to happen, you must use the parameter slug_padding_size specifying the total length of the counter:
example counter
var mongoose = slug = mongoose Schema = mongooseSchema schema = title: String subtitle: String slug: type: String slug: "title" "subtitle" slug_padding_size: 4 unique: true ; mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you' mongoose // slug -> 'سلام-دنیا' mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you-0001' mongoose // slug -> 'am-i-wrong-fallin-in-love-with-you-0002'
Choose your own options
You can change any options adding to the plugin
var mongoose = slug = options = separator: "-" lang: "en" truncate: 120 mongoose Schema = mongooseSchema schema = title: String subtitle: String slug: type: String slug: "title" "subtitle" unique: true ;
You can find more options in the speakingURL's npm page
Support
This plugin is proudly supported by Kubide