UNPKG

yoastseo

Version:

Yoast client-side content analysis

129 lines (122 loc) 5.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _i18n = require("@wordpress/i18n"); var _lodash = require("lodash"); var _assessment = _interopRequireDefault(require("../assessment")); var _inRange = require("../../helpers/assessments/inRange"); var _shortlinker = require("../../../helpers/shortlinker"); var _AssessmentResult = _interopRequireDefault(require("../../../values/AssessmentResult")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const maximumLength = 600; /** * Represents the assessment that assesses the SEO title width and gives the feedback accordingly. */ class PageTitleWidthAssessment extends _assessment.default { /** * Sets the identifier and the config. * * @param {Object} [config] The configuration to use. * @param {boolean} allowShortTitle Whether the short title width is penalized with a bad score or not. * * @returns {void} */ constructor(config = {}, allowShortTitle = false) { super(); const defaultConfig = { minLength: 400, maxLength: maximumLength, scores: { noTitle: 1, widthTooShort: 6, widthTooLong: 3, widthCorrect: 9 }, urlTitle: (0, _shortlinker.createAnchorOpeningTag)("https://yoa.st/34h"), urlCallToAction: (0, _shortlinker.createAnchorOpeningTag)("https://yoa.st/34i") }; this._allowShortTitle = allowShortTitle; this.identifier = "titleWidth"; this._config = (0, _lodash.merge)(defaultConfig, config); } /** * Returns the maximum length. * * @returns {number} The maximum length. */ getMaximumLength() { return maximumLength; } /** * Runs the pageTitleWidth module, based on this returns an assessment result with score. * * @param {Paper} paper The paper to use for the assessment. * @param {Researcher} researcher The researcher used for calling research. * * @returns {AssessmentResult} The assessment result. */ getResult(paper, researcher) { const pageTitleWidth = researcher.getResearch("pageTitleWidth"); const assessmentResult = new _AssessmentResult.default(); assessmentResult.setScore(this.calculateScore(pageTitleWidth)); assessmentResult.setText(this.translateScore(pageTitleWidth)); if (assessmentResult.getScore() < 9) { assessmentResult.setHasJumps(true); assessmentResult.setEditFieldName((0, _i18n.__)("SEO title", "wordpress-seo")); } // Max and actual are used in the snippet editor progress bar. assessmentResult.max = this._config.maxLength; assessmentResult.actual = pageTitleWidth; return assessmentResult; } /** * Returns the score for the SEO title width calculation. * * @param {number} pageTitleWidth The width of the SEO title. * * @returns {number} The calculated score. */ calculateScore(pageTitleWidth) { if ((0, _inRange.inRangeEndInclusive)(pageTitleWidth, 1, 400)) { return this._config.scores.widthTooShort; } if ((0, _inRange.inRangeEndInclusive)(pageTitleWidth, this._config.minLength, this._config.maxLength)) { return this._config.scores.widthCorrect; } if (pageTitleWidth > this._config.maxLength) { return this._config.scores.widthTooLong; } return this._config.scores.noTitle; } /** * Translates the score of the SEO title width calculation to a message the user can understand. * * @param {number} pageTitleWidth The width of the SEO title. * * @returns {string} The translated string. */ translateScore(pageTitleWidth) { if ((0, _inRange.inRangeEndInclusive)(pageTitleWidth, 1, 400)) { if (this._allowShortTitle) { return (0, _i18n.sprintf)(/* translators: %1$s expands to a link on yoast.com, %2$s expands to the anchor end tag */ (0, _i18n.__)("%1$sSEO title width%2$s: Good job!", "wordpress-seo"), this._config.urlTitle, "</a>"); } return (0, _i18n.sprintf)(/* translators: %1$s and %2$s expand to links on yoast.com, %3$s expands to the anchor end tag */ (0, _i18n.__)("%1$sSEO title width%3$s: The SEO title is too short. %2$sUse the space to add keyphrase variations or create compelling call-to-action copy%3$s.", "wordpress-seo"), this._config.urlTitle, this._config.urlCallToAction, "</a>"); } if ((0, _inRange.inRangeEndInclusive)(pageTitleWidth, this._config.minLength, this._config.maxLength)) { return (0, _i18n.sprintf)(/* translators: %1$s expands to a link on yoast.com, %2$s expands to the anchor end tag */ (0, _i18n.__)("%1$sSEO title width%2$s: Good job!", "wordpress-seo"), this._config.urlTitle, "</a>"); } if (pageTitleWidth > this._config.maxLength) { return (0, _i18n.sprintf)(/* translators: %1$s and %2$s expand to links on yoast.com, %3$s expands to the anchor end tag */ (0, _i18n.__)("%1$sSEO title width%3$s: The SEO title is wider than the viewable limit. %2$sTry to make it shorter%3$s.", "wordpress-seo"), this._config.urlTitle, this._config.urlCallToAction, "</a>"); } return (0, _i18n.sprintf)(/* translators: %1$s and %2$s expand to links on yoast.com, %3$s expands to the anchor end tag */ (0, _i18n.__)("%1$sSEO title width%3$s: %2$sPlease create an SEO title%3$s.", "wordpress-seo"), this._config.urlTitle, this._config.urlCallToAction, "</a>"); } } exports.default = PageTitleWidthAssessment; //# sourceMappingURL=PageTitleWidthAssessment.js.map