yoastseo
Version:
Yoast client-side content analysis
119 lines (113 loc) • 5.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _createRegexFromDoubleArray = _interopRequireDefault(require("../helpers/regex/createRegexFromDoubleArray.js"));
var _getSentences = _interopRequireDefault(require("../helpers/sentence/getSentences.js"));
var _quotes = require("../helpers/sanitize/quotes.js");
var _matchWordInSentence = require("../helpers/word/matchWordInSentence.js");
var _htmlParser = _interopRequireDefault(require("../helpers/html/htmlParser"));
var _lodash = require("lodash");
var _helpers = require("../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
let regexFromDoubleArray = null;
let regexFromDoubleArrayCacheKey = "";
/**
* Memoizes the createRegexFromDoubleArray with the twoPartTransitionWords.
*
* @param {Array} twoPartTransitionWords The array containing two-part transition words.
*
* @returns {RegExp} The RegExp to match text with a double array.
*/
function getRegexFromDoubleArray(twoPartTransitionWords) {
const cacheKey = (0, _lodash.flattenDeep)(twoPartTransitionWords).join("");
if (regexFromDoubleArrayCacheKey !== cacheKey || regexFromDoubleArray === null) {
regexFromDoubleArrayCacheKey = cacheKey;
regexFromDoubleArray = (0, _createRegexFromDoubleArray.default)(twoPartTransitionWords);
}
return regexFromDoubleArray;
}
/**
* Matches the sentence against two part transition words.
*
* @param {string} sentence The sentence to match against.
* @param {Array} twoPartTransitionWords The array containing two-part transition words.
* @returns {Array} The found transitional words.
*/
const matchTwoPartTransitionWords = function (sentence, twoPartTransitionWords) {
sentence = (0, _quotes.normalizeSingle)(sentence);
const twoPartTransitionWordsRegex = getRegexFromDoubleArray(twoPartTransitionWords);
return sentence.match(twoPartTransitionWordsRegex);
};
/**
* Matches the sentence against transition words.
*
* @param {string} sentence The sentence to match against.
* @param {Array} transitionWords The array containing transition words.
* @returns {Array} The found transitional words.
*/
const matchTransitionWords = function (sentence, transitionWords) {
sentence = (0, _quotes.normalizeSingle)(sentence);
return transitionWords.filter(word => (0, _matchWordInSentence.isWordInSentence)(word, sentence));
};
/**
* Checks the passed sentences to see if they contain transition words.
*
* @param {Array} sentences The sentences to match against.
* @param {Array} transitionWords The array containing transition words.
* @param {Array} twoPartTransitionWords The array containing two part transition words.
* @param {function} matchTransitionWordsHelper The language-specific helper function to match transition words in a sentence.
*
* @returns {Array} Array of sentence objects containing the complete sentence and the transition words.
*/
const checkSentencesForTransitionWords = function (sentences, transitionWords, twoPartTransitionWords, matchTransitionWordsHelper) {
const results = [];
sentences.forEach(sentence => {
if (twoPartTransitionWords) {
const twoPartMatches = matchTwoPartTransitionWords(sentence, twoPartTransitionWords);
if (twoPartMatches !== null) {
results.push({
sentence: sentence,
transitionWords: twoPartMatches
});
return;
}
}
const transitionWordMatches = matchTransitionWordsHelper ? matchTransitionWordsHelper(sentence, transitionWords) : matchTransitionWords(sentence, transitionWords);
if (transitionWordMatches.length !== 0) {
results.push({
sentence: sentence,
transitionWords: transitionWordMatches
});
}
});
return results;
};
/**
* Checks how many sentences from a text contain at least one transition word or two-part transition word
* that are defined in the transition words config and two part transition words config.
*
* @param {Paper} paper The Paper object to get text from.
* @param {Researcher} researcher The researcher.
*
* @returns {object} An object with the total number of sentences in the text
* and the total number of sentences containing one or more transition words.
*/
function _default(paper, researcher) {
const matchTransitionWordsHelper = researcher.getHelper("matchTransitionWordsHelper");
const transitionWords = researcher.getConfig("transitionWords");
const twoPartTransitionWords = researcher.getConfig("twoPartTransitionWords");
const memoizedTokenizer = researcher.getHelper("memoizedTokenizer");
let text = paper.getText();
text = (0, _htmlParser.default)(text);
text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes);
const sentences = (0, _getSentences.default)(text, memoizedTokenizer);
const sentenceResults = checkSentencesForTransitionWords(sentences, transitionWords, twoPartTransitionWords, matchTransitionWordsHelper);
return {
totalSentences: sentences.length,
sentenceResults: sentenceResults,
transitionWordSentences: sentenceResults.length
};
}
//# sourceMappingURL=findTransitionWords.js.map