UNPKG

yoastseo

Version:

Yoast client-side content analysis

137 lines (131 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _i18n = require("@wordpress/i18n"); var _lodash = require("lodash"); var _AssessmentResult = _interopRequireDefault(require("../../../values/AssessmentResult")); var _Mark = _interopRequireDefault(require("../../../values/Mark")); var _addMark = _interopRequireDefault(require("../../../markers/addMark")); var _helpers = require("../../../helpers"); var _languageProcessing = require("../../../languageProcessing"); var _includesConsecutiveWords = require("./helpers/includesConsecutiveWords"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * An inclusive language assessment. * * Based on the configuration given to it in the constructor, it assesses * whether a paper's text contains potentially non-inclusive phrases and * suggests a potentially more inclusive alternative. */ class InclusiveLanguageAssessment { /** * Creates a new inclusive language assessment. * * @param {object} config The assessment configuration. * * @param {string} config.identifier The identifier of this assessment. * @param {string[]} config.nonInclusivePhrases The non-inclusive phrases. * @param {string|array} config.inclusiveAlternatives The suggested alternative, more inclusive, phrase(s). * @param {number} config.score The score to give if the non-inclusive phrase is recognized in the text. * @param {string} config.feedbackFormat The feedback format string, * should include a `%1$s` placeholder for the non-inclusive phrase * and `%2$s` (and potentially further replacements) for the suggested alternative(s). * @param {string} config.learnMoreUrl The URL to an article explaining more about this specific assessment. * @param {function} [config.rule] A potential additional rule for targeting the non-inclusive phrases. * @param {string} [config.ruleDescription] A description of the rule. * @param {boolean} [config.caseSensitive=false] If the inclusive phrase is case-sensitive, defaults to `false`. * @param {string} [config.category] The category of the assessment. * * @returns {void} */ constructor({ identifier, nonInclusivePhrases, inclusiveAlternatives, score, feedbackFormat, learnMoreUrl, rule, ruleDescription, caseSensitive, category }) { this.identifier = identifier; this.nonInclusivePhrases = nonInclusivePhrases; this.inclusiveAlternatives = inclusiveAlternatives; if ((0, _lodash.isString)(this.inclusiveAlternatives)) { this.inclusiveAlternatives = [this.inclusiveAlternatives]; } this.score = score; this.feedbackFormat = feedbackFormat; this.learnMoreUrl = (0, _helpers.createAnchorOpeningTag)(learnMoreUrl); this.rule = rule || _includesConsecutiveWords.includesConsecutiveWords; this.ruleDescription = ruleDescription; this.caseSensitive = caseSensitive || false; this.category = category; } /** * Checks whether the assessment is applicable for the given paper. * * @param {Paper} paper The paper to check. * @param {Researcher} researcher The researcher. * * @returns {boolean} Whether the assessment is applicable for the given paper. */ isApplicable(paper, researcher) { const sentences = researcher.getResearch("sentences"); // Also include the text title in the analysis as a separate sentence. const textTitle = paper.getTextTitle(); sentences.push(textTitle); this.foundPhrases = []; sentences.forEach(sentence => { let words = (0, _languageProcessing.getWords)(sentence, "\\s", false); if (!this.caseSensitive) { words = words.map(word => word.toLocaleLowerCase()); } const foundPhrase = this.nonInclusivePhrases.find(phrase => this.rule(words, (0, _languageProcessing.getWords)(phrase, "\\s", false)).length >= 1); if (foundPhrase) { this.foundPhrases.push({ sentence: sentence, phrase: foundPhrase }); } }); return this.foundPhrases.length >= 1; } /** * Execute the Assessment and return a result. * * @returns {AssessmentResult} The result of the assessment, containing both a score and a descriptive text. */ getResult() { const link = (0, _i18n.sprintf)("%1$sLearn more.%2$s", this.learnMoreUrl, "</a>"); // eslint-disable-next-line @wordpress/valid-sprintf -- The sprintf function is used to replace placeholders in the feedbackFormat variable. const text = (0, _i18n.sprintf)(this.feedbackFormat, this.foundPhrases[0].phrase, ...this.inclusiveAlternatives); const result = new _AssessmentResult.default({ score: this.score, text: `${text} ${link}` }); result.setIdentifier(this.identifier); result.setHasMarks(true); return result; } /** * Marks text for the inclusive language assessment. * * @returns {Array<Mark>} A list of marks that should be applied. */ getMarks() { if (!this.foundPhrases) { return []; } return this.foundPhrases.map(foundPhrase => new _Mark.default({ original: foundPhrase.sentence, marked: (0, _addMark.default)(foundPhrase.sentence) })); } } exports.default = InclusiveLanguageAssessment; //# sourceMappingURL=InclusiveLanguageAssessment.js.map