Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ajouter un espace après les liens fiche cdt #6237

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ajouter un test si l'enfant n'est pas du texte
Ajouter un test si l'espace n'est pas dans le a

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
Loading