Skip to content

Commit

Permalink
feat(main): interaction category for interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 10, 2015
1 parent 6d11eea commit d626eae
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
25 changes: 22 additions & 3 deletions index.js
Expand Up @@ -12,7 +12,10 @@ var htmlRegexp = /<[^>]+>/g;

var Formatter = function () {};

Formatter.prototype._formatFrontMatter = function (source) {
Formatter.prototype._formatFrontMatter = function (data) {
var source = data.properties;
var derived = data.derived || {};

var target = {
layout: 'micropubpost',
date: source.published[0].toISOString(),
Expand All @@ -37,11 +40,15 @@ Formatter.prototype._formatFrontMatter = function (source) {
}
});

if (derived.category) {
target.category = derived.category;
}

return '---\n' + yaml.safeDump(target) + '---\n';
};

Formatter.prototype._formatContent = function (data) {
return data.content ? data.content + '\n' : '';
return data.properties.content ? data.properties.content + '\n' : '';
};

Formatter.prototype._formatSlug = function (data) {
Expand Down Expand Up @@ -80,11 +87,17 @@ Formatter.prototype.preFormat = function (data) {
data.properties.slug = slug ? [slug] : [];
data.properties.published = [data.properties.published && data.properties.published[0] ? new Date(data.properties.published[0]) : new Date()];

data.derived = {};

if (!_.isEmpty(data.properties['in-reply-to']) || !_.isEmpty(data.properties['like-of'])) {
data.derived.category = 'interaction';
}

return Promise.resolve(data);
};

Formatter.prototype.format = function (data) {
return Promise.resolve(this._formatFrontMatter(data.properties) + this._formatContent(data.properties));
return Promise.resolve(this._formatFrontMatter(data) + this._formatContent(data));
};

Formatter.prototype.formatFilename = function (data) {
Expand All @@ -93,9 +106,15 @@ Formatter.prototype.formatFilename = function (data) {
};

Formatter.prototype.formatURL = function (data, relativeTo) {
var derived = data.derived || {};

var slug = data.properties.slug[0];
var url = strftime('%Y/%m', data.properties.published[0]) + '/' + (slug ? slug + '/' : '');

if (derived.category) {
url = derived.category + '/' + url;
}

if (relativeTo) {
url = urlModule.resolve(relativeTo, url);
}
Expand Down
34 changes: 34 additions & 0 deletions test/formatter.spec.js
Expand Up @@ -126,6 +126,21 @@ describe('Formatter', function () {
);
});

it('should handle derived, real, categories', function () {
baseMicroformatData.derived = { category: 'interaction' };

return formatter.format(baseMicroformatData).should.eventually.equal(
'---\n' +
'layout: micropubpost\n' +
'date: \'2015-06-30T14:34:01.000Z\'\n' +
'title: awesomeness is awesome\n' +
'slug: awesomeness-is-awesome\n' +
'category: interaction\n' +
'---\n' +
'hello world\n'
);
});

});

describe('_formatSlug', function () {
Expand Down Expand Up @@ -186,6 +201,11 @@ describe('Formatter', function () {
return formatter.formatURL(baseMicroformatData, 'http://example.com/foo/').should.eventually.equal('http://example.com/foo/2015/06/awesomeness-is-awesome/');
});

it('should include derived category if any', function () {
baseMicroformatData.derived = { category: 'interaction' };
return formatter.formatURL(baseMicroformatData).should.eventually.equal('interaction/2015/06/awesomeness-is-awesome/');
});

});

describe('preFormat', function () {
Expand All @@ -200,6 +220,20 @@ describe('Formatter', function () {
return formatter.preFormat(baseMicroformatData).should.eventually.have.deep.property('properties.slug[0]', 'awesomeness-is-awesome');
});

it('should derive interaction category for replies', function () {
baseMicroformatData.properties['in-reply-to'] = ['http://example.com/liked/page'];
return formatter.preFormat(baseMicroformatData).should.eventually.have.deep.property('derived.category', 'interaction');
});

it('should derive interaction category for likes', function () {
baseMicroformatData.properties['like-of'] = ['http://example.com/liked/page'];
return formatter.preFormat(baseMicroformatData).should.eventually.have.deep.property('derived.category', 'interaction');
});

it('should not derive interaction category for non-interactions', function () {
return formatter.preFormat(baseMicroformatData).should.eventually.not.have.deep.property('derived.category');
});

});

});

0 comments on commit d626eae

Please sign in to comment.