@mdit/plugin-tab
Version:
tab plugin for MarkdownIt
13 lines (12 loc) • 4.45 kB
JavaScript
import{escapeHtml as y}from"@mdit/helper";import{isSpace as B}from"markdown-it/lib/common/utils.mjs";const j=3,x="@tab",T=x+":active",q=x.length,I=T.length,R=(r,o,t)=>{if(r.src.charCodeAt(o)!==64)return!1;let a=1;for(;a<I&&T.charCodeAt(a)===r.src.charCodeAt(o+a);a++);const b=a===I;if(!b&&a!==q)return!1;const h=o+a,c=r.skipSpaces(h);return c>h&&c<t?{isActive:b,pos:c}:!1},w=(r,o)=>(t,a,b,h)=>{if(o.state!==r)return!1;const c=t.bMarks[a]+t.tShift[a],p=t.eMarks[a],n=t.sCount[a],e=R(t,c,p);if(e===!1)return!1;if(h)return!0;let s=a+1,l=!1;for(;s<b;s++){const g=t.bMarks[s]+t.tShift[s];if(t.sCount[s]===n&&t.src[g]==="@"&&R(t,g,t.eMarks[s])){l=!0;break}}const v=t.parentType,i=t.lineMax,d=t.blkIndent;t.parentType="tab",t.lineMax=s-(l?1:0),t.blkIndent=n;const f=t.push(`${r}_tab_open`,"",1),u=e.pos,C=t.skipSpacesBack(p,u);let k=C,m;for(;k>u;){if(t.src.charCodeAt(k)===35){for(m=k-1;t.src.charCodeAt(m)===92;)m--;if((k-m)%2===1)break}k--}let $,S="";k!==u?(S=t.src.slice(t.skipSpaces(k+1),C),$=t.src.slice(u,t.skipSpacesBack(k,u))):$=t.src.slice(u,C),f.block=!0,f.markup=x,f.info=$,f.meta={active:e.isActive},S&&(f.meta.id=S),f.map=[a,s-(l?1:0)],t.md.block.tokenize(t,a+1,s+(l?0:1));const A=t.push(`${r}_tab_close`,"",-1);return A.block=!0,A.markup="",t.parentType=v,t.lineMax=i,t.blkIndent=d,t.line=s+(l?0:1),!0},z=(r,o)=>(t,a,b,h)=>{const c=t.bMarks[a]+t.tShift[a],p=t.eMarks[a],n=t.sCount[a];if(t.src.charCodeAt(c)!==58)return!1;let e=c+1;for(;e<=p&&t.src.charCodeAt(e)===58;)e++;const s=e-c;if(s<j)return!1;e=t.skipSpaces(e);for(let _=0;_<r.length;_++){if(t.src.charCodeAt(e)!==r.charCodeAt(_))return!1;e++}let l=!1,v;for(;e!==p;){if(v=t.src.charCodeAt(e++),v===35){l=!0;break}if(!B(v))return!1}if(h)return!0;let i=a+1,d=!1,f=e;for(;i<b;i++){const _=t.bMarks[i]+t.tShift[i],M=t.eMarks[i];if(_<M&&t.sCount[i]<n)break;if(t.sCount[i]===n&&t.src.charCodeAt(_)===58){for(e=_+1;e<=M&&t.src.charCodeAt(e)===58;e++);if(e-_>=s&&(e=t.skipSpaces(e),e>=M)){d=!0;break}}}const u=t.parentType,C=t.lineMax,k=t.blkIndent,m=o.state;t.parentType=`${r}_tabs`,t.lineMax=i-(d?1:0),t.blkIndent=n;const $=":".repeat(s);let S="";if(l){f=t.skipSpaces(f);const _=t.skipSpacesBack(p,f);f<_&&(S=t.src.slice(f,_))}const A=t.push(`${r}_tabs_open`,"",1);A.markup=$,A.block=!0,A.info=r,A.meta={id:S},A.map=[a,i-(d?1:0)],o.state=r,t.md.block.tokenize(t,a+1,i-(d?1:0)),o.state=m;const g=t.push(`${r}_tabs_close`,"",-1);return g.markup=$,g.block=!0,t.parentType=u,t.lineMax=C,t.blkIndent=k,t.line=i+(d?1:0),!0},O=r=>(o,t)=>{const a=[];let b=-1,h=!1,c=0;for(let p=t+1;p<o.length;p++){const{block:n,meta:e,type:s,info:l}=o[p];if(n){if(s===`${r}_tabs_open`){c++;continue}if(s===`${r}_tabs_close`){if(c===0)break;c--;continue}if(c>0)continue;if(s===`${r}_tab_open`){h=!0,e.index=a.length,e.active&&(b===-1?b=a.length:e.active=!1),a.push({title:l,index:a.length,id:e.id,isActive:e.active});continue}if(s===`${r}_tab_close`)continue;h||(o[p].type=`${r}_tabs_empty`,o[p].hidden=!0)}}return{active:b,data:a}},D=(r,o)=>{const{info:t,meta:a}=r[o];return{title:t,index:a.index,id:a.id,isActive:a.active}},J={state:null},E=(r,o)=>{const{name:t="tabs",openRender:a=(n,e,s,l,v,i)=>{const{active:d,data:f}=n,u=e[s];u.attrJoin("class",`${t}-tabs-wrapper`),u.meta.id&&u.attrJoin("data-id",u.meta.id);const C=f.map(({title:k,id:m},$)=>`<button type="button" class="${t}-tab-button${d===$?" active":""}" data-tab="${$}"${m?` data-id="${y(m)}"`:""}${d===$?" data-active":""}>${y(r.renderInline(k))}</button>`);return`<div${i.renderAttrs(u)}>
<div class="${t}-tabs-header">
${C.join(`
`)}
</div>
<div class="${t}-tabs-container">
`},closeRender:b=()=>` </div>
</div>
`,tabOpenRender:h=(n,e,s,l,v,i)=>{const d=e[s];return d.attrJoin("class",`${t}-tab-content${n.isActive?" active":""}`),d.attrSet("data-index",n.index.toString()),n.id&&d.attrSet("data-id",n.id.toString()),n.isActive&&d.attrJoin("data-active",""),`<div${i.renderAttrs(e[s])}>
`},tabCloseRender:c=()=>`</div>
`}=o??{},p=O(t);r.block.ruler.before("fence",`${t}_tabs`,z(t,J),{alt:["paragraph","reference","blockquote","list"]}),r.block.ruler.before("paragraph",`${t}_tab`,w(t,J),{alt:["paragraph","reference","blockquote","list"]}),r.renderer.rules[`${t}_tabs_open`]=(n,e,s,l,v)=>{const i=p(n,e);return a(i,n,e,s,l,v)},r.renderer.rules[`${t}_tabs_close`]=b,r.renderer.rules[`${t}_tab_open`]=(n,e,...s)=>{const l=D(n,e);return h(l,n,e,...s)},r.renderer.rules[`${t}_tab_close`]=c};export{E as tab};
//# sourceMappingURL=index.js.map