UNPKG

yoastseo

Version:

Yoast client-side content analysis

68 lines (64 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _getSubheadingTexts = _interopRequireDefault(require("../helpers/html/getSubheadingTexts")); var _countWords = _interopRequireDefault(require("../helpers/word/countWords")); var _lodash = require("lodash"); var _htmlParser = _interopRequireDefault(require("../helpers/html/htmlParser")); var _helpers = require("../helpers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * @typedef {Object} SubheadingText The object containing the subheading and the text following the subheading. * @property {string} subheading The subheading. * @property {string} text The text following the subheading. * @property {number} countLength The length of the text following the subheading. * @property {number} [index] The index of the subheading in the text. */ /** * Gets the subheadings from the text and returns the text following of these subheading in an array. * * @param {Paper} paper The Paper object to get the text from. * @param {Researcher} researcher The researcher to use for analysis. * * @returns {SubheadingText[]} The array containing the object of found subheadings and the length of the text before the first subheading. */ function _default(paper, researcher) { let text = paper.getText(); text = (0, _htmlParser.default)(text); text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes); const matches = (0, _getSubheadingTexts.default)(text); // An optional custom helper to count length to use instead of countWords. const customCountLength = researcher.getHelper("customCountLength"); const foundSubheadings = []; (0, _lodash.forEach)(matches, function (match) { foundSubheadings.push({ subheading: match.subheading, text: match.text, countLength: customCountLength ? customCountLength(match.text) : (0, _countWords.default)(match.text), index: match.index }); }); let textBeforeFirstSubheadingLength = 0; let textBeforeFirstSubheading = ""; if (foundSubheadings.length > 0) { // Find first subheading. const firstSubheading = foundSubheadings[0]; // Retrieve text preceding first subheading. textBeforeFirstSubheading = text.slice(0, firstSubheading.index); textBeforeFirstSubheadingLength = customCountLength ? customCountLength(textBeforeFirstSubheading) : (0, _countWords.default)(textBeforeFirstSubheading); } // Check if there is a text before the first subheading. if (textBeforeFirstSubheadingLength > 0 && textBeforeFirstSubheading !== "") { // Also add the text before the first subheading to the array. foundSubheadings.unshift({ // Assign an empty string for the subheading for text that comes before the first subheading. subheading: "", text: textBeforeFirstSubheading, countLength: textBeforeFirstSubheadingLength }); } return foundSubheadings; } //# sourceMappingURL=getSubheadingTextLengths.js.map