@playpilot/tpi 8.29.0-beta.1 → 8.29.0-beta.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": "8.29.0-beta.1",
3
+ "version": "8.29.0-beta.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -7,6 +7,7 @@ import { clearAfterArticlePlaylinks, insertAfterArticlePlaylinks } from './after
7
7
  import { clearInTextDisclaimer, insertInTextDisclaimer } from './disclaimer'
8
8
  import { exploreTitleUrl, titleUrl } from './routes'
9
9
  import { clearInTextWidgets, insertInTextWidgets } from './inTextWidgets'
10
+ import { decodeHtmlEntities, encodeHtmlEntities } from './html'
10
11
 
11
12
  export const keyDataAttribute = 'data-playpilot-injection-key'
12
13
  export const keySelector = `[${keyDataAttribute}]`
@@ -84,9 +85,21 @@ export function injectLinksInDocument(elements: HTMLElement[], injections: LinkI
84
85
  // We insert the html here separately from below, where it's done with replacementIndex because we need to capture all html
85
86
  // that may have been inside of the match.
86
87
  if (replacementIndex === -1) {
87
- const match = findShortestMatchBetweenPhrases(element.innerHTML, injection.title, phrase_before || '', phrase_after || '')
88
+ let match = findShortestMatchBetweenPhrases(element.innerHTML, injection.title, phrase_before || '', phrase_after || '')
88
89
 
89
90
  if (match) {
91
+ // This is a crude way of checking if a match contains HTML elements
92
+ // If the title is directly inside of an element we discard the rest.
93
+ // This happens sometimes in lists that consists of nothing but titles and may contain special characters
94
+ if (match.match.includes(">") && match.match.includes("<")) {
95
+ const encodedTitle = encodeHtmlEntities(injection.title)
96
+ const matchInsideHTMLIndex = match.match.search(new RegExp(`>\s*${encodedTitle}\s*<`)) + 1
97
+
98
+ if (matchInsideHTMLIndex > 0) {
99
+ match = { match: match.match.slice(matchInsideHTMLIndex, matchInsideHTMLIndex + encodedTitle.length), index: matchInsideHTMLIndex }
100
+ }
101
+ }
102
+
90
103
  const { leadingSpaces, trailingSpaces } = getNumberOfLeadingAndTrailingSpaces(match.match)
91
104
 
92
105
  linkElement.innerHTML = match.match.trim()
@@ -867,6 +867,26 @@ describe('injection.ts', () => {
867
867
  expect(document.querySelectorAll('a')).toHaveLength(4)
868
868
  })
869
869
 
870
+ it('Should properly inject into lists of titles that contain special characters', () => {
871
+ document.body.innerHTML = `<section>
872
+ <ul>
873
+ <li>Daisy Jones & The Six</li>
874
+ <li>Love & Death</li>
875
+ </ul>
876
+ </section>
877
+ `
878
+
879
+ const elements = getLinkInjectionElements(/** @type {HTMLElement} */ (document.querySelector('section')))
880
+ const injections = [
881
+ generateInjection('Daisy Jones & The Six', 'Daisy Jones & The Six'),
882
+ generateInjection('Love & Death', 'Love & Death'),
883
+ ]
884
+
885
+ injectLinksInDocument(elements, { aiInjections: [], manualInjections: injections })
886
+
887
+ expect(document.querySelectorAll('a')).toHaveLength(2)
888
+ })
889
+
870
890
  it('Should properly inject into titles that are split up by multiple elements when phrase_before and phrase_after are given', () => {
871
891
  document.body.innerHTML = '<section>Some text with a <strong>phra</strong><strong>se</strong> in it</section>'
872
892