Skip to content

Commit

Permalink
Merge 23dbc1a into d051aea
Browse files Browse the repository at this point in the history
  • Loading branch information
tconte-pass authored Nov 6, 2024
2 parents d051aea + 23dbc1a commit 36279c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/libs/parsers/highlightLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ type ParsedDescription = Array<string | React.ReactNode>

const externalNavRegex = new RegExp(
/((^|\s)|https?:\/\/)[a-z]([-a-z0-9:%._+~#=]*[a-z0-9])?\.[a-z0-9]{1,6}([/?#]\S*)?(\s|$)/,
'gmi'
'gm'
)

export const customFindUrlChunks = ({ textToHighlight }: FindChunksArgs): Chunk[] => {
// code adapted from https://github.com/bvaughn/highlight-words-core/blob/master/src/utils.js
const chunks = []
let match: RegExpExecArray | null
while ((match = externalNavRegex.exec(textToHighlight))) {
// @ts-expect-error: because of noUncheckedIndexedAccess
const startWithSpace = /\s/.test(textToHighlight[match.index])
const startWithSpace = /\s/.test(textToHighlight[match.index] || '')
const startIndexSpaceAdjustment = startWithSpace ? 1 : 0
const start = match.index + startIndexSpaceAdjustment
// @ts-expect-error: because of noUncheckedIndexedAccess
const endWithSpace = /\s/.test(textToHighlight[externalNavRegex.lastIndex - 1])
const endWithSpace = /\s/.test(textToHighlight[externalNavRegex.lastIndex - 1] || '')
const endIndexSpaceAdjustment = endWithSpace ? 1 : 0
const end = externalNavRegex.lastIndex - endIndexSpaceAdjustment
// We do not return zero-length matches
Expand All @@ -40,7 +38,7 @@ export const customFindUrlChunks = ({ textToHighlight }: FindChunksArgs): Chunk[
const normalizeURL = (partialURL: string): string => {
if (partialURL.startsWith('http://')) return partialURL
if (partialURL.startsWith('https://')) return partialURL
return `http://${partialURL}`
return `${partialURL}`
}

export const highlightLinks = (description: string): ParsedDescription => {
Expand Down
43 changes: 26 additions & 17 deletions src/libs/parsers/tests/highlightLinks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const description1WithoutUrl = `PRESSE / ILS EN PARLENT !
« Hilarant » Vanity Fair
Voir plus de critiques en suivant le lien suivant :`

const descriptionWithoutSpaceAfterDot = `Lorem ipsum dolor sit amet consectetur adipiscing elit.Sed non risus.`

const descriptionWitUppercaseLink = `Lorem ipsum dolor sit amet consectetur adipiscing Elit.sed non risus.`

const description1 = description1WithoutUrl + 'https://fauxliencritique.com/'

const description2 = `https://www.google.com/ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Expand All @@ -37,8 +41,7 @@ describe('customFindUrlChunks', () => {
})

expect(highlightedChunks1).toHaveLength(1)
// @ts-expect-error: because of noUncheckedIndexedAccess
expect(description1.slice(0, highlightedChunks1[0].start)).toBe(description1WithoutUrl)
expect(description1.slice(0, highlightedChunks1[0]?.start)).toBe(description1WithoutUrl)
})

it('finds url chunks and mark them as highlited', () => {
Expand All @@ -61,7 +64,7 @@ describe('highlightLinks', () => {
const parsedDescription = highlightLinks('perdu.com')

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://perdu.com" />,
<ExternalLink key="external-link-0" url="perdu.com" />,
])
})

Expand All @@ -86,31 +89,31 @@ describe('highlightLinks', () => {
const parsedDescription = highlightLinks('www.penofchaos.com')

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://www.penofchaos.com" />,
<ExternalLink key="external-link-0" url="www.penofchaos.com" />,
])
})

it('with path', () => {
const parsedDescription = highlightLinks('httpstat.us/418')

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://httpstat.us/418" />,
<ExternalLink key="external-link-0" url="httpstat.us/418" />,
])
})

it('with query params', () => {
const parsedDescription = highlightLinks('httpstat.us/200?sleep=500')

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://httpstat.us/200?sleep=500" />,
<ExternalLink key="external-link-0" url="httpstat.us/200?sleep=500" />,
])
})

it('with hash', () => {
const parsedDescription = highlightLinks('httpstat.us#anchor')

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://httpstat.us#anchor" />,
<ExternalLink key="external-link-0" url="httpstat.us#anchor" />,
])
})
})
Expand All @@ -124,10 +127,7 @@ describe('highlightLinks', () => {

expect(parsedDescription).toEqual([
`${description1WithoutUrl} `,
<ExternalLink
key="external-link-1"
url="http://www.penofchaos.com/warham/donjon.htm"
/>,
<ExternalLink key="external-link-1" url="www.penofchaos.com/warham/donjon.htm" />,
])
})

Expand All @@ -145,12 +145,9 @@ describe('highlightLinks', () => {
const parsedDescription = highlightLinks(description)

expect(parsedDescription).toEqual([
<ExternalLink
key="external-link-0"
url="http://www.penofchaos.com/warham/donjon.htm"
/>,
<ExternalLink key="external-link-0" url="www.penofchaos.com/warham/donjon.htm" />,
'\n',
<ExternalLink key="external-link-2" url="http://perdu.com" />,
<ExternalLink key="external-link-2" url="perdu.com" />,
])
})
})
Expand All @@ -177,7 +174,7 @@ describe('highlightLinks', () => {
)

expect(parsedDescription).toEqual([
<ExternalLink key="external-link-0" url="http://www.penofchaos.com/warham/donjon.htm" />,
<ExternalLink key="external-link-0" url="www.penofchaos.com/warham/donjon.htm" />,
` ${description1WithoutUrl}`,
])
})
Expand All @@ -188,5 +185,17 @@ describe('highlightLinks', () => {

expect(parsedDescription).toEqual(['[email protected]'])
})

it('exclude uppercase after dot', () => {
const parsedDescription = highlightLinks(descriptionWithoutSpaceAfterDot)

expect(parsedDescription).toEqual([descriptionWithoutSpaceAfterDot])
})

it('exclude uppercase at link start', () => {
const parsedDescription = highlightLinks(descriptionWitUppercaseLink)

expect(parsedDescription).toEqual([descriptionWitUppercaseLink])
})
})
})

0 comments on commit 36279c7

Please sign in to comment.