From 2c23dbb6cac9d4c2923ef5b61572cd0cd916273f Mon Sep 17 00:00:00 2001 From: rlazarini <remi.lazarini@cea.fr> Date: Wed, 4 Dec 2024 15:40:31 +0100 Subject: [PATCH] [Ivette] markdown : function replaceTagsByElement modified --- ivette/src/dome/renderer/text/markdown.tsx | 49 ++++++++++++---------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/ivette/src/dome/renderer/text/markdown.tsx b/ivette/src/dome/renderer/text/markdown.tsx index 81646a20a1c..ba06ca42828 100644 --- a/ivette/src/dome/renderer/text/markdown.tsx +++ b/ivette/src/dome/renderer/text/markdown.tsx @@ -56,30 +56,33 @@ function replaceTagsByElement( patterns?: Pattern[], ): (string | JSX.Element | null)[] { if(!patterns || patterns.length < 1) return [text]; - - const { pattern, replace } = patterns[0]; - patterns.shift(); - - const newContent = []; + type Content = string | JSX.Element | null; + let newContent: (Content|Content[])[] = [text]; let match; - let lastIndex = 0; - - while ((match = pattern.exec(text)) !== null) { - if (match.index > lastIndex) { - const before = replaceTagsByElement( - text.slice(lastIndex, match.index), counter, patterns - ); - before.forEach((elt) => newContent.push(elt)); - } - newContent.push(replace(counter.increment(), match)); - lastIndex = pattern.lastIndex; - } - if (lastIndex < text.length) { - const after = replaceTagsByElement( - text.slice(lastIndex), counter, patterns); - after.forEach((elt) => newContent.push(elt)); - } - return newContent; + let lastIndex: number; + + patterns.forEach(({ pattern, replace }) => { + newContent = newContent.flat(); + newContent.slice().forEach((content, i) => { + if(typeof content === "string") { + const contentTab: (string | JSX.Element | null)[] = []; + lastIndex = 0; + while ((match = pattern.exec(content)) !== null) { + if (match.index > lastIndex) { + contentTab.push(content.slice(lastIndex, match.index)); + } + contentTab.push(replace(counter.increment(), match)); + lastIndex = pattern.lastIndex; + } + if (lastIndex < content.length) { + contentTab.push(content.slice(lastIndex)); + } + newContent.splice(i, 1, contentTab); + } + }); + }); + + return newContent.flat(); } function replaceTags( -- GitLab