Skip to content

Commit

Permalink
Merge pull request #34 from jarodtaylor/master
Browse files Browse the repository at this point in the history
Add a tagsCollection object holding a display & url safe tag label
  • Loading branch information
hswolff authored Sep 14, 2016
2 parents 6fb6dc0 + a316813 commit 2065d2a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ metalsmith

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.

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

It is possible to use `opts.metadataKey` for defining the name of the global tag list.
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function plugin(opts) {
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 @@ -98,6 +99,10 @@ function plugin(opts) {
// 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();
Expand All @@ -108,6 +113,9 @@ function plugin(opts) {
// 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)});

// Add each tag to our overall tagList and initialize array if it
// doesn't exist.
if (!tagList[tag]) {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('metalsmith-tags', function() {
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());
done();
});
});
Expand Down

0 comments on commit 2065d2a

Please sign in to comment.