Skip to content

Commit

Permalink
feat(main): extract person tags from categories
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 29, 2015
1 parent b5134b5 commit c245871
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
20 changes: 20 additions & 0 deletions index.js
Expand Up @@ -15,6 +15,7 @@ var htmlRegexp = /<[^>]+>/g;
var camelRegexp = /([a-z])([A-Z])/g;
var kebabRegexp = /[^a-z0-9]+/g;
var whitespaceRegexp = /\s+/g;
var httpRegexp = /^http(s?):\/\//;

var semiKebabCase = function (name) {
// Convert camel case to spaces, then ensure everything is lower case and then finally – make kebab
Expand Down Expand Up @@ -74,6 +75,9 @@ Formatter.prototype._formatFrontMatter = function (data) {
if (derived.category) {
target.category = derived.category;
}
if (derived.personTags) {
target.persontags = derived.personTags;
}

return '---\n' + yaml.safeDump(target) + '---\n';
};
Expand Down Expand Up @@ -209,6 +213,22 @@ Formatter.prototype.preFormat = function (data) {

data.derived = {};

if (!_.isEmpty(data.properties.category)) {
data.derived.personTags = [];

data.properties.category = _.filter(data.properties.category, function (tag) {
if (httpRegexp.test(tag)) {
data.derived.personTags.push(tag);
return false;
}
return true;
});

if (_.isEmpty(data.derived.personTags)) {
delete data.derived.personTags;
}
}

if (
!_.isEmpty(data.properties.bookmark) ||
!_.isEmpty(data.properties['repost-of']) ||
Expand Down
27 changes: 27 additions & 0 deletions test/formatter.spec.js
Expand Up @@ -176,6 +176,22 @@ describe('Formatter', function () {
);
});

it('should handle derived person-tags', function () {
baseMicroformatData.derived = { personTags: ['http://example.com/'] };

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' +
'persontags:\n' +
' - \'http://example.com/\'\n' +
'---\n' +
'hello world\n'
);
});

it('should convert HTML to Markdown', function () {
baseMicroformatData.properties.content = ['<p>Abc</p><p>123</p><ul><li>Foo</li><li>Bar</li></ul>'];
return formatter.format(baseMicroformatData).should.eventually.equal(
Expand Down Expand Up @@ -357,6 +373,17 @@ describe('Formatter', function () {
return formatter.preFormat(baseMicroformatData).should.eventually.not.have.deep.property('derived.category');
});

it('should extract person tags from regular tags', function () {
baseMicroformatData.properties.category = ['http://example.com/', 'foo', 'http://example.net/'];
return formatter.preFormat(baseMicroformatData).then(function (result) {
result.should.have.deep.property('derived.personTags').that.deep.equals([
'http://example.com/',
'http://example.net/',
]);
result.should.have.deep.property('properties.category').that.deep.equals(['foo']);
});
});

it('should flag that the data has been preformatted', function () {
return formatter.preFormat(baseMicroformatData).should.eventually.have.property('preFormatted', true);
});
Expand Down

0 comments on commit c245871

Please sign in to comment.