Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine tagsUrlSafe and tagsCollection into one property #38

Merged
merged 3 commits into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ metalsmith

This will generate `topics/[tagname].html` pages in your `build` directory with array of `pagination.files` objects on which you can iterate on. You can use `tag` for tag name in your layouts. (You can refer to tests folder for tags layout.)

The `tags` property on your pages will remain but it will be modified to an array of String containing the tags.

There will also be a `tagsUrlSafe` array created that will contain an array of url safe tag names for use in url creation.

There will also be a `tagsCollection` array created that will have an object containing the `urlSlug` and `display` properties of each tag.
The `tags` property on your pages will remain, but it will be modified to an array of objects containing a `name` and `slug` property for each tag.

You can use `metalsmith-permalink` to customize the permalink of the tag pages as you would do with anything else.

Expand Down
17 changes: 1 addition & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var slug = require('slug');

/**
Expand All @@ -18,8 +17,6 @@ function plugin(opts) {
opts.pathPage = opts.pathPage || 'tags/:tag/:num/index.html';
opts.layout = opts.layout || 'partials/tag.hbt';
opts.handle = opts.handle || 'tags';
var handleUrlSafe = opts.handle + 'UrlSafe';
var handleCollection = opts.handle + 'Collection';
opts.metadataKey = opts.metadataKey || 'tags';
opts.sortBy = opts.sortBy || 'title';
opts.reverse = opts.reverse || false;
Expand Down Expand Up @@ -96,25 +93,13 @@ function plugin(opts) {
// Re-initialize tag array.
data[opts.handle] = [];

// Add url safe version of every tag as well.
data[handleUrlSafe] = [];

// Add an object containing the display and
// safe url version of every tag
data[handleCollection] = [];

tagsData.forEach(function(rawTag) {
// Trim leading + trailing white space from tag.
var tag = String(rawTag).trim();

// Save formatted tag data.
data[opts.handle].push(tag);

// Save url safe formatted tag data.
data[handleUrlSafe].push(safeTag(tag));

// Save url safe formatted and display versions of tag data
data[handleCollection].push({ display: tag, urlSlug: safeTag(tag)});
data[opts.handle].push({ name: tag, slug: safeTag(tag)});

// Add each tag to our overall tagList and initialize array if it
// doesn't exist.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metalsmith-tags",
"version": "1.2.0",
"version": "1.3.0",
"description": "A metalsmith plugin to create dedicated pages for tags in posts or pages.",
"main": "lib/index.js",
"scripts": {
Expand Down
7 changes: 2 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ describe('metalsmith-tags', function() {
}))
.build(function(err,files){
if (err) return done(err);
assert.equal(files['index.html'].tags.toString(),['hello', 'world', 'this is', 'tag'].toString());
assert.equal(files['index.html'].tagsUrlSafe.toString(),['hello', 'world', 'this-is', 'tag'].toString());
assert.equal(files['index.html'].tagsCollection.toString(),[{ display: 'hello', urlSlug: 'hello'}, { display: 'world', urlSlug: 'world'}, { display: 'this is', urlSlug: 'this-is'}, { display: 'tag', urlSlug: 'tag'}].toString());
assert.equal(files['index.html'].tags.toString(),[{ name: 'hello', slug: 'hello'}, { name: 'world', slug: 'world'}, { name: 'this is', slug: 'this-is'}, { name: 'tag', slug: 'tag'}].toString());
done();
});
});
Expand Down Expand Up @@ -159,8 +157,7 @@ describe('metalsmith-tags', function() {
}))
.build(function(err, files) {
if (err) return done(err);
assert.equal(files['index.html'].tags.toString(),['hello', 'world', 'this is', 'tag'].toString());
assert.equal(files['index.html'].tagsUrlSafe.toString(),['HELLO', 'WORLD', 'THIS IS', 'TAG'].toString());
assert.equal(files['index.html'].tags.toString(),[{ name: 'hello', slug: 'HELLO'}, { name: 'world', slug: 'WORLD'}, { name: 'this is', slug: 'THIS IS'}, { name: 'tag', slug: 'TAG'}].toString());
done();
});
})
Expand Down