-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): prefix code-du-trvail relatives link (#3060)
* fix(frontend): prefix code-du-trvail relatives link with legifrance.gouv.fr domain closes #3059 * fix quote * fix: handle multiple ref
- Loading branch information
Lionel
authored
Oct 23, 2020
1 parent
942b9cd
commit 8f12410
Showing
4 changed files
with
33 additions
and
44 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
13 changes: 0 additions & 13 deletions
13
...code-du-travail-frontend/src/lib/__tests__/__snapshots__/replaceArticlesRefs.test.js.snap
This file was deleted.
Oops, something went wrong.
36 changes: 20 additions & 16 deletions
36
packages/code-du-travail-frontend/src/lib/__tests__/replaceArticlesRefs.test.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 10 additions & 12 deletions
22
packages/code-du-travail-frontend/src/lib/replaceArticlesRefs.js
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,14 +1,12 @@ | ||
// assume we only have code-du-travail articles | ||
/** | ||
* prefix relative link with a given domain | ||
*/ | ||
|
||
// match basic article references | ||
const RE_ARTICLE_NUM = /<a[^>]+>(((l' *)?article )?([LRD])[\s-.*]*([\d-]+))[\s.]*<\/a>/gim; | ||
//(?:article )? | ||
const RE_ARTICLE_NUM = /href=["']((?<!https:\/\/)[^'"]+)["']/g; | ||
|
||
// fix single articles reference | ||
export const replaceArticlesRefs = (html) => | ||
(html && | ||
html.replace(RE_ARTICLE_NUM, (all, text, prefix1, prefix2, type, num) => { | ||
const slug = type.trim().toLowerCase() + num.trim().toLowerCase(); | ||
return `<a href="/code-du-travail/${slug}">${text}</a>`; | ||
})) || | ||
""; | ||
export const replaceArticlesRefs = (domain, html = "") => | ||
html.replace(RE_ARTICLE_NUM, (_, href) => { | ||
return `href='${domain}${ | ||
href.startsWith("/") ? "" : "/" | ||
}${href}' rel='nofollow noopener' target='_blank'`; | ||
}); |