Skip to content

Commit

Permalink
fix(main): some bugs with new type of HTML-content
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Sep 7, 2015
1 parent 8c33f35 commit 9d131e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
26 changes: 19 additions & 7 deletions index.js
Expand Up @@ -17,6 +17,22 @@ var kebabRegexp = /[^a-z0-9]+/g;
var whitespaceRegexp = /\s+/g;
var httpRegexp = /^http(s?):\/\//;

var getMfValue = function (data) {
return _(data || [])
.map(function (item) {
if (!item) { return; }
if (item.value) { return item.value; }
if (item.html) {
return ent.decode(item.html.replace(htmlRegexp, ' '))
.replace(whitespaceRegexp, ' ')
.trim();
}
if (_.isString(item)) { return item; }
})
.filter()
.value();
};

var semiKebabCase = function (name) {
// Convert camel case to spaces, then ensure everything is lower case and then finally – make kebab
return _.deburr(name)
Expand Down Expand Up @@ -122,9 +138,8 @@ Formatter.prototype._formatSlug = function (data) {
if (data.properties.name) {
name = data.properties.name[0].trim();
}
if (!name && data.properties.content && this.contentSlug) {
name = data.properties.content[0].trim();
name = ent.decode(name.replace(htmlRegexp, ''));
if (!name && !_.isEmpty(data.properties.content) && this.contentSlug) {
name = getMfValue(data.properties.content).join('\n');
}

if (name) {
Expand Down Expand Up @@ -197,10 +212,7 @@ Formatter.prototype.preFormat = function (data) {

var strippedContent, estimatedLang;
if (_.isEmpty(data.properties.lang) && this.deriveLanguages && !_.isEmpty(data.properties.content)) {
strippedContent = ((data.properties.content || [])[0] || '')
.replace(htmlRegexp, ' ')
.replace(whitespaceRegexp, ' ')
.trim();
strippedContent = getMfValue(data.properties.content).join('\n');

if (strippedContent !== '') {
estimatedLang = franc(strippedContent, this.deriveLanguages === true ? {} : { whitelist : this.deriveLanguages });
Expand Down
17 changes: 15 additions & 2 deletions test/formatter.spec.js
Expand Up @@ -281,7 +281,7 @@ describe('Formatter', function () {

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>'];
baseMicroformatData.properties.content = [{html: '<h1>Foo</h1> Bar &amp; <strong>Abc</strong>'}];
formatter = new Formatter({ contentSlug: true });
// Test twice so that we don't get a non-reusable regexp!
formatter._formatSlug(baseMicroformatData).should.equal('foo-bar-abc');
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('Formatter', function () {
.that.deep.equals(['de']);
});

it('should hanbdle undetectable language', function () {
it('should handle undetectable language', function () {
baseMicroformatData.properties.content = ['Nope'];

formatter = new Formatter({
Expand Down Expand Up @@ -545,6 +545,19 @@ describe('Formatter', function () {
.that.deep.equals(['123']);
});

it('should derive english language from HTML content', function () {
baseMicroformatData.properties.content = [{html: 'Another evening at the cottage and Bob was feeling brave. Make some food by himself? Surely, how hard could it be. But as the night went on and the dinner cooked Bob grew ever more desperate. Pepper, salt – all these crazy names of things he had never heard before. It would be a long night for poor Mr Bob.'}];

formatter = new Formatter({
deriveLanguages: ['eng', 'swe'],
});

return formatter.preFormat(baseMicroformatData)
.should.eventually
.have.deep.property('properties.lang')
.that.deep.equals(['en']);
});

});

describe('_formatFilesSlug', function () {
Expand Down

0 comments on commit 9d131e0

Please sign in to comment.