Skip to content

Commit

Permalink
fix(glossary): exclure les titres et sous-titres provenant des contri…
Browse files Browse the repository at this point in the history
…bs (#1316)
  • Loading branch information
carolineBda authored Mar 14, 2024
1 parent 41d6386 commit 8f3e62d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,21 @@ describe("Glossary", () => {
expect(addGlossary(markdown)).toEqual(markdown);
});

test("should not replace html property for cc word", () => {
test("should not add webcomponent tooltip in a span tag with class \"title\"", () => {
const htmlContent = `<span class="title">L'indemnité de fin de contrat n'est pas due dans les cas suivants</span>`;
expect(addGlossary(htmlContent)).toEqual(
`<span class="title">L'indemnité de fin de contrat n'est pas due dans les cas suivants</span>`
);
});

test("should not add webcomponent tooltip in a span tag with class \"sub-title\"", () => {
const htmlContent = `<span class="sub-title">L'indemnité de fin de contrat n'est pas due dans les cas suivants</span>`;
expect(addGlossary(htmlContent)).toEqual(
`<span class="sub-title">L'indemnité de fin de contrat n'est pas due dans les cas suivants</span>`
);
});

test("should not replace within tag attributes", () => {
const htmlContent =
'<p class="un accord de branche ou pas">voici une convention collective et un web component mais aussi dispositions, ceci est un test</p>';
expect(addGlossary(htmlContent)).toEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import type { Glossary, Term } from "../types";
import type { GlossaryTerms } from "./types";

const conventionMatchers =
"[C|c]onventions? [C|c]ollectives?|[A|a]ccords? de [B|b]ranches?|[D|d]ispositions? [C|c]onventionnelles?";
"[Cc]onventions? [Cc]ollectives?|[Aa]ccords? de [Bb]ranches?|[Dd]ispositions? [Cc]onventionnelles?";

const startWordBreaks = `(?<=^| |\\.|,|'|>|\\()`;
const endWordBreaks = `(?= |\\.|,|'|$|<|\\))`;

const endAnchorOmit = `(?![^<]*</a>|[^<]*</summary>|[^<]*</strong></summary>|[^<]*</h[1-6]>)`;
const startAnchorOmit = `(?<!<span class="(?:sub-)?title">[^<]*)`;
const endAnchorOmit = `(?![^<]*(?:</a>|</summary>|</strong></summary>|</h[1-6]>))`;

const tagAttributeOmit = `(?<=(^|>)[^><]*)`;

const startTag = `${tagAttributeOmit}${startWordBreaks}`;
const startTag = `${tagAttributeOmit}${startAnchorOmit}${startWordBreaks}`;
const endTag = `${endWordBreaks}${endAnchorOmit}`;

export const explodeGlossaryTerms = (glossary: Glossary): GlossaryTerms[] => {
Expand All @@ -23,26 +24,14 @@ export const explodeGlossaryTerms = (glossary: Glossary): GlossaryTerms[] => {

return glossaryTerms;
};

const regexSpecialChars = ["(", ")"];
const escapeRegexSpecialChars = (term: string) => {
const regexSpecialChars = [
".",
"+",
"*",
"?",
"^",
"$",
"(",
")",
"[",
"]",
"{",
"}",
"|",
];
return regexSpecialChars.reduce((term: string, specialChar: string) => {
return term.replace(new RegExp(`\\${specialChar}`), `\\${specialChar}`);
}, term);
return regexSpecialChars.reduce(
(escapedTerm: string, specialChar: string) => {
return escapedTerm.replace(specialChar, `\\${specialChar}`);
},
term
);
};

const explodeTerm = (term: Term): GlossaryTerms[] => {
Expand Down Expand Up @@ -72,7 +61,7 @@ const regexCapital = (term: string) => {
const firstCharUp = firstChar.toUpperCase();
const firstCharRegex =
firstCharLow !== firstCharUp
? `[${firstCharLow}|${firstCharUp}]`
? `[${firstCharLow}${firstCharUp}]`
: firstCharLow;
return `${
regexString ? `${regexString} ` : ""
Expand Down

0 comments on commit 8f3e62d

Please sign in to comment.