Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excludes image captions from keyphrase in introduction assessment #21414

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,57 @@ const paragraphWithExactParagraphMatchEN = "<p>" + sentenceWithExactMatchOfSomeK
sentenceWithSomeKeywordsEN + sentenceWithoutKeywordsEN + "/<p>";
const paragraphWithoutMatchEN = "<p>" + sentenceWithoutKeywordsEN + sentenceWithoutKeywordsEN + sentenceWithoutKeywordsEN + "/<p>";

describe( "a test for excluded elements", function() {
it( "should not recognize image captions as the introduction if it occurs at the beginning of the post (classic editor)", function() {
// The keyphrase is 'tortie cat', where it is added to the image caption. The first paragraph after the image doesn't contain the keyphrase.
// Hence, this test should return that the keyphrase was not found in the first paragraph.
const paper = new Paper( "\"[caption id=\"attachment_1205\" align=\"alignnone\" width=\"300\"]<img class=\"size-medium wp-image-1205\" src=\"https://basic.wordpress.test/wp-content/uploads/2024/05/cat-5579221_640-300x200.jpg\" alt=\"\" width=\"300\" height=\"200\" /> A great tortie cat.[/caption]<p> </p><p id=\"mntl-sc-block_18-0\" class=\"comp mntl-sc-block lifestyle-sc-block-html mntl-sc-block-html text-passage u-how-to-title-align\">In the early 2000's, researchers at the National Institutes of Health discovered that the genetic mutations that cause cats to have black coats may offer them some protection from diseases. In fact, the mutations affect the same genes that offer HIV resistance to some humans.</p>",
{ keyword: "tortie cat" } );
const researcher = new EnglishResearcher( paper );
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
} );
expect( firstParagraph( paper, researcher ).introduction.childNodes[ 0 ].value ).toEqual(
"In the early 2000's, researchers at the National Institutes of Health discovered that the genetic mutations that cause cats to have black coats may offer them some protection from diseases. In fact, the mutations affect the same genes that offer HIV resistance to some humans."
);
} );
it( "should not recognize image captions as the introduction if it occurs at the beginning of the post (block editor)", function() {
// The keyphrase is 'tortie cat', where it is added to the image caption. The first paragraph after the image doesn't contain the keyphrase.
// Hence, this test should return that the keyphrase was not found in the first paragraph.
const paper = new Paper( "<!-- wp:image {\"id\":1377,\"sizeSlug\":\"full\",\"linkDestination\":\"none\"} -->\n" +
"<figure class=\"wp-block-image size-full\"><img src=\"cat.png\" alt=\"\" class=\"wp-image-1377\"/><figcaption class=\"wp-element-caption\">A great tortie cat.</figcaption></figure>\n" +
"<!-- /wp:image -->\n" +
"\n" +
"<!-- wp:paragraph -->\n" +
"<p>Some other text.</p>\n" +
"<!-- /wp:paragraph -->",
{ keyword: "tortie cat" } );
const researcher = new EnglishResearcher( paper );
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
} );
expect( firstParagraph( paper, researcher ).introduction.childNodes[ 0 ].value ).toEqual( "Some other text." );
} );
} );

describe( "checks for the content words from the keyphrase in the first paragraph (English)", function() {
it( "returns whether all keywords were matched in one sentence", function() {
const paper = new Paper( paragraphWithSentenceMatchEN, { keyword: keyphraseEN } );
const researcher = new EnglishResearcher( paper );
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -47,7 +90,7 @@ describe( "checks for the content words from the keyphrase in the first paragrap
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -60,7 +103,7 @@ describe( "checks for the content words from the keyphrase in the first paragrap
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -74,7 +117,7 @@ describe( "checks for the content words from the keyphrase in the first paragrap
const researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -86,7 +129,7 @@ describe( "checks for the content words from the keyphrase in the first paragrap
const researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -98,7 +141,7 @@ describe( "checks for the content words from the keyphrase in the first paragrap
const researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -113,7 +156,7 @@ describe( "checks for the content words from a synonym phrase in the first parag
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "synonym",
Expand All @@ -126,7 +169,7 @@ describe( "checks for the content words from a synonym phrase in the first parag
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: true,
keyphraseOrSynonym: "synonym",
Expand All @@ -139,7 +182,7 @@ describe( "checks for the content words from a synonym phrase in the first parag
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -154,7 +197,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -167,7 +210,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -180,7 +223,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -193,7 +236,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -216,7 +259,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -236,7 +279,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -257,7 +300,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -281,7 +324,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -300,7 +343,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -319,7 +362,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -339,7 +382,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -360,7 +403,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -379,7 +422,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -399,7 +442,7 @@ describe( "tests for edge cases", function() {
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -415,7 +458,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
researcher.addResearchData( "morphology", morphologyData );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -428,7 +471,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
const researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -443,7 +486,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
const researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "synonym",
Expand All @@ -455,7 +498,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
let researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual(
expect( firstParagraph( paper, researcher ) ).toMatchObject(
{
foundInOneSentence: true,
foundInParagraph: true,
Expand All @@ -467,7 +510,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -477,7 +520,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -487,7 +530,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
researcher = new EnglishResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -501,7 +544,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
researcher.addResearchData( "morphology", morphologyDataJA );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: false,
foundInParagraph: false,
keyphraseOrSynonym: "",
Expand All @@ -514,7 +557,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
const researcher = new JapaneseResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "keyphrase",
Expand All @@ -528,7 +571,7 @@ describe( "a test for the keyphrase in first paragraph research when the exact m
const researcher = new JapaneseResearcher( paper );
buildTree( paper, researcher );

expect( firstParagraph( paper, researcher ) ).toEqual( {
expect( firstParagraph( paper, researcher ) ).toMatchObject( {
foundInOneSentence: true,
foundInParagraph: true,
keyphraseOrSynonym: "synonym",
Expand Down
3 changes: 2 additions & 1 deletion packages/yoastseo/src/languageProcessing/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import matchStringWithRegex from "./regex/matchStringWithRegex";
import { normalize } from "./sanitize/quotes";
import { filterShortcodesFromHTML } from "./sanitize/filterShortcodesFromTree";
import { createShortcodeTagsRegex, filterShortcodesFromHTML } from "./sanitize/filterShortcodesFromTree";
import removeHtmlBlocks from "./html/htmlParser";

export {
matchStringWithRegex,
normalize,
removeHtmlBlocks,
filterShortcodesFromHTML,
createShortcodeTagsRegex,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {string[]} shortcodeTags The tags of the shortcodes to filter.
* @returns {RegExp} The regex to recognize the shortcodes.
*/
const createShortcodeTagsRegex = shortcodeTags => {
export const createShortcodeTagsRegex = shortcodeTags => {
const shortcodeTagsRegexString = `\\[\\/?(${ shortcodeTags.join( "|" ) })[^\\]]*\\]`;
return new RegExp( shortcodeTagsRegexString, "g" );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getStartOffset( node ) {
* @param {Node} node The current node.
* @returns {Node} The parent node.
*/
function getParentNode( paper, node ) {
export function getParentNode( paper, node ) {
// Includes a fallback so that if a parent node cannot be found for an implicit paragraph, we use the current node as the parent node.
return paper.getTree().findAll( treeNode => treeNode.childNodes && treeNode.childNodes.includes( node ) )[ 0 ] || node;
}
Expand Down
Loading
Loading