Skip to content

Commit

Permalink
fix(frontend): prefix code-du-trvail relatives link (#3060)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Answer from "../../src/common/Answer";
import Html from "../../src/common/Html";
import Metas from "../../src/common/Metas";
import { Layout } from "../../src/layout/Layout";
// import { replaceArticlesRefs } from "../../src/lib/replaceArticlesRefs";
import { replaceArticlesRefs } from "../../src/lib/replaceArticlesRefs";

const {
publicRuntimeConfig: { API_URL },
Expand Down Expand Up @@ -39,7 +39,7 @@ class Fiche extends React.Component {
},
} = this.props;

// const fixedHtml = replaceArticlesRefs(html);
const fixedHtml = replaceArticlesRefs("https://legifrance.gouv.fr", html);
return (
<Layout>
<Metas title={title} description={description} />
Expand All @@ -53,7 +53,7 @@ class Fiche extends React.Component {
})
}
emptyMessage="Article introuvable"
html={html}
html={fixedHtml}
source={{ name: "Code du travail", url }}
>
{notaHtml && (
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions packages/code-du-travail-frontend/src/lib/replaceArticlesRefs.js
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'`;
});

0 comments on commit 8f12410

Please sign in to comment.