3 lines
5.8 KiB
JavaScript
3 lines
5.8 KiB
JavaScript
import{isSpace as d}from"markdown-it/lib/common/utils.mjs";const b=(e,l,t)=>{let f;const n=l,h={ok:!1,pos:l,value:""};for(f=e.charCodeAt(l);l<t&&f>=48&&f<=57||f===37;)f=e.charCodeAt(++l);return h.ok=!0,h.pos=l,h.value=e.slice(n,l),h},z=(e,l,t)=>{if(e.charCodeAt(l)!==61)return null;l++;const f=e.charCodeAt(l);if(f!==120&&(f<48||f>57))return null;const n=b(e,l,t);if(l=n.pos,e.charCodeAt(l++)!==120)return null;const h=b(e,l,t);return l=h.pos,{pos:l,width:n.value,height:h.value}},M=(e,l)=>{const t=e.env,f=e.pos,n=e.posMax;if(e.src.charCodeAt(e.pos)!==33||e.src.charCodeAt(e.pos+1)!==91)return!1;const h=e.pos+2,a=e.md.helpers.parseLinkLabel(e,e.pos+1,!1);if(a<0)return!1;let o=a+1,m,p="",A="",C="",s="";if(o<n&&e.src.charCodeAt(o)===40){for(o++;o<n&&d(e.src.charCodeAt(o));)o++;if(o+5>n)return!1;let i;i=e.md.helpers.parseLinkDestination(e.src,o,e.posMax),i.ok&&(p=e.md.normalizeLink(i.str),e.md.validateLink(p)?o=i.pos:p="");const u=o;for(;o<n&&d(e.src.charCodeAt(o));o++);i=e.md.helpers.parseLinkTitle(e.src,o,e.posMax);let r=!1;if(u!==o&&i.ok){for(A=i.str,o=i.pos;o<n;o++)if(!d(e.src.charCodeAt(o))){r=!0;break}if(!r||o+3>n)return!1}else A="";const c=z(e.src,o,e.posMax);if(c)for({width:C,height:s,pos:o}=c;o<n&&d(e.src.charCodeAt(o));o++);if(o>=n||e.src.charCodeAt(o)!==41)return e.pos=f,!1;o++}else{let i="";if(typeof t.references>"u")return!1;for(;o<n&&(m=e.src.charCodeAt(o),!(m!==32&&m!==9));o++);if(o<n&&e.src.charCodeAt(o)===91){const r=o+1;o=e.md.helpers.parseLinkLabel(e,o),o>=0?i=e.src.slice(r,o++):o=a+1}else o=a+1;i||(i=e.src.slice(h,a));const u=t.references[e.md.utils.normalizeReference(i)];if(!u)return e.pos=f,!1;p=u.href,A=u.title??""}if(!l){const i=e.src.slice(h,a),u=[];e.md.inline.parse(i,e.md,e.env,u);const r=e.push("image","img",0),c=[["src",p],["alt",""]];A&&c.push(["title",A]),C&&c.push(["width",C]),s&&c.push(["height",s]),r.attrs=c,r.children=u,r.content=i}return e.pos=o,e.posMax=n,!0},w=e=>{e.inline.ruler.before("emphasis","image",M)},L=e=>e>=48&&e<=57,S=e=>{const l=e.length;let t=e.lastIndexOf("|");if(t===-1)return null;const f=e.substring(0,t++).trimEnd();for(;t<l&&d(e.charCodeAt(t));)t++;if(t===l)return null;const n=t;for(;t<l&&L(e.charCodeAt(t));)t++;if(t===n||t===l)return null;const h=e.substring(n,t);for(;t<l&&d(e.charCodeAt(t));)t++;if(t===l||e.charCodeAt(t++)!==120)return null;for(;t<l&&d(e.charCodeAt(t));)t++;const a=t;for(;t<l&&L(e.charCodeAt(t));)t++;if(t===a)return null;const o=e.substring(a,t),m=Number(h),p=Number(o);if(!m&&!p)return null;for(;t<l;)if(!d(e.charCodeAt(t++)))return null;return{label:f,width:m?h:null,height:p?o:null}},x=(e,l)=>{const t=e.env,f=e.pos,n=e.posMax;if(e.src.charCodeAt(e.pos)!==33||e.src.charCodeAt(e.pos+1)!==91)return!1;const h=e.pos+2,a=e.md.helpers.parseLinkLabel(e,e.pos+1,!1);if(a<0)return!1;const o=e.src.slice(h,a),m=S(o);if(!m)return!1;const{label:p,width:A,height:C}=m;let s=a+1,i="",u="";if(s<n&&e.src.charCodeAt(s)===40){for(s++;s<n&&d(e.src.charCodeAt(s));)s++;if(s===n)return!1;let r;r=e.md.helpers.parseLinkDestination(e.src,s,e.posMax),r.ok&&(i=e.md.normalizeLink(r.str),e.md.validateLink(i)?s=r.pos:i="");const c=s;for(;s<n&&d(e.src.charCodeAt(s));s++);if(r=e.md.helpers.parseLinkTitle(e.src,s,e.posMax),s<n&&c!==s&&r.ok)for(u=r.str,s=r.pos;s<n&&d(e.src.charCodeAt(s));s++);else u="";if(s>=n||e.src.charCodeAt(s)!==41)return e.pos=f,!1;s++}else{let r="";if(typeof t.references>"u")return!1;for(;s<n&&d(e.src.charCodeAt(s));s++);if(s<n&&e.src.charCodeAt(s)===91){const g=s+1;s=e.md.helpers.parseLinkLabel(e,s),s>=0?r=e.src.slice(g,s++):s=a+1}else s=a+1;r||(r=p);const c=t.references[e.md.utils.normalizeReference(r)];if(!c)return e.pos=f,!1;i=c.href,u=c.title??""}if(!l){const r=e.push("image","img",0),c=[["src",i],["alt",""]];u&&c.push(["title",u]),A&&c.push(["width",A]),C&&c.push(["height",C]);const g=[];e.md.inline.parse(p,e.md,e.env,g),r.attrs=c,r.children=g,r.content=p}return e.pos=s,e.posMax=n,!0},I=e=>{e.inline.ruler.before("image","obsidian-img-size",x)},k=e=>e>=48&&e<=57,R=e=>{const l=e.length;let t=e.lastIndexOf("=");if(t===-1||t+3>l||t!==0&&!d(e.charCodeAt(t-1)))return null;const f=e.substring(0,t++).trimEnd();let n=null,h=null;if(k(e.charCodeAt(t))){const a=t;for(;t<l&&k(e.charCodeAt(t));)t++;if(n=e.substring(a,t),e.charCodeAt(t++)!==120)return null}else if(e.charCodeAt(t++)!==120)return null;if(t<l){const a=t;for(;t<l&&k(e.charCodeAt(t));)t++;t>a&&(h=e.substring(a,t))}for(;t<l;){if(!d(e.charCodeAt(t)))return null;t++}return{label:f,width:n,height:h}},v=(e,l)=>{const t=e.env,f=e.pos,n=e.posMax;if(e.src.charCodeAt(e.pos)!==33||e.src.charCodeAt(e.pos+1)!==91)return!1;const h=e.pos+2,a=e.md.helpers.parseLinkLabel(e,e.pos+1,!1);if(a<0)return!1;const o=e.src.slice(h,a),m=R(o);if(!m)return!1;const{label:p,width:A,height:C}=m;let s=a+1,i="",u="";if(s<n&&e.src.charCodeAt(s)===40){for(s++;s<n&&d(e.src.charCodeAt(s));)s++;if(s>=n)return!1;let r;r=e.md.helpers.parseLinkDestination(e.src,s,e.posMax),r.ok&&(i=e.md.normalizeLink(r.str),e.md.validateLink(i)?s=r.pos:i="");const c=s;for(;s<n&&d(e.src.charCodeAt(s));s++);if(r=e.md.helpers.parseLinkTitle(e.src,s,e.posMax),s<n&&c!==s&&r.ok)for(u=r.str,s=r.pos;s<n&&d(e.src.charCodeAt(s));s++);else u="";if(s>=n||e.src.charCodeAt(s)!==41)return e.pos=f,!1;s++}else{let r="";if(typeof t.references>"u")return!1;for(;s<n&&d(e.src.charCodeAt(s));s++);if(s<n&&e.src.charCodeAt(s)===91){const g=s+1;s=e.md.helpers.parseLinkLabel(e,s),s>=0?r=e.src.slice(g,s++):s=a+1}else s=a+1;r||(r=p);const c=t.references[e.md.utils.normalizeReference(r)];if(!c)return e.pos=f,!1;i=c.href,u=c.title??""}if(!l){const r=e.push("image","img",0),c=[["src",i],["alt",""]];u&&c.push(["title",u]),A&&c.push(["width",A]),C&&c.push(["height",C]);const g=[];e.md.inline.parse(p,e.md,e.env,g),r.attrs=c,r.children=g,r.content=p}return e.pos=s,e.posMax=n,!0},y=e=>{e.inline.ruler.before("image","img-size",v)};export{y as imgSize,v as imgSizeRule,w as legacyImgSize,I as obsidianImgSize,x as obsidianImgSizeRule};
|
|
//# sourceMappingURL=index.js.map
|