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

Add a tagsCollection object holding a display & url safe tag label #34

Merged
merged 2 commits into from
Sep 14, 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
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