Skip to content

Commit

Permalink
fix: ajouter un espace après les liens fiche cdt (#6237)
Browse files Browse the repository at this point in the history
* fix: ajouter un espace après les liens fiche cdt

* refactor: utilisation de html-react-parser

* chore: review

---------

Co-authored-by: victor <[email protected]>
  • Loading branch information
Viczei and victor authored Oct 31, 2024
1 parent 520afad commit 482396f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import parse, { domToReact } from "html-react-parser";
import { ReactHTMLElement } from "react";

export const ContentParser = ({
children,
}): string | JSX.Element | JSX.Element[] => {
const options = {
replace: (domNode) => {
if (
domNode.name === "a" &&
domNode.children &&
domNode.children.length > 0
) {
let tagElement = domNode.children[0];
while (tagElement.type !== "text") {
tagElement = tagElement.children[0];
}
const text = tagElement.data;
tagElement.data = text.trim();
const textToTrim = text[text.length - 1] === " ";
return (
<>
<a {...domNode.attribs}>{domToReact(domNode.children)}</a>

{textToTrim ? " " : ""}
</>
);
}
},
};
return <>{parse(children, options)}</>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { render } from "@testing-library/react";
import { ContentParser } from "../ContentParser";

describe("CDT content parser", () => {
test("should move space at the end of link to the end of link tag", () => {
const html = `<a href="https://test.com">text </a>to test`;
const { baseElement } = render(<ContentParser>{html}</ContentParser>);

expect(baseElement.outerHTML).toEqual(
`<body><div><a href="https://test.com">text</a> to test</div></body>`
);
});

test("should not add space at the end of link to the end of link tag if no space to trim", () => {
const html = `<a href="https://test.com">text</a> to test`;
const { baseElement } = render(<ContentParser>{html}</ContentParser>);

expect(baseElement.outerHTML).toEqual(
`<body><div><a href="https://test.com">text</a> to test</div></body>`
);
});

test("should move space at the end of link to the end of link tag even within tag", () => {
const html = `<a href="https://test.com"><b>text </b></a>to test`;
const { baseElement } = render(<ContentParser>{html}</ContentParser>);

expect(baseElement.outerHTML).toEqual(
`<body><div><a href="https://test.com"><b>text</b></a> to test</div></body>`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fr } from "@codegouvfr/react-dsfr";
import Html from "../common/Html";
import { ContainerRich } from "../layout/ContainerRich";
import { RelatedItem } from "../documents";
import { ContentParser } from "./ContentParser";

type Props = {
metaDescription: string;
Expand Down Expand Up @@ -40,7 +41,7 @@ export function ArticleCodeDuTravail({
</p>

<div className={fr.cx("fr-mb-5w")}>
<Html>{html}</Html>
<ContentParser>{html}</ContentParser>
</div>
{notaHtml && (
<div className={fr.cx("fr-highlight", "fr-mb-5w")}>
Expand Down

0 comments on commit 482396f

Please sign in to comment.