@playpilot/tpi 5.20.1 → 5.20.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playpilot/tpi",
3
- "version": "5.20.1",
3
+ "version": "5.20.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -254,7 +254,7 @@ export function injectLinksInDocument(elements: HTMLElement[], injections: LinkI
254
254
  }
255
255
  }
256
256
 
257
- addLinkInjectionEventListeners(validInjections)
257
+ addLinkInjectionEventListeners(foundInjections)
258
258
  addCSSVariablesToLinks()
259
259
 
260
260
  const afterArticleInjections = filterInvalidAfterArticleInjections(mergedInjections)
@@ -269,7 +269,7 @@ export function injectLinksInDocument(elements: HTMLElement[], injections: LinkI
269
269
 
270
270
  const matchingElement = document.querySelector(`[${keyDataAttribute}="${injection.key}"]`)
271
271
  const failed = isValidPlaylinkType(injection) && !injection.inactive && !injection.removed && !injection.after_article && !matchingElement
272
- const containsSentence = !!elements.find(element => cleanPhrase(element.innerText).includes(cleanPhrase(injection.sentence)))
272
+ const containsSentence = foundInjections.find(i => i.key === injection.key)
273
273
  const failedMessage =
274
274
  !failed ? '' :
275
275
  failedMessages[injection.key] ||
package/src/lib/text.ts CHANGED
@@ -61,7 +61,11 @@ export function replaceBetween(text: string, replacement: string, startIndex: nu
61
61
  export function cleanPhrase(phrase: string): string {
62
62
  return decodeHtmlEntities(phrase)
63
63
  .toLowerCase()
64
+ .normalize('NFD')
65
+ .replace(/\p{Diacritic}/gu, '') // Replace accents and other diacritic symbols (https://stackoverflow.com/a/51874002/1665157)
64
66
  .replace(/\s+/g, '') // Replace any number of white space with nothing
67
+ .replace(/['"‘’“”]/g, '"') // Replace all qoutes with the same type of quote
68
+ .replace(/^['"]+|['"]+$/g, '') // Remove leading and trailing quotes
65
69
  .replace(/\.*$/, '') // Remove trailing periods
66
70
  .replaceAll('…', '...') // Replace ellipsis character with regular characters
67
71
  }
@@ -312,6 +312,50 @@ describe('linkInjection.js', () => {
312
312
  expect(document.querySelectorAll('[data-playpilot-injection-key]')).toHaveLength(2)
313
313
  })
314
314
 
315
+ it('Should disregard missing trailing periods', () => {
316
+ document.body.innerHTML = '<p>This is a sentence with a phrase in it</p>'
317
+
318
+ const elements = Array.from(document.body.querySelectorAll('p'))
319
+ const injection = generateInjection('This is a sentence with a phrase in it.', 'a phrase')
320
+
321
+ injectLinksInDocument(elements, { aiInjections: [injection], manualInjections: [] })
322
+
323
+ expect(document.querySelectorAll('[data-playpilot-injection-key]')).toHaveLength(1)
324
+ })
325
+
326
+ it('Should disregard additional quotes', () => {
327
+ document.body.innerHTML = '<p>This is part of a paragraph. This is a sentence with a phrase in it."</p>'
328
+
329
+ const elements = Array.from(document.body.querySelectorAll('p'))
330
+ const injection = generateInjection('"This is a sentence with a phrase in it."', 'a phrase')
331
+
332
+ injectLinksInDocument(elements, { aiInjections: [injection], manualInjections: [] })
333
+
334
+ expect(document.querySelectorAll('[data-playpilot-injection-key]')).toHaveLength(1)
335
+ })
336
+
337
+ it('Should disregard differences in accents', () => {
338
+ document.body.innerHTML = '<p>This is à sentence with a phràse in it."</p>'
339
+
340
+ const elements = Array.from(document.body.querySelectorAll('p'))
341
+ const injection = generateInjection('This is a sentence with a phrase in it.', 'a phràse')
342
+
343
+ injectLinksInDocument(elements, { aiInjections: [injection], manualInjections: [] })
344
+
345
+ expect(document.querySelectorAll('[data-playpilot-injection-key]')).toHaveLength(1)
346
+ })
347
+
348
+ it('Should treat different types of quotes as the same', () => {
349
+ document.body.innerHTML = '<p>This is a “sentence” with a "phrase" in it.</p>'
350
+
351
+ const elements = Array.from(document.body.querySelectorAll('p'))
352
+ const injection = generateInjection('This is a "sentence" with a \'phrase’ in it.', 'phrase')
353
+
354
+ injectLinksInDocument(elements, { aiInjections: [injection], manualInjections: [] })
355
+
356
+ expect(document.querySelectorAll('[data-playpilot-injection-key]')).toHaveLength(1)
357
+ })
358
+
315
359
  it('Should leave the text intact if no injections were found', () => {
316
360
  document.body.innerHTML = '<p>This is a sentence with an injection.</p>'
317
361
 
@@ -112,6 +112,32 @@ describe('text.js', () => {
112
112
  it('Should return given phrase without trailing period', () => {
113
113
  expect(cleanPhrase('Some phrase.')).toBe('somephrase')
114
114
  })
115
+
116
+ it('Should return given phrase without leading or trailing quotes', () => {
117
+ expect(cleanPhrase('"Some phrase"')).toBe('somephrase')
118
+ expect(cleanPhrase('Some phrase"')).toBe('somephrase')
119
+ expect(cleanPhrase('"Some phrase')).toBe('somephrase')
120
+
121
+ expect(cleanPhrase('\'Some phrase\'')).toBe('somephrase')
122
+ expect(cleanPhrase('Some phrase\'')).toBe('somephrase')
123
+ expect(cleanPhrase('\'Some phrase')).toBe('somephrase')
124
+ })
125
+
126
+ it('Should return given phrase without leading or trailing quotes and trailing period', () => {
127
+ expect(cleanPhrase('"Some phrase."')).toBe('somephrase')
128
+ })
129
+
130
+ it('Should disregard accents and diacritics', () => {
131
+ expect(cleanPhrase('María')).toBe(cleanPhrase('Maria'))
132
+ expect(cleanPhrase('Göteborg')).toBe(cleanPhrase('Goteborg'))
133
+ expect(cleanPhrase('über')).toBe(cleanPhrase('uber'))
134
+ })
135
+
136
+ it('Should disregard differences in quotes', () => {
137
+ expect(cleanPhrase('Some "phrase"')).toBe(cleanPhrase('Some \'phrase\''))
138
+ expect(cleanPhrase('Some ”phrase“')).toBe(cleanPhrase('Some "phrase"'))
139
+ expect(cleanPhrase('Some ’phrase\'')).toBe(cleanPhrase('Some "phrase"'))
140
+ })
115
141
  })
116
142
 
117
143
  describe('truncateAroundPhrase', () => {