UNPKG

yoastseo

Version:

Yoast client-side content analysis

68 lines (65 loc) 3.13 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"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Computes the amount 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 {Array} functionWords The function words list. * @param {function} matchWordCustomHelper The language-specific helper function to match word in text. * * @returns {number} The amount 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 {Object} paper The paper object containing the text and keyword. * @param {Researcher} researcher The researcher object. * * @returns {Object} The result object. */ function matchKeywordInSubheadings(paper, researcher) { const functionWords = researcher.getConfig("functionWords"); // A custom helper to match word in text. const matchWordCustomHelper = researcher.getHelper("matchWordCustomHelper"); 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 }; 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