yoastseo
Version:
Yoast client-side content analysis
196 lines (187 loc) • 8.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _lodash = require("lodash");
var _formatNumber = _interopRequireDefault(require("../../../helpers/formatNumber"));
var _inRange = require("../../helpers/assessments/inRange");
var _shortlinker = require("../../../helpers/shortlinker");
var _stripHTMLTags = require("../../../languageProcessing/helpers/sanitize/stripHTMLTags");
var _AssessmentResult = _interopRequireDefault(require("../../../values/AssessmentResult"));
var _Mark = _interopRequireDefault(require("../../../values/Mark.js"));
var _addMark = _interopRequireDefault(require("../../../markers/addMark.js"));
var _assessment = _interopRequireDefault(require("../assessment"));
var _htmlParser = _interopRequireDefault(require("../../../languageProcessing/helpers/html/htmlParser"));
var _getWords = _interopRequireDefault(require("../../../languageProcessing/helpers/word/getWords"));
var _helpers = require("../../../languageProcessing/helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Represents the assessment that checks whether there are enough transition words in the text.
*/
class TransitionWordsAssessment extends _assessment.default {
/**
* Sets the identifier and the config.
*
* @param {object} config The configuration to use.
*
* @returns {void}
*/
constructor(config = {}) {
super();
const defaultConfig = {
urlTitle: (0, _shortlinker.createAnchorOpeningTag)("https://yoa.st/34z"),
urlCallToAction: (0, _shortlinker.createAnchorOpeningTag)("https://yoa.st/35a"),
transitionWordsNeededIfTextLongerThan: 200
};
this.identifier = "textTransitionWords";
this._config = (0, _lodash.merge)(defaultConfig, config);
}
/**
* Calculates the actual percentage of transition words in the sentences.
*
* @param {object} sentences The object containing the total number of sentences and the number of sentences containing
* a transition word.
*
* @returns {number} The percentage of sentences containing a transition word.
*/
calculateTransitionWordPercentage(sentences) {
if (sentences.transitionWordSentences === 0 || sentences.totalSentences === 0) {
return 0;
}
return (0, _formatNumber.default)(sentences.transitionWordSentences / sentences.totalSentences * 100);
}
/**
* Calculates the score for the assessment based on the percentage of sentences containing transition words.
*
* @param {number} percentage The percentage of sentences containing transition words.
*
* @returns {number} The score.
*/
calculateScoreFromPercentage(percentage) {
if (percentage < 20) {
// Red indicator.
return 3;
}
if ((0, _inRange.inRangeStartInclusive)(percentage, 20, 30)) {
// Orange indicator.
return 6;
}
// Green indicator.
return 9;
}
/**
* Calculates transition word result.
*
* @param {object} transitionWordSentences The object containing the total number of sentences and the number of sentences containing
* a transition word.
* @param {number} textLength The length of the text.
*
* @returns {object} Object containing score and text.
*/
calculateTransitionWordResult(transitionWordSentences, textLength) {
const percentage = this.calculateTransitionWordPercentage(transitionWordSentences);
const score = this.calculateScoreFromPercentage(percentage);
const hasMarks = percentage > 0;
// If the text is shorter than the minimum required length for transition words, we always return a green traffic light.
if (textLength < this._config.transitionWordsNeededIfTextLongerThan) {
if (percentage > 0) {
return {
score: (0, _formatNumber.default)(9),
hasMarks: hasMarks,
text: (0, _i18n.sprintf)(/* translators: %1$s expands to a link on yoast.com, %2$s expands to the anchor end tag. */
(0, _i18n.__)("%1$sTransition words%2$s: Well done!", "wordpress-seo"), this._config.urlTitle, "</a>")
};
}
return {
score: (0, _formatNumber.default)(9),
hasMarks: hasMarks,
text: (0, _i18n.sprintf)(/* translators: %1$s expands to a link on yoast.com, %2$s expands to the anchor end tag. */
(0, _i18n.__)("%1$sTransition words%2$s: You are not using any transition words, but your text is short enough and probably doesn't need them.", "wordpress-seo"), this._config.urlTitle, "</a>")
};
}
if (score < 7 && percentage === 0) {
return {
score: (0, _formatNumber.default)(score),
hasMarks: hasMarks,
text: (0, _i18n.sprintf)(/* translators: %1$s and %3$s expand to a link to yoast.com, %2$s expands to the anchor end tag */
(0, _i18n.__)("%1$sTransition words%2$s: None of the sentences contain transition words. %3$sUse some%2$s.", "wordpress-seo"), this._config.urlTitle, "</a>", this._config.urlCallToAction)
};
}
if (score < 7) {
return {
score: (0, _formatNumber.default)(score),
hasMarks: hasMarks,
text: (0, _i18n.sprintf)(
/* translators: %1$s and %4$s expand to a link to yoast.com, %2$s expands to the anchor end tag,
%3$s expands to the percentage of sentences containing transition words */
(0, _i18n.__)("%1$sTransition words%2$s: Only %3$s of the sentences contain transition words, which is not enough. %4$sUse more of them%2$s.", "wordpress-seo"), this._config.urlTitle, "</a>", percentage + "%", this._config.urlCallToAction)
};
}
return {
score: (0, _formatNumber.default)(score),
hasMarks: hasMarks,
text: (0, _i18n.sprintf)(/* translators: %1$s expands to a link on yoast.com, %2$s expands to the anchor end tag. */
(0, _i18n.__)("%1$sTransition words%2$s: Well done!", "wordpress-seo"), this._config.urlTitle, "</a>")
};
}
/**
* Scores the percentage of sentences including one or more transition words.
*
* @param {object} paper The paper to use for the assessment.
* @param {object} researcher The researcher used for calling research.
*
* @returns {object} The Assessment result.
*/
getResult(paper, researcher) {
const customCountLength = researcher.getHelper("customCountLength");
const customMinimumRequiredTextLength = researcher.getConfig("assessmentApplicability").transitionWords;
if (customMinimumRequiredTextLength) {
this._config.transitionWordsNeededIfTextLongerThan = customMinimumRequiredTextLength;
}
let text = paper.getText();
text = (0, _htmlParser.default)(text);
text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes);
const textLength = customCountLength ? customCountLength(text) : (0, _getWords.default)(text).length;
const transitionWordSentences = researcher.getResearch("findTransitionWords");
const transitionWordResult = this.calculateTransitionWordResult(transitionWordSentences, textLength);
const assessmentResult = new _AssessmentResult.default();
assessmentResult.setScore(transitionWordResult.score);
assessmentResult.setText(transitionWordResult.text);
assessmentResult.setHasMarks(transitionWordResult.hasMarks);
return assessmentResult;
}
/**
* Marks text for the transition words assessment.
*
* @param {Paper} paper The paper to use for the marking.
* @param {Researcher} researcher The researcher containing the necessary research.
*
* @returns {Array<Mark>} A list of marks that should be applied.
*/
getMarks(paper, researcher) {
const transitionWordSentences = researcher.getResearch("findTransitionWords");
return (0, _lodash.map)(transitionWordSentences.sentenceResults, function (sentenceResult) {
let sentence = sentenceResult.sentence;
sentence = (0, _stripHTMLTags.stripIncompleteTags)(sentence);
return new _Mark.default({
original: sentence,
marked: (0, _addMark.default)(sentence)
});
});
}
/**
* Checks if the transition words assessment is applicable to the paper.
*
* @param {Paper} paper The paper to check.
* @param {Researcher} researcher The researcher object.
*
* @returns {boolean} Returns true if the assessment is available in the researcher of the language.
*/
isApplicable(paper, researcher) {
return researcher.hasResearch("findTransitionWords");
}
}
exports.default = TransitionWordsAssessment;
//# sourceMappingURL=TransitionWordsAssessment.js.map