From 12b5da6f6cb4aa91ea2eaa15b06659c36a7c926e Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Mon, 6 Jul 2015 22:27:31 +0200 Subject: [PATCH] fix(main): remove HTML before content slug calc --- index.js | 13 ++++++++++++- package.json | 1 + test/formatter.spec.js | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1386202..f21c020 100644 --- a/index.js +++ b/index.js @@ -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 () {}; @@ -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+/); diff --git a/package.json b/package.json index d089cfb..09b134b 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/formatter.spec.js b/test/formatter.spec.js index 01aa9f2..be778fc 100644 --- a/test/formatter.spec.js +++ b/test/formatter.spec.js @@ -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 = ['

Foo

Bar & Abc']; + // 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;