Skip to content
Snippets Groups Projects
Commit 2c23dbb6 authored by Remi Lazarini's avatar Remi Lazarini
Browse files

[Ivette] markdown : function replaceTagsByElement modified

parent 702ff914
No related branches found
No related tags found
No related merge requests found
...@@ -56,30 +56,33 @@ function replaceTagsByElement( ...@@ -56,30 +56,33 @@ function replaceTagsByElement(
patterns?: Pattern[], patterns?: Pattern[],
): (string | JSX.Element | null)[] { ): (string | JSX.Element | null)[] {
if(!patterns || patterns.length < 1) return [text]; if(!patterns || patterns.length < 1) return [text];
type Content = string | JSX.Element | null;
const { pattern, replace } = patterns[0]; let newContent: (Content|Content[])[] = [text];
patterns.shift();
const newContent = [];
let match; let match;
let lastIndex = 0; let lastIndex: number;
while ((match = pattern.exec(text)) !== null) { patterns.forEach(({ pattern, replace }) => {
if (match.index > lastIndex) { newContent = newContent.flat();
const before = replaceTagsByElement( newContent.slice().forEach((content, i) => {
text.slice(lastIndex, match.index), counter, patterns if(typeof content === "string") {
); const contentTab: (string | JSX.Element | null)[] = [];
before.forEach((elt) => newContent.push(elt)); lastIndex = 0;
} while ((match = pattern.exec(content)) !== null) {
newContent.push(replace(counter.increment(), match)); if (match.index > lastIndex) {
lastIndex = pattern.lastIndex; contentTab.push(content.slice(lastIndex, match.index));
} }
if (lastIndex < text.length) { contentTab.push(replace(counter.increment(), match));
const after = replaceTagsByElement( lastIndex = pattern.lastIndex;
text.slice(lastIndex), counter, patterns); }
after.forEach((elt) => newContent.push(elt)); if (lastIndex < content.length) {
} contentTab.push(content.slice(lastIndex));
return newContent; }
newContent.splice(i, 1, contentTab);
}
});
});
return newContent.flat();
} }
function replaceTags( function replaceTags(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment