UNPKG

jsx-slack

Version:

Build JSON object for Slack Block Kit surfaces from JSX

15 lines (14 loc) 3.86 kB
import{JSXSlack as f}from"../jsx.mjs";import{detectSpecialLink as v,intToAlpha as h,intToRoman as p}from"../utils.mjs";import{escapeURL as u,decodeEntity as b,escapeEntity as y}from"./escape.mjs";import{measureWidth as d,makeIndent as m}from"./measure.mjs";import{parents as C}from"../../vendor/unist-util-parents/lib/index.mjs";import{phrasing as B}from"../../vendor/mdast-util-phrasing/lib/index.mjs";const k=["\u2022","\u25E6","\u25AA\uFE0E"],M=n=>{var c;return!((c=n.data)!=null&&c.codeBlock)&&B(n)};class ${constructor(c){this.codes=[],this.lists=[],this.visitors={root:e=>this.renderCodeBlock(this.block(e)),text:e=>{var t,i;if((t=e.data)!=null&&t.time)return this.visitors.time(e);if((i=e.data)!=null&&i.escape){let s=e;for(;s=s.parent;)if(s.type==="link")return this.escape(e.data.escape);return`<!date^00000000^{_}|${e.value}>`}return this.escape(e.value)},paragraph:e=>this.block(e),blockquote:e=>[...this.block(e).split(` `),""].map(t=>`&gt; ${t}`).join(` `),emphasis:e=>this.markup("_",this.block(e)),strong:e=>this.markup("*",this.block(e)),delete:e=>this.markup("~",this.block(e),{skipCodeBlock:!0}),inlineCode:e=>{var t;return(t=e.data)!=null&&t.codeBlock?this.visitors.code(e):this.markup("`",this.block(e))},code:e=>{const t=this.codes.length;return this.codes.push(this.block(e)),`<<code:${t}>>`},link:e=>{if(!e.url)return this.block(e);switch(v(e.url)){case"#C":case"@UW":return`<${e.url}>`;case"@S":return`<!subteam^${e.url.slice(1)}>`;case"@channel":return"<!channel|channel>";case"@everyone":return"<!everyone|everyone>";case"@here":return"<!here|here>";default:{const t=this.renderCodeBlock(this.block(e).replace(/\n+/g," "),{singleLine:!0}),i=t.match(/^(<!date\^(?!0{8}).+)\|(.+>)$/);if(i){const s=u(e.url).replace(/\^+/g,encodeURI);return`${i[1]}^${s}|${i[2]}`}return e.url===b(t)&&!t.includes("|")?`<${t}>`:`<${u(e.url)}|${t}>`}}},list:e=>{this.lists.unshift([Math.floor(e.start-1)||0,[]]);const t=this.block(e),[,i]=this.lists.shift();let s;if(e.ordered)s=new Map(i.map(r=>[r,`${e.orderedType==="a"?h(r):e.orderedType==="A"?h(r).toUpperCase():e.orderedType==="i"?p(r):e.orderedType==="I"?p(r).toUpperCase():r.toString()}.`]));else{const r=k[Math.min(this.lists.length,k.length-1)];s=new Map(i.map(o=>[o,r]))}const a=Math.max(...[...s.values()].map(d));return t.replace(/<<l(-?\d+)>>/g,(r,o)=>{const l=s.get(Number.parseInt(o,10)),g=d(l);return`${m(a-g)}${l} `}).replace(/<<ls>>/g,`${m(a)} `)},listItem:e=>{var t,i;let s="s";return(t=e.data)!=null&&t.implied||((i=e.data)!=null&&i.value?this.lists[0][0]=e.data.value:this.lists[0][0]+=1,this.lists[0][1].push(this.lists[0][0]),s=this.lists[0][0].toString()),this.block(e).split(` `).map((a,r)=>`<<l${r>0?"s":s}>>${a}`).join(` `)},time:e=>{const t=this.escape(e.data.time.datetime),i=this.escape(e.value.replace(/\n+/g," ")),s=this.escape(e.data.time.fallback);return`<!date^${t}^${i}|${s}>`},break:()=>` `},this.block=e=>{var t,i;const s=[];let a;for(const r of e.children)a&&((t=a.data)!=null&&t.codeBlock&&s.push(` `),M(r)||((i=s[s.length-1])!=null&&i.endsWith(` `)||s.push(` `),!(r.type==="list"&&this.lists.length>0)&&["paragraph","blockquote","list"].includes(a.type)&&s.push(` `))),s.push(this.visit(r,e)),a=r;return s.join("")},this.escape=y,this.markup=(e,t,{skipCodeBlock:i=!1}={})=>{const s=f.exactMode()?`\u200B${e}\u200B`:e;return t.split(` `).map(a=>a.replace(/^((?:&gt; )*)(.*)$/,(r,o,l)=>l&&!(i&&l.startsWith("<<code:"))?`${o}${s}${l}${s}`:`${o}${l}`)).join(` `)},this.renderCodeBlock=(e,{singleLine:t=!1}={})=>e.replace(/<<code:(\d+)>>/g,(i,s)=>{const a=this.codes[Number.parseInt(s,10)];return t?`\`\`\`${a.replace(/\n+/g," ")}\`\`\``:`\`\`\` ${a} \`\`\``}),this.visit=(e,t)=>this.visitors[e.type](e,t),this.root=C(c)}compile(){return this.codes=[],this.lists=[],this.visit(this.root)}}function I(n){return new $(n).compile()}export{$ as MrkdwnCompiler,I as default};