Skip to content

Commit

Permalink
fix(main): remove HTML before content slug calc
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 6, 2015
1 parent fb31d70 commit 12b5da6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Expand Up @@ -6,6 +6,9 @@ var _ = require('lodash');
var urlModule = require('url');
var yaml = require('js-yaml');
var strftime = require('strftime');
var ent = require('ent');

var htmlRegexp = /<[^>]+>/g;

var Formatter = function () {};

Expand Down Expand Up @@ -46,7 +49,15 @@ Formatter.prototype._formatSlug = function (data) {
return _.kebabCase(data.properties.slug[0]);
}

var name = (data.properties.name || data.properties.content || [''])[0].trim();
var name;

if (data.properties.name) {
name = data.properties.name[0].trim();
}
if (!name && data.properties.content) {
name = data.properties.content[0].trim();
name = ent.decode(name.replace(htmlRegexp, ''));
}

if (name) {
name = name.split(/\s+/);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -27,6 +27,7 @@
"sinon": "^1.15.4"
},
"dependencies": {
"ent": "^2.2.0",
"js-yaml": "^3.3.1",
"lodash": "^3.10.0",
"strftime": "^0.9.2"
Expand Down
8 changes: 8 additions & 0 deletions test/formatter.spec.js
Expand Up @@ -143,6 +143,14 @@ describe('Formatter', function () {
formatter._formatSlug(baseMicroformatData).should.equal('hello-world');
});

it('should ignore html-tags when basing slug on content', function () {
delete baseMicroformatData.properties.name;
baseMicroformatData.properties.content = ['<h1>Foo</h1> Bar &amp; <strong>Abc</strong>'];
// Test twice so that we don't get a non-reusable regexp!
formatter._formatSlug(baseMicroformatData).should.equal('foo-bar-abc');
formatter._formatSlug(baseMicroformatData).should.equal('foo-bar-abc');
});

it('should ulimately fall back to publish time', function () {
delete baseMicroformatData.properties.name;
delete baseMicroformatData.properties.content;
Expand Down

0 comments on commit 12b5da6

Please sign in to comment.