Skip to content

Commit 75da360

Browse files
committed
fix(chatbot): detectChoiceOption exclut maintenant aussi les liens mailto: et tel:
1 parent 615344f commit 75da360

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

app/js/core/chatbot/parsers/helpers/detectChoiceOption.mjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import { startsWithAnyOf } from "../../../../utils/strings.mjs";
2+
13
const regexOrderedList = /^\d{1,3}[.)]\s\[[^\]]+\]\(([^)]*)\)\s*$/;
24
const regexOrderedListRandom = /^\d{1,3}\)/;
5+
const externalLinks = ["http", "mailto:", "tel:"];
6+
7+
function isExternalLink(url) {
8+
url = url.trim().toLowerCase();
9+
return startsWithAnyOf(url, externalLinks);
10+
}
311

412
export function detectChoiceOption(line) {
513
const match = line.match(regexOrderedList);
6-
const isChoice =
7-
match && !match[1].trim().toLowerCase().startsWith("http") ? true : false;
14+
let isChoice = false;
15+
16+
if (match) {
17+
const url = match[1];
18+
isChoice = !isExternalLink(url);
19+
}
20+
821
return {
922
isChoice: isChoice,
1023
isRandom: isChoice && regexOrderedListRandom.test(line),

tests/unit/core/chatbot/parsers/helpers/detectChoiceOption.spec.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ describe("detectChoiceOption", () => {
109109
expect(result.isRandom).toBeFalse();
110110
});
111111

112-
it("does not detect an ordered list with link to a URL starting with http… as a choice option", () => {
113-
const result = detectChoiceOption("1. [text](https://example.com)");
114-
expect(result.isChoice).toBeFalse();
115-
expect(result.isRandom).toBeFalse();
112+
it("does not detect an ordered list with link to an external URL, as a choice option", () => {
113+
const result1 = detectChoiceOption("1. [text](https://example.com)");
114+
expect(result1.isChoice).toBeFalse();
115+
expect(result1.isRandom).toBeFalse();
116+
const result2 = detectChoiceOption("1. [text](mailto:my@email.com)");
117+
expect(result2.isChoice).toBeFalse();
118+
expect(result2.isRandom).toBeFalse();
119+
const result3 = detectChoiceOption("1. [text](tel:12312399R)");
120+
expect(result3.isChoice).toBeFalse();
121+
expect(result3.isRandom).toBeFalse();
116122
});
117123
});

0 commit comments

Comments
 (0)