UNPKG

@mdit/plugin-attrs

Version:
4 lines (3 loc) 7.28 kB
import{escapeRegExp as b}from"@mdit/helper";const R=/[^\t\n\f />"'=]/,E=" ",w="=",k=".",v="#",p=(e,t,{left:i,right:n,allowed:r})=>{let s="",l="",o=!0,a=!1;const f=[];for(let h=t+i.length;h<e.length;h++){if(e.slice(h,h+n.length)===n){s!==""&&f.push([s,l]);break}const c=e.charAt(h);if(c===w&&o){o=!1;continue}if(c===k&&s===""){e.charAt(h+1)===k?(s="css-module",h++):s="class",o=!1;continue}if(c===v&&s===""){s="id",o=!1;continue}if(c==='"'&&l===""){a=!0;continue}if(c==='"'&&a){a=!1;continue}if(c===E&&!a){if(s==="")continue;f.push([s,l]),s="",l="",o=!0;continue}if(!(o&&c.search(R)===-1)){if(o){s+=c;continue}l+=c}}return r.length?f.filter(([h])=>r.some(c=>c instanceof RegExp?c.test(h):c===h)):f},u=(e,t)=>{t&&e.forEach(i=>{const[n,r]=i;n==="class"?t.attrJoin("class",r):n==="css-module"?t.attrJoin("css-module",r):t.attrPush(i)})},d=(e,t,i)=>n=>{const r=t.length,s=i.length,l=r+1+s;if(!n||typeof n!="string"||n.length<l)return!1;const o=g=>{const x=g.charAt(r)===".",O=g.charAt(r)==="#";return x||O?g.length>=l+1:g.length>=l};let a,f,h,c;const y=l-s;switch(e){case"start":h=n.slice(0,r),a=h===t?0:-1,f=a===-1?-1:n.indexOf(i,y),c=n.charAt(f+s),c&&i.includes(c)&&(f=-1);break;case"end":a=n.lastIndexOf(t),f=a===-1?-1:n.indexOf(i,a+y),f=f===n.length-s?f:-1;break;case"only":h=n.slice(0,r),a=h===t?0:-1,h=n.slice(n.length-s),f=h===i?n.length-s:-1;break}return a!==-1&&f!==-1&&o(n.substring(a,f+s))},$=(e,t,i)=>{const n=b(t),r=b(i),s=e.search(new RegExp(`[ \\n]?${n}[^${n}${r}]+${r}$`));return s!==-1?e.slice(0,s):e},m=(e,t)=>{if(e[t].type==="softbreak")return null;if(e[t].nesting===0)return e[t];const i=e[t].level,n=e[t].type.replace("_close","_open");for(;t>=0;--t)if(e[t].type===n&&e[t].level===i)return e[t];return null},I=e=>({name:"code-block",tests:[{shift:0,block:!0,info:d("end",e.left,e.right)}],transform:(t,i)=>{const n=t[i];let r="";const s=/{(?:[\d,-]+)}/.exec(n.info);s&&(n.info=n.info.replace(s[0],""),r=s[0]);const l=n.info.lastIndexOf(e.left),o=p(n.info,l,e);u(o,n),n.info=`${$(n.info,e.left,e.right)} ${r}`}}),S=e=>[{name:"inline nesting 0",tests:[{shift:0,type:"inline",children:[{shift:-1,type:t=>t==="image"||t==="code_inline"},{shift:0,type:"text",content:d("start",e.left,e.right)}]}],transform:(t,i,n)=>{const r=t[i].children[n],s=r.content.indexOf(e.right),l=t[i].children[n-1],o=p(r.content,0,e);u(o,l),r.content.length===s+e.right.length?t[i].children.splice(n,1):r.content=r.content.slice(s+e.right.length)}},{name:"inline attributes",tests:[{shift:0,type:"inline",children:[{shift:-1,nesting:-1},{shift:0,type:"text",content:d("start",e.left,e.right)}]}],transform:(t,i,n)=>{const r=t[i].children[n],{content:s}=r,l=p(s,0,e),o=m(t[i].children,n-1);u(l,o),r.content=s.slice(s.indexOf(e.right)+e.right.length)}}],j=e=>({name:"table",tests:[{shift:0,type:"table_close"},{shift:1,type:"paragraph_open"},{shift:2,type:"inline",content:d("only",e.left,e.right)}],transform:(t,i)=>{const n=t[i+2],r=m(t,i),s=p(n.content,0,e);u(s,r),t.splice(i+1,3)}}),D=e=>[{name:"list softbreak",tests:[{shift:-2,type:"list_item_open"},{shift:0,type:"inline",children:[{position:-2,type:"softbreak"},{position:-1,type:"text",content:d("only",e.left,e.right)}]}],transform:(t,i,n)=>{const r=t[i].children[n],s=p(r.content,0,e);let l=i-2;for(;t[l-1]&&t[l-1].type!=="ordered_list_open"&&t[l-1].type!=="bullet_list_open";)l--;u(s,t[l-1]),t[i].children=t[i].children.slice(0,-2)}},{name:"list double softbreak",tests:[{shift:0,type:t=>t==="bullet_list_close"||t==="ordered_list_close"},{shift:1,type:"paragraph_open"},{shift:2,type:"inline",content:d("only",e.left,e.right),children:t=>t.length===1},{shift:3,type:"paragraph_close"}],transform:(t,i)=>{const n=t[i+2],r=p(n.content,0,e),s=m(t,i);u(r,s),t.splice(i+1,3)}},{name:"list item end",tests:[{shift:-2,type:"list_item_open"},{shift:0,type:"inline",children:[{position:-1,type:"text",content:d("end",e.left,e.right)}]}],transform:(t,i,n)=>{const r=t[i].children[n],{content:s}=r,l=p(s,s.lastIndexOf(e.left),e);u(l,t[i-2]);const o=s.slice(0,s.lastIndexOf(e.left));r.content=o[o.length-1]===" "?o.slice(0,-1):o}}],K=e=>({name:` {.a} softbreak then curly in start`,tests:[{shift:0,type:"inline",children:[{position:-2,type:"softbreak"},{position:-1,type:"text",content:d("only",e.left,e.right)}]}],transform:(t,i,n)=>{const r=t[i].children[n],s=p(r.content,0,e);let l=i+1;for(;t[l+1]&&t[l+1].nesting===-1;)l++;const o=m(t,l);u(s,o),t[i].children=t[i].children.slice(0,-2)}}),P=e=>({name:"horizontal rule",tests:[{shift:0,type:"paragraph_open"},{shift:1,type:"inline",children:t=>t.length===1,content:t=>new RegExp(`^ {0,3}[-*_]{3,} ?${b(e.left)}[^${b(e.right)}]`).test(t)},{shift:2,type:"paragraph_close"}],transform:(t,i)=>{const n=t[i];n.type="hr",n.tag="hr",n.nesting=0;const{content:r}=t[i+1],s=r.lastIndexOf(e.left),l=p(r,s,e);u(l,n),n.markup=r,t.splice(i+1,2)}}),T=e=>({name:"end of block",tests:[{shift:0,type:"inline",children:[{position:-1,content:d("end",e.left,e.right),type:t=>t!=="code_inline"&&t!=="math_inline"}]}],transform:(t,i,n)=>{const r=t[i].children[n],{content:s}=r,l=p(s,s.lastIndexOf(e.left),e);let o=i+1;for(;t[o+1]&&t[o+1].nesting===-1;)o++;const a=m(t,o);u(l,a);const f=s.slice(0,s.lastIndexOf(e.left));r.content=f[f.length-1]===" "?f.slice(0,-1):f}}),A=["fence","inline","table","list","hr","softbreak","block"],L=e=>{const t=e.rule===!1?[]:Array.isArray(e.rule)?e.rule.filter(n=>A.includes(n)):A,i=[];return t.includes("fence")&&i.push(I(e)),t.includes("inline")&&i.push(...S(e)),t.includes("list")&&i.push(...D(e)),t.includes("table")&&i.push(j(e)),t.includes("softbreak")&&i.push(K(e)),t.includes("hr")&&i.push(P(e)),t.includes("block")&&i.push(T(e)),i},M=(e,t)=>t>=0?e[t]:e[e.length+t],C=e=>Array.isArray(e)&&!!e.length&&e.every(t=>typeof t=="function"),J=e=>Array.isArray(e)&&!!e.length&&e.every(t=>typeof t=="object"),_=(e,t,i)=>{const n={match:!1,position:null},r=i.shift!==void 0?t+i.shift:i.position;if(i.shift!==void 0&&r<0)return n;const s=M(e,r);if(s===void 0)return n;for(const l of Object.keys(i)){if(l==="shift"||l==="position")continue;if(s[l]===void 0)return n;if(l==="children"&&J(i.children)){if(s.children?.length===0)return n;let a;const f=i.children,h=s.children;if(f.every(c=>c.position!==void 0)){if(a=f.every(c=>_(h,c.position,c).match),a){const c=f[f.length-1]?.position??0;n.position=c>=0?c:h.length+c}}else for(let c=0;c<h.length;c++)if(a=f.every(y=>_(h,c,y).match),a){n.position=c;break}if(a===!1)return n;continue}const o=i[l];switch(typeof o){case"boolean":case"number":case"string":if(s[l]!==o)return n;break;case"function":if(!o(s[l]))return n;break;case"object":if(C(o)){if(!o.every(a=>a(s[l])))return n;break}continue;default:throw new Error(`Unknown type of pattern test (key: ${l}). Test should be of type boolean, number, string, function or array of functions.`)}}return n.match=!0,n},Y=(e,{left:t="{",right:i="}",allowed:n=[],rule:r="all"}={})=>{const s=L({left:t,right:i,allowed:n,rule:r}),l=({tokens:o})=>{for(let a=0;a<o.length;a++)for(let f=0;f<s.length;f++){const h=s[f];let c=null;h.tests.every(y=>{const g=_(o,a,y);return g.position!==null&&({position:c}=g),g.match})&&(h.transform(o,a,c),(h.name==="inline attributes"||h.name==="inline nesting 0")&&f--)}};e.core.ruler.before("linkify","attrs",l)};export{Y as attrs}; //# sourceMappingURL=index.js.map