Skip to content

Commit

Permalink
feat: remove empty paragraphs in actu
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlbrjc committed Dec 5, 2024
1 parent 5f3fa86 commit 505ee03
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fixtures/actualites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function uneActualiteRaw(): ActualitesRaw {
titre: 'Invitation à la journée présentiel du 31 octobre 2024',
etiquettes: [],
contenu:
'<p>Rdv demain aux nouveaux locaux de la Fabrique</p><a href="perdu.com">Vous êtes perdu ?</a>',
'<p /><p>Rdv demain aux nouveaux locaux de la Fabrique</p> <p/><a href="perdu.com">Vous êtes perdu ?</a><p></p>',
},
],
dateDerniereModification: '2024-10-30',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pass-emploi-web",
"version": "5.1.1",
"version": "5.1.2",
"private": true,
"engines": {
"node": "22.11.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/actualitesContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ describe('ActualitesProvdider', () => {
)
})

it('recupère les actualités et transforme les liens', () => {
it('recupère les actualités, transforme les liens et supprime les paragraphes vides', () => {
// Then
expect(getActualites).toHaveBeenCalledWith(conseiller.structure)
expect(screen.getAllByRole('paragraph')).toHaveLength(1)
expect(screen.getByRole('paragraph')).toHaveTextContent(
'Rdv demain aux nouveaux locaux de la Fabrique'
)
Expand Down
12 changes: 11 additions & 1 deletion utils/actualitesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function ActualitesProvider({
articles: actualitesRaw.articles.map((article) => ({
...article,
titre: parse(article.titre),
contenu: parse(transformerLiensExternes(article.contenu)),
contenu: parse(nettoyerEtFormater(article.contenu)),
})),
}
)
Expand All @@ -56,6 +56,16 @@ export function useActualites(): ActualitesParsees | undefined {
return useContext(ActualitesContext)
}

function nettoyerEtFormater(str: string): string {
let resultat = supprimerParagraphesVides(str)
resultat = transformerLiensExternes(resultat)
return resultat
}

function supprimerParagraphesVides(str: string): string {
return str.replace(/(<p>\s*<\/p>)|(<p\s*\/>)/g, '')
}

function transformerLiensExternes(str: string): string {
const aTagRegex = /<a\b[^>]*href=['"]([^'"]+)['"][^>]*>([\s\S]*?)<\/a>/g

Expand Down

0 comments on commit 505ee03

Please sign in to comment.