UNPKG

@bothub-chat/ui

Version:

Bothub UI Components

2 lines (1 loc) 1.41 kB
const t=({rawContext:t,translation:n})=>{if(!t)return"";const r=t.match(/owner="([^"]+)"/),e=t.match(/repo="([^"]+)"/),o=t.match(/branch="([^"]+)"/),a=t.match(/<file-tree>([\s\S]*?)<\/file-tree>/),s=t.match(/<loaded-files>([\s\S]*?)<\/loaded-files>/),c=r?r[1]:"",i=e?e[1]:"",l=o?o[1]:"",h=a?a[1].trim():"";let $="";if(c&&i&&($+=`### 📦 ${n.repo}: **${c}/${i}**\n`,$+=`**${n.branch}:** \`${l}\`\n\n`),h){const t=(t=>{const n={};t.forEach(t=>{if(!t.trim())return;const r=t.split("/");let e=n;r.forEach(t=>{e[t]||(e[t]={}),e=e[t]})});const r=(t,n="")=>{const e=Object.keys(t);return e.map((o,a)=>{const s=a===e.length-1,c=s?"└── ":"├── ",i=s?" ":"│ ",l=r(t[o],n+i);return`${n}${c}${o}${l?`\n${l}`:""}`}).join("\n")};return r(n)})(h.split("\n").map(t=>t.trim()).filter(Boolean));$+=`#### 📂 ${n.structure}:\n`,$+=`\`\`\`text\n${t}\n\`\`\`\n\n`}if(s){const t=s[1],r=Array.from(t.matchAll(/<file\s+([^>]+)>/g));if(r.length>0){$+=`#### 📥 ${n.loaded}:\n`;$+=`${r.map(t=>{const n=t[1],r=n.match(/path="([^"]+)"/),e=n.match(/status="([^"]+)"/),o=n.match(/size="([^"]+)"/);return`- \`${r?r[1]:"unknown"}\` *(Status: ${e?e[1]:"-"}, Size: ${(t=>{if(0===t)return"0 B";const n=Math.floor(Math.log(t)/Math.log(1024));return`${parseFloat((t/Math.pow(1024,n)).toFixed(2))} ${["B","KB","MB","GB"][n]}`})(o?parseInt(o[1],10):0)})*`}).join("\n")}\n`}}return $};export{t as formatGithubContextToMarkdown};