openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 4.41 kB
JavaScript
function stripCompletedLongOrTildeFences(e){if(!e.includes("````")&&!e.includes(`~~~`))return e;let t,n=0,r=``;for(let i of e.matchAll(/(?:^|\n) {0,3}(`{3,}(?!`)|~{3,}(?!~))([^\r\n\u2028\u2029]*)\r?(?=\n|$)/g)){let[,a,o]=i;if(a!==void 0&&o!==void 0){if(t)a[0]===t.marker[0]&&a.length>=t.marker.length&&/^[ \t]*$/.test(o)&&((t.marker[0]===`~`||t.marker.length>3)&&(r+=e.slice(n,t.start)+` `,n=i.index+i[0].length),t=void 0);else{if(a[0]==="`"&&o.includes("`"))continue;t={start:i.index,marker:a}}}}return r+e.slice(n)}function splitTableRow(e){if(!e.includes(`|`)||/^(?: {4}| {0,3}\t)/.test(e)||/^ {0,3}(?:>|#{1,6}(?:\s|$)|(?:[-+*]|\d+[.)])\s)/.test(e))return;let t=[],n=0;for(let r=0;r<e.length;r+=1)e[r]===`\\`?r+=1:e[r]===`|`&&(t.push(e.slice(n,r).trim()),n=r+1);if(t.length!==0)return t.push(e.slice(n).trim()),t[0]===``&&t.shift(),t.at(-1)===``&&t.pop(),t.length>0?t:void 0}function stripTables(e){if(!e.includes(`|`))return e;let t=e.split(`
`),n=t.map(splitTableRow);for(let e=0;e<t.length-1;e+=1){let r=n[e],i=n[e+1];if(r&&i&&r.length===i.length&&i.every(e=>/^:?-+:?$/.test(e))){t[e]=r.join(` `).replace(/\\\|/g,`|`),t[++e]=``;for(let r=n[e+1];r;r=n[e+1])t[++e]=r.join(` `).replace(/\\\|/g,`|`)}}return t.join(`
`)}function flattenMarkdownToPlainText(e){let t=[],n=stripTables(stripInlineLinks(stripCompletedLongOrTildeFences(e).replace(/```[\s\S]*?```/g,` `).replace(/```/g,` `),{transformText:e=>e.replace(/\0|(?<!`)(`+)(?!`)([\s\S]*?[^`])\1(?!`)/g,(e,n,r)=>r===void 0?`\0\0`:`\0${t.push(r)-1}\0`)})).replace(/^[\t ]{0,3}(?:(?:>[\t ]*)+|(?:#{1,6}|[-+*]|\d+[.)])\s+)/gm,``);for(let e of[`*`,`_`,`~`])n=stripEmphasis(n,e);return n.replace(/\0(\d*)\0/g,(e,n)=>n?t[Number(n)]??``:`\0`).replace(/\s+/g,` `).trim()}function stripEmphasis(e,t){let n=t===`~`?[2]:[2,1],r={1:0,2:0},findCloser=(n,i)=>{let a=t.repeat(n),o=e.indexOf(a,Math.max(i,r[n]));for(;o!==-1;){if(/\S/.test(e.charAt(o-1))&&(t!==`_`||!/^[\p{L}\p{N}]/u.test(e.slice(o+n,o+n+2))))return r[n]=o,o;o=e.indexOf(a,o+1)}return r[n]=e.length,-1},i=``,a=0;for(let r=e.indexOf(t);r!==-1;r=e.indexOf(t,r+1)){let o=(e.codePointAt(r-2)??0)>65535?2:1;if(t!==`_`||r===0||r!==a&&!/[\p{L}\p{N}]/u.test(e.slice(r-o,r)))for(let o of n){let n=r+o;if(!e.startsWith(t.repeat(o),r)||!/\S/.test(e.charAt(n)))continue;let s=findCloser(o,n+1);if(s!==-1){i+=e.slice(a,r)+e.slice(n,s),a=s+o,r=a-1;break}}}return i+e.slice(a)}const e=/[\t\n\v\f\r ]/;function isAsciiSpaceOrControl(e){return e<=` `||e===``}function matchParens(e){let t=/* @__PURE__ */ new Map,n=[];for(let r=0;r<e.length;r+=1){let i=e.charAt(r);if(i===`\\`)r+=1;else if(isAsciiSpaceOrControl(i))n.length=0;else if(i===`(`)n.push(r);else if(i===`)`){let e=n.pop();e!==void 0&&t.set(e,r)}}return t}const t={'"':`"`,"'":`'`,"(":`)`};function skipSpaces(t,n){let r=n;for(;r<t.length&&e.test(t.charAt(r));)r+=1;return r}function findDestinationEnd(e,n,r){let i=skipSpaces(e,n+1);if(e.charAt(i)===`<`){for(i+=1;i<e.length&&!`<>
`.includes(e.charAt(i));)i+=e.charAt(i)===`\\`?2:1;if(e.charAt(i)!==`>`)return;i+=1}else for(;i<e.length&&e.charAt(i)!==`)`&&!isAsciiSpaceOrControl(e.charAt(i));)if(e.charAt(i)===`(`){let e=r.get(i);if(e===void 0)return;i=e+1}else i+=e.charAt(i)===`\\`?2:1;let a=skipSpaces(e,i),o=t[e.charAt(a)];if(a>i&&o){for(i=a+1;i<e.length&&e.charAt(i)!==o;){if(o===`)`&&e.charAt(i)===`(`)return;i+=e.charAt(i)===`\\`?2:1}i=skipSpaces(e,i+1)}else i=a;return e.charAt(i)===`)`?i:void 0}function matchLinks(e){let t=/* @__PURE__ */ new Map,n=/* @__PURE__ */ new Map,r=matchParens(e),i=[];for(let a=0;a<e.length;a+=1){let o=e.charAt(a);if(o===`\\`)a+=1;else if(o==="`")a=Math.max(a,e.indexOf("`",a+1));else if(o===`[`)i.push(a);else if(o===`]`){let o=i.pop();if(o===void 0)continue;t.set(o,a);let s=e.charAt(a+1)===`(`?findDestinationEnd(e,a+1,r):void 0;s!==void 0&&(n.set(a,s),a=s)}}return{labelEnds:t,destinationEnds:n}}function stripInlineLinks(e,t={}){let n=t.transformText??(e=>e),{labelEnds:r,destinationEnds:i}=matchLinks(e),a=/* @__PURE__ */ new Map,o=``,s=0;for(let c=0;c<e.length;c+=1){let l=a.get(c);if(l!==void 0){o+=n(e.slice(s,c)),s=l,c=l-1;continue}let u=e[c]===`[`?r.get(c):void 0,d=u===void 0?void 0:i.get(u);if(u===void 0||d===void 0)continue;let f=c>s&&e[c-1]===`!`;if(f)for(let t=c-2;t>=0&&e[t]===`\\`;--t)f=!f;(f||u!==c+1)&&(o+=n(e.slice(s,f?c-1:c)),f&&t.images===`omit`?(s=d+1,c=d):(s=c+1,a.set(u,d+1)))}return o+n(e.slice(s))}export{flattenMarkdownToPlainText as t};