-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow custom colors for actu tags
- Loading branch information
1 parent
1f948bf
commit bffdef2
Showing
8 changed files
with
164 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,79 @@ | ||
import sanitizeHtml from 'sanitize-html' | ||
|
||
import { ActualitesRaw, ArticleJson } from 'interfaces/actualites' | ||
import { | ||
ActualitesRaw, | ||
ArticleJson, | ||
EtiquetteArticle, | ||
TagJson, | ||
} from 'interfaces/actualites' | ||
import { StructureConseiller } from 'interfaces/conseiller' | ||
import { fetchJson } from 'utils/httpClient' | ||
|
||
export async function getActualites( | ||
structure: StructureConseiller | ||
): Promise<ActualitesRaw | undefined> { | ||
const url = ((): string => { | ||
switch (structure) { | ||
case StructureConseiller.MILO: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_MILO_LINK as string | ||
case StructureConseiller.POLE_EMPLOI: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FT_CEJ_LINK as string | ||
case StructureConseiller.POLE_EMPLOI_BRSA: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FR_BRSA_LINK as string | ||
case StructureConseiller.POLE_EMPLOI_AIJ: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FR_AIJ_LINK as string | ||
case StructureConseiller.CONSEIL_DEPT: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_CD_LINK as string | ||
case StructureConseiller.AVENIR_PRO: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_AVENIR_PRO_LINK as string | ||
} | ||
})() | ||
const urlTags = process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_TAGS | ||
const urlActualites = getUrlActualites(structure) | ||
if (!urlTags || !urlActualites) return | ||
|
||
if (!url) return | ||
const [{ content: tagsJson }, { content: articlesJson }]: [ | ||
{ content: TagJson[] }, | ||
{ content: ArticleJson[] }, | ||
] = await Promise.all([fetchJson(urlTags), fetchJson(urlActualites)]) | ||
if (!articlesJson.length) return | ||
|
||
const articlesJson = await fetchJson(url) | ||
|
||
const derniereDateModification = articlesJson.content.reduce( | ||
const derniereDateModification = articlesJson.reduce( | ||
(latest: string, item: ArticleJson) => { | ||
return new Date(item.modified) > new Date(latest) ? item.modified : latest | ||
}, | ||
articlesJson.content[0].modified | ||
articlesJson[0].modified | ||
) | ||
|
||
return { | ||
articles: articlesJson.content.map((article: ArticleJson) => ({ | ||
articles: articlesJson.map((article: ArticleJson) => ({ | ||
id: article.id, | ||
contenu: formaterArticle(article), | ||
titre: article.title.rendered, | ||
etiquettes: extraireEtiquettes(article, tagsJson), | ||
contenu: extraireContenuAssaini(article), | ||
})), | ||
dateDerniereModification: derniereDateModification, | ||
} | ||
} | ||
|
||
function formaterArticle({ content }: ArticleJson): string { | ||
const contentAssaini = sanitizeHtml(content.rendered, { | ||
function extraireContenuAssaini({ content }: ArticleJson): string { | ||
return sanitizeHtml(content.rendered, { | ||
disallowedTagsMode: 'recursiveEscape', | ||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']), | ||
}) | ||
|
||
return ajouterTagCategorie(contentAssaini) | ||
} | ||
|
||
function ajouterTagCategorie(str: string): string { | ||
const codeRegex = /<code\b[^>]*>([\s\S]*?)<\/code>/ | ||
|
||
const baliseCode = str.match(codeRegex) | ||
if (!baliseCode) return str | ||
const baliseCodeContent = baliseCode[1] | ||
|
||
const tags = baliseCodeContent.split(',').map((word) => word.trim()) | ||
|
||
const categories = tags | ||
.map((tag) => { | ||
return `<span className='flex items-center w-fit text-s-medium text-additional_3 px-3 bg-additional_3_lighten whitespace-nowrap rounded-full'>${tag}</span>` | ||
}) | ||
.join('') | ||
function extraireEtiquettes( | ||
article: ArticleJson, | ||
tagsJson: TagJson[] | ||
): EtiquetteArticle[] { | ||
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, | ||
})) | ||
} | ||
|
||
return str.replace( | ||
/<pre\b[^>]*>[\s\S]*?<\/pre>/g, | ||
`<div className='flex gap-2'>${categories}</div>` | ||
) | ||
function getUrlActualites(structure: StructureConseiller) { | ||
switch (structure) { | ||
case StructureConseiller.MILO: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_MILO_LINK as string | ||
case StructureConseiller.POLE_EMPLOI: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FT_CEJ_LINK as string | ||
case StructureConseiller.POLE_EMPLOI_BRSA: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FR_BRSA_LINK as string | ||
case StructureConseiller.POLE_EMPLOI_AIJ: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_FR_AIJ_LINK as string | ||
case StructureConseiller.CONSEIL_DEPT: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_CD_LINK as string | ||
case StructureConseiller.AVENIR_PRO: | ||
return process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_AVENIR_PRO_LINK as string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.