|
1 | 1 | import { detectChoiceOption } from "./detectChoiceOption.mjs"; |
| 2 | +import { detectedResponseTitle } from "./detectResponseTitle.mjs"; |
2 | 3 | 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"; |
8 | 10 |
|
9 | 11 | export function getMainContentInformations( |
10 | 12 | mdWithoutYaml, |
11 | 13 | indexEndIntroduction, |
12 | 14 | yaml, |
13 | 15 | ) { |
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 |
25 | 31 | 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 | + } |
69 | 41 |
|
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 | + } |
71 | 46 |
|
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; |
76 | 52 | } |
| 53 | + |
| 54 | + // Gestion du reste du contenu de la réponse |
| 55 | + handleRegularContent(line, yaml, currentData); |
77 | 56 | } |
| 57 | + |
78 | 58 | chatbotData.push([ |
79 | | - currentResponseTitle, |
80 | | - currentLiItems, |
81 | | - content, |
82 | | - lastOrderedList, |
| 59 | + currentData.responseTitle, |
| 60 | + currentData.keywords, |
| 61 | + currentData.content, |
| 62 | + currentData.choiceOptions, |
83 | 63 | ]); |
| 64 | + |
84 | 65 | return chatbotData; |
85 | 66 | } |
0 commit comments