Skip to content

Commit

Permalink
Merge pull request #180 from nymag/clean-validation-text
Browse files Browse the repository at this point in the history
clean validation text
  • Loading branch information
TakenPilot committed Aug 20, 2015
2 parents 3a5e6df + 20e4962 commit 5bf1c76
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
49 changes: 36 additions & 13 deletions publishing-rules/ban-tk.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
var label, description,
tkText = 'TK',
searchTexts = ['TK', 'tktk'],
articleFields = ['primaryHeadline', 'teaser'],
paragraphField = 'text',
_ = require('lodash'),
stripTags = require('striptags'),
references = require('../services/references'),
refProp = references.referenceProperty,
cutStart = 20,
cutEnd = 20;

label = 'Ban TKs';
description = 'TKs are not allowed';
description = 'Any TK or tktk in the article\'s primary headline, teaser or in any paragraph cannot be published.';

/**
* Return first value of list to be found in str
* @param {string} str
* @param {[string]} list
* @returns {string|null}
*/
function findFirstText(str, list) {
var i;

for (i = 0; i < list.length; i++) {
if (str.indexOf(list[i]) > -1) {
return list[i];
}
}
return null;
}

/**
* @param {string} componentName
Expand All @@ -29,20 +47,22 @@ function getLabel(componentName, label, ref) {
/**
* @param {string} value
* @param {object} data
* @param {string} tkText
* @returns {string}
*/
function getPreview(value, data) {
function getPreview(value, data, tkText) {
var index;

if (_.isString(value)) {
index = value.indexOf(tkText);

if (index > cutStart) {
value = '...' + value.substr(index - cutStart);
index = index - (index - cutStart) + 3;
}

if (value.length > index + cutEnd) {
value = value.substr(0, index + cutEnd) + '...';
value = value.substr(0, index + cutEnd + tkText.length) + '...';
}

return value;
Expand All @@ -56,19 +76,20 @@ function getPreview(value, data) {
* @param {object} component
* @param {string} fieldName
* @param {[object]} errors
* @param {string} tkText
*/
function addError(component, fieldName, errors) {
function addError(component, fieldName, errors, tkText) {
var ref = component[refProp],
componentName = ref && references.getComponentNameFromReference(ref),
data = component && component[fieldName],
schema = data._schema,
label = schema && schema._label,
value = data && data.value,
value = data && data.value && stripTags(data.value),
error = {
ref: ref,
fieldName: fieldName,
label: getLabel(componentName, label, ref),
preview: getPreview(value, data)
preview: getPreview(value, data, tkText)
};

errors.push(error);
Expand All @@ -86,23 +107,25 @@ function validate(state) {

_.each(groups.paragraph, function (component) {
var field = component[paragraphField],
value = field && field.value;
value = field && field.value && stripTags(field.value),
tkText = value && findFirstText(value, searchTexts);

if (_.isString(value) && value.indexOf(tkText) > -1) {
if (tkText) {

addError(component, paragraphField, errors);
addError(component, paragraphField, errors, tkText);

}
});

_.each(groups.article, function (component) {
_.each(articleFields, function (fieldName) {
var field = component[fieldName],
value = field && field.value;
value = field && field.value && stripTags(field.value),
tkText = value && findFirstText(value, searchTexts);

if (_.isString(value) && value.indexOf(tkText) > -1) {
if (tkText) {

addError(component, fieldName, errors);
addError(component, fieldName, errors, tkText);

}
});
Expand Down
6 changes: 3 additions & 3 deletions publishing-rules/ban-tk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ describe(dirname, function () {
ref: 'k/components/paragraph/instances/l',
fieldName: 'text',
label: 'Paragraph',
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfgh...'
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;...'
}]);
});

it('returns errors for articles', function () {
var justText = 'TK',
shortText = 'abcTKdef',
longText = 'qwertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;z',
longText = 'qwer<em>tyuiopasdfghjkl;zTKqwertyuiop</em>asdfghjkl;z',
state = {
refs: {
'f/components/article': {primaryHeadline: {value: shortText}},
Expand Down Expand Up @@ -127,7 +127,7 @@ describe(dirname, function () {
ref: 'm/components/article/instances/n',
fieldName: 'primaryHeadline',
label: 'Article',
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfgh...'
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;...'
}, {
ref: 'o/components/article/instances/p',
fieldName: 'primaryHeadline',
Expand Down

0 comments on commit 5bf1c76

Please sign in to comment.