-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AI Proofread: Target specific term occurrence (#38397)
* rename file * add helper functions * fix complex word entry * add the occurrence to the request payload * changelog
- Loading branch information
Showing
9 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/update-jetpack-ai-proofread-target-position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
AI Proofread: Target specific term occurrence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...pack/extensions/plugins/ai-assistant-plugin/components/breve/utils/get-node-text-index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Get the starting text index of a node's content within its parent's text content. | ||
* Used to determine the position of a highlight within a sentence. | ||
*/ | ||
export const getNodeTextIndex = ( parent: HTMLElement, node: HTMLElement ) => { | ||
const nodes = Array.from( parent.childNodes ); | ||
const nodePosition = nodes.indexOf( node ); | ||
|
||
const nodesBefore = nodes.slice( 0, nodePosition ); | ||
const container = document.createElement( 'div' ); | ||
|
||
nodesBefore.forEach( n => container.appendChild( n.cloneNode( true ) ) ); | ||
|
||
return container.innerText.length; | ||
}; |
13 changes: 13 additions & 0 deletions
13
...ck/extensions/plugins/ai-assistant-plugin/components/breve/utils/get-non-link-ancestor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Helper function to get the first non-link ancestor of an element. | ||
* Used to determine the sentence context for a given word inside a link. | ||
*/ | ||
export const getNonLinkAncestor = ( element: HTMLElement ) => { | ||
let parent: HTMLElement | null = element?.parentElement ?? null; | ||
|
||
while ( parent && parent.tagName === 'A' ) { | ||
parent = parent.parentElement; | ||
} | ||
|
||
return parent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...etpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/number-to-ordinal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Converts a number to an ordinal string. | ||
* Used to inform the AI model of the position of the right word in a sentence. | ||
*/ | ||
export const numberToOrdinal = ( number: number ) => { | ||
const suffix = [ 'th', 'st', 'nd', 'rd' ]; | ||
const lastTwoDigits = number % 100; | ||
|
||
return ( | ||
number + ( suffix[ ( lastTwoDigits - 20 ) % 10 ] || suffix[ lastTwoDigits ] || suffix[ 0 ] ) | ||
); | ||
}; |