1- import { escapeRegExp } from "lodash-es" ;
2- import addMark from "../../../markers/addMarkSingleWord" ;
3- import Mark from "../../../values/Mark" ;
4- import getAnchorsFromText from "../link/getAnchorsFromText" ;
51import matchWords from "../match/matchTextWithArray" ;
62import arrayToRegex from "../regex/createRegexFromArray" ;
7-
8- // Regex to deconstruct an anchor into open tag, content and close tag.
9- // [^] matches any character, including newline
10- const anchorDeconstructionRegex = / ( < a [ \s ] + [ ^ > ] + > ) ( [ ^ ] * ?) ( < \/ a > ) / ;
11-
12- /**
13- * Deconstructs an anchor to the opening tag and the content. The content is the anchor text.
14- * We don't return the closing tag since the value would always be the same, i.e. </a>.
15- *
16- * @param {string } anchor An anchor of the shape <a ...>...</a>.
17- *
18- * @returns {object } An object containing the opening tag and the content.
19- */
20- export const deConstructAnchor = function ( anchor ) {
21- // The const array mirrors the anchorDeconstructionRegex, using a comma to access the first element without a name.
22- const [ , openTag , content ] = anchor . match ( anchorDeconstructionRegex ) ;
23- return {
24- openTag : openTag ,
25- content : content ,
26- } ;
27- } ;
28-
29- /**
30- * Reconstructs an anchor from an openTag, the content, and the closing tag.
31- *
32- * @param {string } openTag The opening tag of the anchor. Must be of the shape <a ...>.
33- * @param {string } content The text of the anchor.
34- *
35- * @returns {string } An anchor.
36- */
37- export const reConstructAnchor = function ( openTag , content ) {
38- return `${ openTag } ${ content } </a>` ;
39- } ;
40-
41-
42- /**
43- * Gets the anchors and marks the anchors' text if the words are found in it.
44- *
45- * @param {string } sentence The sentence to retrieve the anchors from.
46- * @param {RegExp } wordsRegex The regex of the words.
47- *
48- * @returns {Object } The anchors and the marked anchors.
49- */
50- const getMarkedAnchors = function ( sentence , wordsRegex ) {
51- // Retrieve the anchors.
52- const anchors = getAnchorsFromText ( sentence ) ;
53- // For every anchor, apply the markings only to the anchor tag.
54- const markedAnchors = anchors . map ( anchor => {
55- // Retrieve the open tag and the content/anchor text.
56- const { openTag, content } = deConstructAnchor ( anchor ) ;
57-
58- // Apply the marking to the anchor text if there is a match.
59- const markedAnchorText = content . replace ( wordsRegex , ( x ) => addMark ( x ) ) ;
60-
61- // Create a new anchor tag with a (marked) anchor text.
62- return reConstructAnchor ( openTag , markedAnchorText ) ;
63- } ) ;
64- return { anchors, markedAnchors } ;
65- } ;
3+ import addMark from "../../../markers/addMarkSingleWord" ;
4+ import Mark from "../../../values/Mark" ;
5+ import { escapeRegExp } from "lodash-es" ;
666
677/**
688 * Adds marks to a sentence and merges marks if those are only separated by a space
699 * (e.g., if highlighting words "ballet" and "shoes" in a sentence "I have a lot of ballet shoes and other paraphernalia."
7010 * the marks will be put around "ballet shoes" together, not "`ballet` `shoes`".)
7111 *
7212 * @param {string } sentence The sentence to mark words in.
73- * @param {[string] } wordsFoundInSentence The words to mark in the sentence.
13+ * @param {[string] } topicFoundInSentence The words to mark in the sentence.
7414 * @param {function } matchWordCustomHelper The language-specific helper function to match word in text.
7515 *
7616 * @returns {string } The sentence with marks.
7717 */
78- export const collectMarkingsInSentence = function ( sentence , wordsFoundInSentence , matchWordCustomHelper ) {
79- wordsFoundInSentence = wordsFoundInSentence . map ( word => escapeRegExp ( word ) ) ;
18+ export const collectMarkingsInSentence = function ( sentence , topicFoundInSentence , matchWordCustomHelper ) {
19+ topicFoundInSentence = topicFoundInSentence . map ( word => escapeRegExp ( word ) ) ;
8020 // If a language has a custom helper to match words, we disable the word boundary when creating the regex.
81- const wordsRegex = matchWordCustomHelper ? arrayToRegex ( wordsFoundInSentence , true ) : arrayToRegex ( wordsFoundInSentence ) ;
82-
83- // Retrieve the anchors and mark the anchors' text if the words are found in the anchors' text.
84- const { anchors, markedAnchors } = getMarkedAnchors ( sentence , wordsRegex ) ;
85-
86- let markup = sentence . replace ( wordsRegex , function ( x ) {
21+ const topicRegex = matchWordCustomHelper ? arrayToRegex ( topicFoundInSentence , true ) : arrayToRegex ( topicFoundInSentence ) ;
22+ const markup = sentence . replace ( topicRegex , function ( x ) {
8723 return addMark ( x ) ;
8824 } ) ;
8925
90- /**
91- * In 'markup', we apply the markings also inside the anchor's attribute if there is a match, on top of
92- * marking the anchor's text.
93- * The step below is to replace the incorrectly marked anchors with the marked anchors that we want:
94- * where the markings are only applied in the anchor's text.
95- */
96- if ( anchors . length > 0 ) {
97- const markupAnchors = getAnchorsFromText ( markup ) ;
98- for ( let i = 0 ; i < markupAnchors . length ; i ++ ) {
99- markup = markup . replace ( markupAnchors [ i ] , markedAnchors [ i ] ) ;
100- }
101- }
102-
103- /*
104- * If two marks are separated by only a space, remove the closing tag of the first mark and the opening tag of the
105- * second mark so that the two marks can be combined into one.
106- */
10726 return ( markup . replace ( new RegExp ( "</yoastmark> <yoastmark class='yoast-text-mark'>" , "ig" ) , " " ) ) ;
10827} ;
10928
@@ -118,15 +37,16 @@ export const collectMarkingsInSentence = function( sentence, wordsFoundInSentenc
11837 * @returns {[string] } The sentences with marks.
11938 */
12039export function markWordsInSentences ( wordsToMark , sentences , locale , matchWordCustomHelper ) {
121- let wordsFoundInSentence = [ ] ;
40+ let topicFoundInSentence = [ ] ;
12241 let markings = [ ] ;
12342
12443 sentences . forEach ( function ( sentence ) {
125- wordsFoundInSentence = matchWords ( sentence , wordsToMark , locale , matchWordCustomHelper ) . matches ;
126- if ( wordsFoundInSentence . length > 0 ) {
44+ topicFoundInSentence = matchWords ( sentence , wordsToMark , locale , matchWordCustomHelper ) . matches ;
45+
46+ if ( topicFoundInSentence . length > 0 ) {
12747 markings = markings . concat ( new Mark ( {
12848 original : sentence ,
129- marked : collectMarkingsInSentence ( sentence , wordsFoundInSentence , matchWordCustomHelper ) ,
49+ marked : collectMarkingsInSentence ( sentence , topicFoundInSentence , matchWordCustomHelper ) ,
13050 } ) ) ;
13151 }
13252 } ) ;
0 commit comments