UNPKG

yoastseo

Version:

Yoast client-side content analysis

175 lines (169 loc) 7.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _i18n = require("@wordpress/i18n"); var _lodash = require("lodash"); var _missingArgument = _interopRequireDefault(require("../errors/missingArgument")); var _helpers = require("../helpers"); var _AssessmentResult = _interopRequireDefault(require("../values/AssessmentResult.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * The PreviouslyUsedKeyword plugin allows to check for previously used keywords. */ class PreviouslyUsedKeyword { /** * Constructs a new PreviouslyUsedKeyword plugin. * * @param {object} app The app. * @param {object} args An arguments object. * @param {object} args.usedKeywords An object with keywords and ids where they are used. * @param {object} args.usedKeywordsPostTypes An object with the post types of the post ids from usedKeywords. * @param {string} args.searchUrl The url used to link to a search page when multiple usages of the keyword are found. * @param {string} args.postUrl The url used to link to a post when 1 usage of the keyword is found. * * @constructor */ constructor(app, args) { if ((0, _lodash.isUndefined)(app)) { throw new _missingArgument.default("The previously keyword plugin requires the YoastSEO app"); } if ((0, _lodash.isUndefined)(args)) { args = { usedKeywords: {}, usedKeywordsPostTypes: {}, searchUrl: "", postUrl: "" }; } this.app = app; this.usedKeywords = args.usedKeywords; this.usedKeywordsPostTypes = args.usedKeywordsPostTypes; this.searchUrl = args.searchUrl; this.postUrl = args.postUrl; this.urlTitle = (0, _helpers.createAnchorOpeningTag)("https://yoa.st/33x"); this.urlCallToAction = (0, _helpers.createAnchorOpeningTag)("https://yoa.st/33y"); } /** * Registers the assessment with the assessor. * * @returns {void} */ registerPlugin() { this.app.registerAssessment("usedKeywords", { getResult: this.assess.bind(this) }, "previouslyUsedKeywords"); } /** * Updates the usedKeywords. * * @param {object} usedKeywords An object with keywords and ids where they are used. * @param {object} usedKeywordsPostTypes An object with keywords and in which post types they are used. * The post types correspond with the ids in the usedKeywords parameter. * @returns {void} */ updateKeywordUsage(usedKeywords, usedKeywordsPostTypes) { this.usedKeywords = usedKeywords; this.usedKeywordsPostTypes = usedKeywordsPostTypes; } /** * Scores the previously used keyword assessment based on the count. * * @param {object} previouslyUsedKeywords The result of the previously used keywords research * @param {Paper} paper The paper object to research. * @returns {{text: string, score: number}} The result object with a feedback text and a score. */ scoreAssessment(previouslyUsedKeywords, paper) { if (Object.keys(previouslyUsedKeywords).length === 0) { return { text: (0, _i18n.sprintf)( /* translators: %1$s and %2$s expand to a link to an article on yoast.com, %3$s expands to an anchor end tag. */ (0, _i18n.__)("%1$sPreviously used keyphrase%3$s: No focus keyphrase was set for this page. " + "%2$sPlease add a focus keyphrase you haven't used before on other content%3$s.", "wordpress-seo"), this.urlTitle, this.urlCallToAction, "</a>"), score: 1 }; } const count = previouslyUsedKeywords.count; const id = previouslyUsedKeywords.id; const postTypeToDisplay = previouslyUsedKeywords.postTypeToDisplay; let url; if (count === 0) { return { text: (0, _i18n.sprintf)( /* translators: %1$s expands to a link to an article on yoast.com, %2$s expands to an anchor end tag. */ (0, _i18n.__)("%1$sPreviously used keyphrase%2$s: You've not used this keyphrase before, very good.", "wordpress-seo"), this.urlTitle, "</a>"), score: 9 }; } if (count === 1) { url = `<a href='${this.postUrl.replace("{id}", id)}' target='_blank'>`; return { /* translators: %1$s expands to an admin link where the keyphrase is already used, %2$s expands to the anchor end tag, %3$s and %4$s expand to links on yoast.com. */ text: (0, _i18n.sprintf)((0, _i18n.__)("%3$sPreviously used keyphrase%2$s: You've used this keyphrase %1$sonce before%2$s. %4$sDo not use your keyphrase more than once%2$s.", "wordpress-seo"), url, "</a>", this.urlTitle, this.urlCallToAction), score: 6 }; } if (count > 1) { if (postTypeToDisplay) { url = `<a href='${this.searchUrl.replace("{keyword}", encodeURIComponent(paper.getKeyword()))}&post_type=${postTypeToDisplay}' target='_blank'>`; } else { url = `<a href='${this.searchUrl.replace("{keyword}", encodeURIComponent(paper.getKeyword()))}' target='_blank'>`; } return { /* translators: %1$s expands to a link to the admin search page for the keyphrase, %2$s expands to the anchor end tag, %3$s and %4$s expand to links to yoast.com */ text: (0, _i18n.sprintf)((0, _i18n.__)("%3$sPreviously used keyphrase%2$s: You've used this keyphrase %1$smultiple times before%2$s. %4$sDo not use your keyphrase more than once%2$s.", "wordpress-seo"), url, "</a>", this.urlTitle, this.urlCallToAction), score: 1 }; } } /** * Researches the previously used keywords, based on the used keywords and the keyword in the paper. * * @param {Paper} paper The paper object to research. * @returns {{id: number, count: number} || {}} The object with the count and the id of the previously used keyword, * or an empty object if the paper has no keyphrase. */ researchPreviouslyUsedKeywords(paper) { const keyword = paper.getKeyword(); if (!keyword) { return {}; } let count = 0; let postTypeToDisplay = ""; let id = 0; if (!(0, _lodash.isUndefined)(this.usedKeywords[keyword]) && this.usedKeywords[keyword].length > 0) { count = this.usedKeywords[keyword].length; if (keyword in this.usedKeywordsPostTypes) { postTypeToDisplay = this.usedKeywordsPostTypes[keyword][0]; } id = this.usedKeywords[keyword][0]; } return { id: id, count: count, postTypeToDisplay: postTypeToDisplay }; } /** * Executes the assessment that checks whether a text uses previously used keywords. * * @param {Paper} paper The Paper object to assess. * @returns {AssessmentResult} The assessment result containing both a score and a descriptive text. */ assess(paper) { const previouslyUsedKeywords = this.researchPreviouslyUsedKeywords(paper); const previouslyUsedKeywordsResult = this.scoreAssessment(previouslyUsedKeywords, paper); const assessmentResult = new _AssessmentResult.default(); assessmentResult.setScore(previouslyUsedKeywordsResult.score); assessmentResult.setText(previouslyUsedKeywordsResult.text); return assessmentResult; } } exports.default = PreviouslyUsedKeyword; //# sourceMappingURL=previouslyUsedKeywords.js.map