UNPKG

@bothub-chat/ui

Version:

Bothub UI Components

2 lines (1 loc) 1.25 kB
const t=/(```[\s\S]*?```|`[^`\n]*`)/g,n=/(\$\$[\s\S]*?\$\$)/g,e=/[=^_\\{}]/,r=/[=^_{}]|\\[a-zA-Z]+|[+\-*/<>]\s*\d|\d\s*[+\-*/<>]/;function i(t){return" "===t||"\n"===t||"\t"===t||"\r"===t}function $(t){return void 0!==t&&t>="0"&&t<="9"}function s(t,n){for(let e=n;e<t.length;e+=1)if("$"===t[e]&&"\\"!==t[e-1])if("$"!==t[e+1]){if(!i(t[e-1])&&!$(t[e+1]))return e}else e+=1;return-1}function u(t){return!e.test(t)&&!/^\d+[^\d\s.,$]/.test(t)&&/^\d[\d.,]*\s/.test(t)}function o(t){return r.test(t)}function c(t){return function(t){return t.replace(/\\\$\$([\s\S]*?)\\\$\$/g,(t,n)=>`$$\n${n.trim()}\n$$`).replace(new RegExp("(?<!\\\\)\\\\\\[([\\s\\S]*?)(?<!\\\\)\\\\]","g"),(t,n)=>o(n)?`$$\n${n.trim()}\n$$`:t).replace(new RegExp("(?<!\\\\)\\\\\\((.+?)(?<!\\\\)\\\\\\)","g"),(t,n)=>o(n)?`$${n.trim()}$`:t)}(t).split(n).map(t=>function(t){return t.startsWith("$$")&&t.endsWith("$$")}(t)?t:function(t){let n="";for(let e=0;e<t.length;e+=1){const r=t[e];if("$"===r&&"\\"!==t[e-1]&&$(t[e+1])){const r=s(t,e+1),i=-1===r?"":t.slice(e+1,r);if(-1===r||u(i)){n+="\\$";continue}}n+=r}return n}(t)).join("")}function f(n){return n.split(t).map(t=>function(t){return t.startsWith("```")||t.startsWith("`")}(t)?t:c(t)).join("")}export{f as normalizeMessageMarkdown};