Skip to content

Commit c66201a

Browse files
committed
fix(markdown): pas de prise en compte des parties dans un message avec de l'aléatoire qui ne contiennent que des commentaires HTML
1 parent 9d764b9 commit c66201a

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

app/js/markdown/custom/random.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { getRandomElement } from "../../utils/arrays.mjs";
33
// Gestion du cas où il y a plusieurs messages possibles de réponse, séparés par "---"
44
export function processRandomMessage(message) {
55
const SEPARATOR = "\n---\n";
6-
// Découpe en variantes et ignore les parties vides (espaces / retours à la ligne)
6+
// Découpe en variantes et ignore les parties vides (espaces / retours à la ligne) ou seulement avec des commentaires
77
const messageVariants = message
88
.split(SEPARATOR)
9-
.map((part) => part.trim())
9+
.map((part) => part.replace(/<!--[\s\S]*?-->/g, "").trim())
1010
.filter((part) => part.length > 0);
1111

1212
if (messageVariants.length > 1) {
@@ -17,7 +17,7 @@ export function processRandomMessage(message) {
1717
// On découpe la partie 'baseMessage' en variantes et on ignore les parties vides
1818
const baseMessageVariants = baseMessage
1919
.split("---")
20-
.map((part) => part.trim())
20+
.map((part) => part.replace(/<!--[\s\S]*?-->/g, "").trim())
2121
.filter((part) => part.length > 0);
2222
if (baseMessageVariants.length > 0) {
2323
message = getRandomElement(baseMessageVariants) + choiceOptionsHtml;

app/script.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/script.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/unit/markdown/custom/random.spec.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ ${choiceOptionsHTML}`;
4848
expect(output).toContain(`${choiceOptionsHTML}`);
4949
});
5050

51+
it("ignores parts that contain only multi-line HTML comments", () => {
52+
const variants = ["Real message 1", "Real message 2"];
53+
const input = `
54+
<!--
55+
This is a multi-line comment
56+
that should be ignored
57+
-->
58+
---
59+
${variants[0]}
60+
---
61+
<!-- Another comment -->
62+
<!-- Another comment -->
63+
64+
<!-- Another comment -->
65+
---
66+
${variants[1]}
67+
68+
${choiceOptionsHTML}
69+
`;
70+
71+
const output = processRandomMessage(input).trim();
72+
const bareOutputWithoutChoiceOptions = output
73+
.replace(choiceOptionsHTML, "")
74+
.trim();
75+
76+
// Vérifie que la sortie ne contient pas de commentaire
77+
expect(bareOutputWithoutChoiceOptions.startsWith("<!--")).toBeFalse();
78+
expect(bareOutputWithoutChoiceOptions.endsWith("-->")).toBeFalse();
79+
});
80+
5181
it("ignores empty or whitespace-only parts between separators or before choiceOptions", () => {
5282
const variants = [`Real message 1`, `Real message 2`];
5383
const input = `

0 commit comments

Comments
 (0)