UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

7 lines • 7.65 kB
import{sliceVisible,stripAnsi,visibleLength}from"#cli/ui/terminal-text.js";import{lexer}from"#compiled/marked/index.js";import{graphemes}from"#shared/text-boundaries.js";const ansi={bold:`\x1B[1m`,boldOff:`\x1B[22m`,inlineCode:`\x1B[38;5;245m`,inlineCodeOff:`\x1B[39m`,dim:`\x1B[2m`,dimOff:`\x1B[22m`,italic:`\x1B[3m`,italicOff:`\x1B[23m`,strike:`\x1B[9m`,strikeOff:`\x1B[29m`,underline:`\x1B[4m`,underlineOff:`\x1B[24m`};function detectMarkdownRendering(e){let t=e?.EVE_TUI_RENDER_MARKDOWN??process.env.EVE_TUI_RENDER_MARKDOWN;return t!==`0`&&t!==`false`}function renderMarkdown(e,t=1/0){return trimTrailingBlankRows(renderBlocks(lexer(e),t,!0)).join(` `)}function renderBlocks(e,t,n=!1){let r=[];for(let i of e){switch(i.type){case`space`:pushBlankRow(r);break;case`heading`:r.push(renderHeading(i.depth??1,i.tokens));break;case`paragraph`:case`text`:r.push(...renderInline(i.tokens,i.text).split(` `));break;case`code`:r.push(...renderCodeBlock(i,t));break;case`blockquote`:r.push(...renderBlocks(i.tokens??[],Math.max(1,t-2)).map(e=>`${ansi.dim}│${ansi.dimOff} ${e}`));break;case`list`:r.push(...renderList(i,t));break;case`table`:r.push(...renderTable(i,t));break;case`hr`:r.push(ansi.dim+`─`.repeat(horizontalRuleWidth(t))+ansi.dimOff);break;case`html`:r.push(...(i.text??i.raw??``).split(` `));break;case`def`:break;default:i.tokens===void 0?i.text!==void 0&&r.push(i.text):r.push(...renderBlocks(i.tokens,t))}n&&isVisibleBlock(i)&&pushBlankRow(r)}return r}function isVisibleBlock(e){return e.type!==`space`&&e.type!==`def`}function renderHeading(e,t){let n=e===1||e===3||e===5,r=e===1||e===2||e===4,i=renderInline(t,``,{restoreAfterStrong:(e===4||e===5||e>=6)&&!r?ansi.dim:``,restoreUnderlineAfterLink:n,suppressBold:r});switch(e){case 1:return`${ansi.bold}${ansi.underline}${i}${ansi.underlineOff}${ansi.boldOff}`;case 2:return`${ansi.bold}${i}${ansi.boldOff}`;case 3:return`${ansi.underline}${i}${ansi.underlineOff}`;case 4:return`${ansi.bold}${ansi.dim}${i}${ansi.boldOff}`;case 5:return`${ansi.dim}${ansi.underline}${i}${ansi.underlineOff}${ansi.dimOff}`;default:return`${ansi.dim}${i}${ansi.dimOff}`}}function renderCodeBlock(e,t){let n=e.lang?.trim()??``,r=(e.text??``).replace(/\n$/u,``).split(` `),i=Math.max(1,...r.map(visibleLength));if(!Number.isFinite(t)||t>5){let e=Math.min(Number.isFinite(t)?Math.floor(t):i,Math.max(6,i,visibleLength(n)+4));return[n.length>0?codePanelHeader(n,e):codePanelRule(e),...r.flatMap(t=>wrapCodeRow(t,e)),codePanelRule(e)]}return r.flatMap(e=>wrapCodeRow(e,Math.max(1,Math.floor(t))))}function codePanelHeader(e,t){let n=sliceVisible(e,Math.max(1,t-4))||`?`;return`${ansi.dim}─ ${n} ${`─`.repeat(Math.max(0,t-3-visibleLength(n)))}${ansi.dimOff}`}function codePanelRule(e){return`${ansi.dim}${`─`.repeat(e)}${ansi.dimOff}`}function wrapCodeRow(e,t){if(e.length===0)return[``];let n=e.match(/^[\t ]*/u)?.[0]??``,r=[],i=e,a=!1;for(;i.length>0;){let e=a&&visibleLength(n)<t?n:``,o=Math.max(1,t-visibleLength(e)),s=sliceVisible(i,o),c=s.length===0?firstGraphemeFallback(i):void 0,l=c?.rendered??s;r.push(`${e}${l}`),i=i.slice(c?.consumed??s.length),a=!0}return r}function renderList(e,t){let n=[],r=typeof e.start==`number`?e.start:1;for(let i of e.items??[]){let a=e.ordered?`${ansi.dim}${r}.${ansi.dimOff}`:`${ansi.dim}•${ansi.dimOff}`,o=e.ordered?`${r}.`.length:1,s=i.checked===!0?`✓ `:i.checked===!1?`☐ `:``,c=renderBlocks(i.tokens??[],Math.max(1,t-o-1));(c.length===0?[``]:c).forEach((e,t)=>{n.push(t===0?`${a} ${s}${e}`:`${` `.repeat(o+1)}${e}`)}),r+=1}return n}function renderInline(e,t=``,n={}){if(e===void 0)return t;let r=``;for(let t of e)t.type===`image`&&r.endsWith(` `)&&(r=`${r.slice(0,-1)}\u00a0`),r+=renderInlineToken(t,n);return r}function renderInlineToken(e,t){switch(e.type){case`text`:return renderInline(e.tokens,e.text??``,t);case`strong`:{let n=renderInline(e.tokens,e.text,t);return t.suppressBold?n:`${ansi.bold}${n}${ansi.boldOff}${t.restoreAfterStrong??``}`}case`em`:return`${ansi.italic}${renderInline(e.tokens,e.text,t)}${ansi.italicOff}`;case`del`:return`${ansi.strike}${renderInline(e.tokens,e.text,t)}${ansi.strikeOff}`;case`codespan`:return`${ansi.inlineCode}${e.text??``}${ansi.inlineCodeOff}`;case`link`:return renderLink(renderInline(e.tokens,e.text,t),e.href??``,t.restoreUnderlineAfterLink);case`image`:return renderLink(`▧\u00a0${(e.text?.trim()||`image`).replaceAll(` `,`\xA0`)}`,e.href??``,t.restoreUnderlineAfterLink);case`br`:return` `;case`checkbox`:return e.checked?`✓ `:`☐ `;case`escape`:return e.text??``;case`html`:return e.text??e.raw??``;default:return renderInline(e.tokens,e.text??e.raw??``,t)}}function renderLink(e,t,n=!1){if(!isSafeLinkUrl(t))return e;let r=n?ansi.underline:``;return`\x1b]8;;${t}\x1b\\${ansi.underline}${e}${ansi.underlineOff}\x1b]8;;\x1b\\${r}`}function isSafeLinkUrl(e){return e.length>0&&Buffer.byteLength(e,`utf8`)<=2083&&[...e].every(e=>{let t=e.codePointAt(0)??0;return t>=32&&t!==127&&(t<128||t>159)})}function renderTable(e,t){let n=e.header??[];if(n.length===0)return[];let r=n.map((t,n)=>normalizeAlignment(e.align?.[n])),i=n.map(renderTableCell),a=(e.rows??[]).map(e=>normalizeTableRow(e,n.length).map(renderTableCell)),o=[i,...a],s=r.map((e,t)=>Math.max(1,...o.map(e=>visibleLength(e[t]??``)))),c=s.reduce((e,t)=>e+t,0)+s.length*3+1;if(s.length>0&&(!Number.isFinite(t)||c<=t)){let e=i.map(e=>`${ansi.bold}${e}${ansi.boldOff}`);return[formatTableBorder(s,`┌`,`┬`,`┐`),formatBoxedTableRow(e,s,r),formatTableBorder(s,`├`,`┼`,`┤`),...a.flatMap((e,t)=>[formatBoxedTableRow(e,s,r),...t+1<a.length?[formatTableBorder(s,`├`,`┼`,`┤`)]:[]]),formatTableBorder(s,`└`,`┴`,`┘`)]}return renderVerticalTable(i,a,t)}function renderTableCell(e){return renderInline(e.tokens,e.text).replaceAll(` `,` `)}function normalizeTableRow(e,t){return Array.from({length:t},(t,n)=>e[n]??{text:``,tokens:[]})}function normalizeAlignment(e){return e??`left`}function renderVerticalTable(e,t,n){let r=Number.isFinite(n)?Math.max(1,Math.floor(n)):80;if(r<=2)return renderNarrowTable(e,t,r);let i=r-2,a=`${ansi.dim}┌${`─`.repeat(i)}┐${ansi.dimOff}`,o=`${ansi.dim}├${`─`.repeat(i)}┤${ansi.dimOff}`,s=`${ansi.dim}└${`─`.repeat(i)}┘${ansi.dimOff}`,c=[a];return t.forEach((n,r)=>{e.forEach((e,t)=>{c.push(...boxedTableLines(`${e}: ${n[t]??``}`,i))}),r+1<t.length&&c.push(o)}),c.push(s),c}function renderNarrowTable(e,t,n){return t.flatMap(t=>e.map((e,r)=>{let i=stripAnsi(`${e}: ${t[r]??``}`),a=sliceVisible(i,n);return a.length>0?a:firstGraphemeFallback(i).rendered}))}function boxedTableLines(e,t){let n=[],r=e;do{let e=sliceVisible(r,t),i=e.length===0?firstGraphemeFallback(r):void 0,a=i?.rendered??e,o=` `.repeat(Math.max(0,t-visibleLength(a)));n.push(`${ansi.dim}│${ansi.dimOff}${a}${o}${ansi.dim}│${ansi.dimOff}`),r=r.slice(i?.consumed??e.length)}while(r.length>0);return n}function firstGraphemeFallback(e){return{consumed:graphemes(e)[0]?.text.length??1,rendered:`?`}}function formatTableBorder(e,t,n,r){return`${t}${e.map(e=>`─`.repeat(e+2)).join(n)}${r}`}function formatBoxedTableRow(e,t,n){return`│ ${e.map((e,r)=>alignTableCell(e,t[r]??1,n[r]??`left`)).join(` │ `)} │`}function alignTableCell(e,t,n){let r=Math.max(0,t-visibleLength(e));if(n===`right`)return`${` `.repeat(r)}${e}`;if(n===`center`){let t=Math.floor(r/2);return`${` `.repeat(t)}${e}${` `.repeat(r-t)}`}return`${e}${` `.repeat(r)}`}function horizontalRuleWidth(e){return Number.isFinite(e)?Math.max(1,Math.floor(e)):60}function pushBlankRow(e){e.length>0&&e.at(-1)!==``&&e.push(``)}function trimTrailingBlankRows(e){for(;e.at(-1)===``;)e.pop();return e}export{detectMarkdownRendering,renderMarkdown};