Skip to content

Commit b5cc3be

Browse files
committed
refactor(chatbot) extractMainContent modularisé
1 parent e159eef commit b5cc3be

4 files changed

Lines changed: 131 additions & 71 deletions

File tree

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,66 @@
11
import { detectChoiceOption } from "./detectChoiceOption.mjs";
2+
import { detectedResponseTitle } from "./detectResponseTitle.mjs";
23
import {
3-
detectedResponseTitle,
4-
isStructureTitle,
5-
} from "./detectResponseTitle.mjs";
6-
7-
const regexDynamicContentIfBlock = /`if (.*?)`/;
4+
handleNewResponseTitle,
5+
handleKeywords,
6+
handleDynamicContent,
7+
handleChoiceOptions,
8+
handleRegularContent,
9+
} from "./helpers/processorsMainContent.mjs";
810

911
export function getMainContentInformations(
1012
mdWithoutYaml,
1113
indexEndIntroduction,
1214
yaml,
1315
) {
14-
let currentResponseTitle = null;
15-
let currentLiItems = [];
16-
let content = [];
17-
let lastOrderedList = null;
18-
let listParsed = false;
19-
const contentAfterFirstPart = mdWithoutYaml.substring(indexEndIntroduction);
20-
const contentAfterFirstPartLines = contentAfterFirstPart.split("\n");
21-
let ifCondition = "";
22-
let chatbotData = [];
23-
for (let line of contentAfterFirstPartLines) {
24-
const choiceStatus = detectChoiceOption(line);
16+
const mainContent = mdWithoutYaml.substring(indexEndIntroduction);
17+
const mainContentLines = mainContent.split("\n");
18+
const chatbotData = [];
19+
20+
const currentData = {
21+
responseTitle: null,
22+
keywords: [],
23+
content: [],
24+
choiceOptions: null,
25+
listParsed: false,
26+
ifCondition: "",
27+
};
28+
29+
for (let line of mainContentLines) {
30+
// Gestion des titres de réponse
2531
if (detectedResponseTitle(line, yaml)) {
26-
// Gestion des identifiants de réponse, et début de traitement du contenu de chaque réponse
27-
if (currentResponseTitle) {
28-
chatbotData.push([
29-
currentResponseTitle,
30-
currentLiItems,
31-
content,
32-
lastOrderedList,
33-
]);
34-
}
35-
currentResponseTitle = line
36-
.replace(detectedResponseTitle(line, yaml), "")
37-
.trim();
38-
currentLiItems = [];
39-
lastOrderedList = null;
40-
listParsed = false;
41-
content = [];
42-
} else if (line.startsWith("- ") && !listParsed) {
43-
// Gestion des listes
44-
currentLiItems.push(line.replace("- ", "").trim());
45-
} else if (yaml.dynamicContent && regexDynamicContentIfBlock.test(line)) {
46-
// Cas des blocs dynamiques conditionnels
47-
ifCondition = line.match(regexDynamicContentIfBlock)[1]
48-
? line.match(regexDynamicContentIfBlock)[1]
49-
: "";
50-
content.push(line + "\n");
51-
listParsed = true;
52-
} else if (yaml.dynamicContent && line.includes("`endif`")) {
53-
ifCondition = "";
54-
content.push(line + "\n");
55-
listParsed = true;
56-
} else if (choiceStatus.isChoice) {
57-
// Cas des listes ordonnées
58-
listParsed = false;
59-
if (!lastOrderedList) {
60-
lastOrderedList = [];
61-
}
62-
const listContent = line.replace(/^\d+(\.|\))\s/, "").trim();
63-
let link = listContent.replace(/^\[.*?\]\(/, "").replace(/\)$/, "");
64-
link = yaml.obfuscate ? btoa(link) : link;
65-
const text = listContent.replace(/\]\(.*/, "").replace(/^\[/, "");
66-
lastOrderedList.push([text, link, choiceStatus.isRandom, ifCondition]);
67-
} else if (line.length > 0 && !isStructureTitle(line, yaml)) {
68-
// Gestion du reste du contenu de la réponse
32+
handleNewResponseTitle(line, yaml, currentData, chatbotData);
33+
continue;
34+
}
35+
36+
// Gestion des mots clés
37+
if (line.startsWith("- ") && !currentData.listParsed) {
38+
handleKeywords(line, currentData);
39+
continue;
40+
}
6941

70-
// Pour définir le contenu d'une réponse, le chatbot ne prend pas en compte les lignes qui contiennent un titre qui sert simplent à structurer le chatbot (pour le créateur, sans affichage dans le chatbot côté utilisateur)
42+
// Gestion des blocs conditionnels si on a du contenu dynamique
43+
if (handleDynamicContent(line, currentData, yaml)) {
44+
continue;
45+
}
7146

72-
// Possibilité de faire des liens à l'intérieur du contenu vers une réponse
73-
line = line.replaceAll(/\[(.*?)\]\((#.*?)\)/g, '<a href="$2">$1</a>');
74-
content.push(line);
75-
listParsed = true;
47+
// Gestion des éventuelles options de choix proposées en fin de réponse
48+
const choiceStatus = detectChoiceOption(line);
49+
if (choiceStatus.isChoice) {
50+
handleChoiceOptions(line, choiceStatus, yaml, currentData);
51+
continue;
7652
}
53+
54+
// Gestion du reste du contenu de la réponse
55+
handleRegularContent(line, yaml, currentData);
7756
}
57+
7858
chatbotData.push([
79-
currentResponseTitle,
80-
currentLiItems,
81-
content,
82-
lastOrderedList,
59+
currentData.responseTitle,
60+
currentData.keywords,
61+
currentData.content,
62+
currentData.choiceOptions,
8363
]);
64+
8465
return chatbotData;
8566
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
detectedResponseTitle,
3+
isStructureTitle,
4+
} from "../detectResponseTitle.mjs";
5+
6+
const regexDynamicContentIfBlock = /`if (.*?)`/;
7+
8+
// Gestion des titres de réponse
9+
export function handleNewResponseTitle(line, yaml, currentData, chatbotData) {
10+
if (currentData.responseTitle) {
11+
chatbotData.push([
12+
currentData.responseTitle,
13+
currentData.keywords,
14+
currentData.content,
15+
currentData.choiceOptions,
16+
]);
17+
}
18+
currentData.responseTitle = line
19+
.replace(detectedResponseTitle(line, yaml), "")
20+
.trim();
21+
currentData.keywords = [];
22+
currentData.choiceOptions = null;
23+
currentData.content = [];
24+
currentData.listParsed = false;
25+
}
26+
27+
// Gestion des mots clés
28+
export function handleKeywords(line, currentData) {
29+
currentData.keywords.push(line.replace("- ", "").trim());
30+
}
31+
32+
// Gestion des blocs conditionnels si on a du contenu dynamique
33+
export function handleDynamicContent(line, currentData, yaml) {
34+
if (yaml.dynamicContent && regexDynamicContentIfBlock.test(line)) {
35+
currentData.ifCondition =
36+
(line.match(regexDynamicContentIfBlock) &&
37+
line.match(regexDynamicContentIfBlock)[1]) ||
38+
"";
39+
40+
currentData.content.push(line + "\n");
41+
currentData.listParsed = true;
42+
return true;
43+
}
44+
if (yaml.dynamicContent && line.includes("`endif`")) {
45+
currentData.ifCondition = "";
46+
currentData.content.push(line + "\n");
47+
currentData.listParsed = true;
48+
return true;
49+
}
50+
return false;
51+
}
52+
53+
// Gestion des éventuelles options de choix proposées en fin de réponse
54+
export function handleChoiceOptions(line, choiceStatus, yaml, currentData) {
55+
currentData.listParsed = false;
56+
if (!currentData.choiceOptions) {
57+
currentData.choiceOptions = [];
58+
}
59+
const listContent = line.replace(/^\d+(\.|\))\s/, "").trim();
60+
let link = listContent.replace(/^\[.*?\]\(/, "").replace(/\)$/, "");
61+
link = yaml.obfuscate ? btoa(link) : link;
62+
const text = listContent.replace(/\]\(.*/, "").replace(/^\[/, "");
63+
currentData.choiceOptions.push([
64+
text,
65+
link,
66+
choiceStatus.isRandom,
67+
currentData.ifCondition,
68+
]);
69+
}
70+
71+
// Gestion du reste du contenu de la réponse
72+
export function handleRegularContent(line, yaml, currentData) {
73+
if (line.length > 0 && !isStructureTitle(line, yaml)) {
74+
// Gestion des liens à l'intérieur du contenu, vers une réponse
75+
line = line.replaceAll(/\[(.*?)\]\((#.*?)\)/g, '<a href="$2">$1</a>');
76+
currentData.content.push(line);
77+
currentData.listParsed = true;
78+
}
79+
}

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.

0 commit comments

Comments
 (0)