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 9f957e3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 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
8 changes: 2 additions & 6 deletions services/actualites.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getActualites(
] = await Promise.all([fetchJson(urlTags), fetchJson(urlActualites)])
if (!articlesJson.length) return

const articlesTries = articlesJson.sort(comparerArticles)
const articlesTries = articlesJson.toSorted(comparerArticles)
return {
articles: articlesTries.map((article: ArticleJson) => ({
id: article.id,
Expand Down Expand Up @@ -55,11 +55,7 @@ function extraireEtiquettes(
return article.tags
.map((idTag) => tagsJson.find(({ id }) => id === idTag))
.filter((tag) => tag !== undefined)
.map((tag) => ({
id: tag!.id,
nom: tag!.name,
couleur: tag!.description,
}))
.map((tag) => ({ id: tag.id, nom: tag.name, couleur: tag.description }))
}

function getUrlActualites(structure: StructureConseiller) {
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 9f957e3

Please sign in to comment.