UNPKG

yoastseo

Version:

Yoast client-side content analysis

87 lines (82 loc) 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = matchKeywordInSubheadings; var _getSubheadings = require("../helpers/html/getSubheadings"); var _stripNonTextTags = _interopRequireDefault(require("../helpers/sanitize/stripNonTextTags")); var _findKeywordFormsInString = require("../helpers/match/findKeywordFormsInString"); var _htmlParser = _interopRequireDefault(require("../helpers/html/htmlParser")); var _helpers = require("../helpers"); var _getWords = _interopRequireDefault(require("../helpers/word/getWords")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * @typedef {import("../../languageProcessing/AbstractResearcher").default } Researcher * @typedef {import("../../values/").Paper } Paper */ /** * @typedef {Object} KeyphraseInSubheadingsResult * @property {number} count The number of subheadings. * @property {number} matches The number of subheadings reflecting the topic. * @property {number} percentReflectingTopic The percentage of subheadings reflecting the topic. * @property {string} text The text that was analyzed. * @property {number} textLength The length of the text that was analyzed in words. */ /** * Computes the number of subheadings reflecting the topic. * * @param {Object} topicForms The main key phrase and its synonyms to check. * @param {string[]} subheadings The subheadings to check. * @param {boolean} useSynonyms Whether to match synonyms or only main keyphrase. * @param {string} locale The current locale. * @param {string[]} functionWords The function words list. * @param {function} matchWordCustomHelper The language-specific helper function to match word in text. * * @returns {number} The number of subheadings reflecting the topic. */ const numberOfSubheadingsReflectingTopic = function (topicForms, subheadings, useSynonyms, locale, functionWords, matchWordCustomHelper) { return subheadings.filter(subheading => { const matchedTopicForms = (0, _findKeywordFormsInString.findTopicFormsInString)(topicForms, subheading, useSynonyms, locale, matchWordCustomHelper); if (functionWords.length === 0) { return matchedTopicForms.percentWordMatches === 100; } return matchedTopicForms.percentWordMatches > 50; }).length; }; /** * Checks if there are any h2 or h3 subheadings in the text and if they have the keyphrase or synonyms in them. * * @param {Paper} paper The paper object containing the text and keyword. * @param {Researcher} researcher The researcher object. * * @returns {KeyphraseInSubheadingsResult} An object containing the number of subheadings, * the number of subheadings reflecting the topic, the percentage of subheadings reflecting the topic and an empty string. */ function matchKeywordInSubheadings(paper, researcher) { const functionWords = researcher.getConfig("functionWords"); // A custom helper to match word in text. const matchWordCustomHelper = researcher.getHelper("matchWordCustomHelper"); const customCountLength = researcher.getHelper("customCountLength"); let text = paper.getText(); text = (0, _htmlParser.default)(text); text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes); text = (0, _stripNonTextTags.default)(text); const topicForms = researcher.getResearch("morphology"); const locale = paper.getLocale(); const result = { count: 0, matches: 0, percentReflectingTopic: 0, text: text, textLength: customCountLength ? customCountLength(text) : (0, _getWords.default)(text).length }; const useSynonyms = true; const subheadings = (0, _getSubheadings.getSubheadingContentsTopLevel)(text); if (subheadings.length !== 0) { result.count = subheadings.length; result.matches = numberOfSubheadingsReflectingTopic(topicForms, subheadings, useSynonyms, locale, functionWords, matchWordCustomHelper); result.percentReflectingTopic = result.matches / result.count * 100; } return result; } //# sourceMappingURL=matchKeywordInSubheadings.js.map