hexo-bitiful-toolkit 0.1.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +9 -1
  2. package/package.json +18 -16
package/dist/index.js CHANGED
@@ -1 +1,9 @@
1
- var Y=Object.defineProperty;var _e=Object.getOwnPropertyDescriptor;var me=Object.getOwnPropertyNames;var xe=Object.prototype.hasOwnProperty;var ye=(t,e)=>()=>(t&&(e=t(t=0)),e);var ae=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),be=(t,e)=>{for(var a in e)Y(t,a,{get:e[a],enumerable:!0})},Ie=(t,e,a,l)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of me(e))!xe.call(t,s)&&s!==a&&Y(t,s,{get:()=>e[s],enumerable:!(l=_e(e,s))||l.enumerable});return t};var Fe=t=>Ie(Y({},"__esModule",{value:!0}),t);var oe={};be(oe,{rgbaToDataURL:()=>ie,rgbaToThumbHash:()=>Le,thumbHashToApproximateAspectRatio:()=>le,thumbHashToAverageRGBA:()=>Te,thumbHashToDataURL:()=>$e,thumbHashToRGBA:()=>se});function Le(t,e,a){if(t>100||e>100)throw new Error(`${t}x${e} doesn't fit in 100x100`);let{PI:l,round:s,max:o,cos:n,abs:r}=Math,h=0,d=0,p=0,c=0;for(let m=0,_=0;m<t*e;m++,_+=4){let g=a[_+3]/255;h+=g/255*a[_],d+=g/255*a[_+1],p+=g/255*a[_+2],c+=g}c&&(h/=c,d/=c,p/=c);let f=c<t*e,K=f?5:7,E=o(1,s(K*t/o(t,e))),z=o(1,s(K*e/o(t,e))),k=[],U=[],M=[],N=[];for(let m=0,_=0;m<t*e;m++,_+=4){let g=a[_+3]/255,v=h*(1-g)+g/255*a[_],i=d*(1-g)+g/255*a[_+1],u=p*(1-g)+g/255*a[_+2];k[m]=(v+i+u)/3,U[m]=(v+i)/2-u,M[m]=v-i,N[m]=g}let D=(m,_,g)=>{let v=0,i=[],u=0,y=[];for(let x=0;x<g;x++)for(let A=0;A*g<_*(g-x);A++){let O=0;for(let $=0;$<t;$++)y[$]=n(l/t*A*($+.5));for(let $=0;$<e;$++)for(let Q=0,ge=n(l/e*x*($+.5));Q<t;Q++)O+=m[Q+$*t]*y[Q]*ge;O/=t*e,A||x?(i.push(O),u=o(u,r(O))):v=O}if(u)for(let x=0;x<i.length;x++)i[x]=.5+.5/u*i[x];return[v,i,u]},[G,w,V]=D(k,o(3,E),o(3,z)),[W,J,X]=D(U,3,3),[S,q,j]=D(M,3,3),[R,H,P]=f?D(N,5,5):[],B=t>e,b=s(63*G)|s(31.5+31.5*W)<<6|s(31.5+31.5*S)<<12|s(31*V)<<18|f<<23,C=(B?z:E)|s(63*X)<<3|s(63*j)<<9|B<<15,F=[b&255,b>>8&255,b>>16,C&255,C>>8],L=f?6:5,T=0;f&&F.push(s(15*R)|s(15*P)<<4);for(let m of f?[w,J,q,H]:[w,J,q])for(let _ of m)F[L+(T>>1)]|=s(15*_)<<((T++&1)<<2);return new Uint8Array(F)}function se(t){let{PI:e,min:a,max:l,cos:s,round:o}=Math,n=t[0]|t[1]<<8|t[2]<<16,r=t[3]|t[4]<<8,h=(n&63)/63,d=(n>>6&63)/31.5-1,p=(n>>12&63)/31.5-1,c=(n>>18&31)/31,f=n>>23,K=(r>>3&63)/63,E=(r>>9&63)/63,z=r>>15,k=l(3,z?f?5:7:r&7),U=l(3,z?r&7:f?5:7),M=f?(t[5]&15)/15:1,N=(t[5]>>4)/15,D=f?6:5,G=0,w=(B,b,C)=>{let F=[];for(let L=0;L<b;L++)for(let T=L?0:1;T*b<B*(b-L);T++)F.push(((t[D+(G>>1)]>>((G++&1)<<2)&15)/7.5-1)*C);return F},V=w(k,U,c),W=w(3,3,K*1.25),J=w(3,3,E*1.25),X=f&&w(5,5,N),S=le(t),q=o(S>1?32:32*S),j=o(S>1?32/S:32),R=new Uint8Array(q*j*4),H=[],P=[];for(let B=0,b=0;B<j;B++)for(let C=0;C<q;C++,b+=4){let F=h,L=d,T=p,m=M;for(let i=0,u=l(k,f?5:3);i<u;i++)H[i]=s(e/q*(C+.5)*i);for(let i=0,u=l(U,f?5:3);i<u;i++)P[i]=s(e/j*(B+.5)*i);for(let i=0,u=0;i<U;i++)for(let y=i?0:1,x=P[i]*2;y*U<k*(U-i);y++,u++)F+=V[u]*H[y]*x;for(let i=0,u=0;i<3;i++)for(let y=i?0:1,x=P[i]*2;y<3-i;y++,u++){let A=H[y]*x;L+=W[u]*A,T+=J[u]*A}if(f)for(let i=0,u=0;i<5;i++)for(let y=i?0:1,x=P[i]*2;y<5-i;y++,u++)m+=X[u]*H[y]*x;let _=F-2/3*L,g=(3*F-_+T)/2,v=g-T;R[b]=l(0,255*a(1,g)),R[b+1]=l(0,255*a(1,v)),R[b+2]=l(0,255*a(1,_)),R[b+3]=l(0,255*a(1,m))}return{w:q,h:j,rgba:R}}function Te(t){let{min:e,max:a}=Math,l=t[0]|t[1]<<8|t[2]<<16,s=(l&63)/63,o=(l>>6&63)/31.5-1,n=(l>>12&63)/31.5-1,h=l>>23?(t[5]&15)/15:1,d=s-2/3*o,p=(3*s-d+n)/2,c=p-n;return{r:a(0,e(1,p)),g:a(0,e(1,c)),b:a(0,e(1,d)),a:h}}function le(t){let e=t[3],a=t[2]&128,l=t[4]&128,s=l?a?5:7:e&7,o=l?e&7:a?5:7;return s/o}function ie(t,e,a){let l=t*4+1,s=6+e*(5+l),o=[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,t>>8,t&255,0,0,e>>8,e&255,8,6,0,0,0,0,0,0,0,s>>>24,s>>16&255,s>>8&255,s&255,73,68,65,84,120,1],n=[0,498536548,997073096,651767980,1994146192,1802195444,1303535960,1342533948,-306674912,-267414716,-690576408,-882789492,-1687895376,-2032938284,-1609899400,-1111625188],r=1,h=0;for(let d=0,p=0,c=l-1;d<e;d++,c+=l-1)for(o.push(d+1<e?0:1,l&255,l>>8,~l&255,l>>8^255,0),h=(h+r)%65521;p<c;p++){let f=a[p]&255;o.push(f),r=(r+f)%65521,h=(h+r)%65521}o.push(h>>8,h&255,r>>8,r&255,0,0,0,0,0,0,0,0,73,69,78,68,174,66,96,130);for(let[d,p]of[[12,29],[37,41+s]]){let c=-1;for(let f=d;f<p;f++)c^=o[f],c=c>>>4^n[c&15],c=c>>>4^n[c&15];c=~c,o[p++]=c>>>24,o[p++]=c>>16&255,o[p++]=c>>8&255,o[p++]=c&255}return"data:image/png;base64,"+btoa(String.fromCharCode(...o))}function $e(t){let e=se(t);return ie(e.w,e.h,e.rgba)}var ce=ye(()=>{});var te=ae((je,re)=>{var Z=require("fs/promises"),ne=require("path"),ee=class{constructor(e={}){this.config={enable:!1,cache_file:".thumbcache.json",hexo_root:process.cwd(),cache_key_type:"img_src",...e},this.cache={},this.isDirty=!1,this.cacheFilePath=ne.join(this.config.hexo_root,this.config.cache_file),this.stats={apiRequests:0,cacheHits:0}}generateCacheKey(e){if(this.config.cache_key_type==="img_filename"){let a=new URL(e);return ne.basename(a.pathname)}else return e}async loadCacheFromFile(){try{if(console.log("[BITIFUL] Loading cache from local file..."),await Z.access(this.cacheFilePath).then(()=>!0).catch(()=>!1)){let e=await Z.readFile(this.cacheFilePath,"utf8");this.cache=JSON.parse(e||"{}"),console.log(`[BITIFUL] Cache loaded from ${this.cacheFilePath}, ${Object.keys(this.cache).length} items`)}else console.log("[BITIFUL] Cache file not found, starting with empty cache"),this.cache={};return!0}catch(e){return console.warn("[BITIFUL] Failed to load cache from file:",e.message),this.cache={},!1}}async saveCacheToFile(){if(!this.config.enable||!this.isDirty)return console.log("[BITIFUL] Skipping cache save: disabled or no changes"),!1;try{console.log("[BITIFUL] Saving cache to local file...");let e=JSON.stringify(this.cache,null,2);return Z.writeFileSync(this.cacheFilePath,e,"utf8"),console.log(`[BITIFUL] Cache saved to ${this.cacheFilePath}, ${Object.keys(this.cache).length} items`),this.isDirty=!1,!0}catch(e){return console.warn("[BITIFUL] Failed to save cache to file:",e.message),!1}}getCachedDataURL(e){let a=this.generateCacheKey(e);return this.cache[a]?(this.stats.cacheHits++,this.cache[a]):(this.stats.apiRequests++,null)}setCachedDataURL(e,a){let l=this.generateCacheKey(e);a&&this.cache[l]!==a&&(this.cache[l]=a,this.isDirty=!0)}getStats(){let e=this.stats.apiRequests+this.stats.cacheHits;return{totalItems:Object.keys(this.cache).length,isDirty:this.isDirty,cacheEnabled:this.config.enable,...this.stats,totalRequests:e,cacheHitRate:e>0?(this.stats.cacheHits/e*100).toFixed(1):0}}};re.exports=ee});var ue=ae((Pe,fe)=>{var{thumbHashToDataURL:Ue}=(ce(),Fe(oe)),He=te();function Be(t){try{let e=t.replace(/\s/g,""),a=atob(e),l=new Uint8Array(a.length);for(let s=0;s<a.length;s++)l[s]=a.charCodeAt(s);return l}catch(e){throw new Error(`Base64 decode failed: ${e.message}`)}}async function Ce(t,e=null){file_name=t.split("/").pop().split("?")[0];try{if(e){let r=e.getCachedDataURL(t);if(r)return r}console.log(`[BITIFUL] \u274C ${file_name} \u7F13\u5B58\u672A\u547D\u4E2D\uFF0C\u6B63\u5728\u83B7\u53D6...`);let a=`${t.split("?")[0]}?fmt=thumbhash`,s=await(await fetch(a)).text(),o=Be(s.trim()),n=Ue(o);return e&&n&&(console.log(`[BITIFUL] \u5C06${t}\u53CA\u5176\u5BF9\u5E94\u7684dataUrl\u5199\u5165\u7F13\u5B58`),e.setCachedDataURL(t,n)),n}catch(a){return console.warn("Failed to generate thumbhash:",a.message),null}}function he(t,e,a){let l=e.some(n=>t.includes(n)),s=t.split(".").pop().split("?")[0].toLowerCase(),o=!a.includes(s);return l&&o}function ve(t,e,a,l,s){let o=a.srcset_widths.map(d=>`${e.includes("?")?`${e}&w=${d}`:`${e}?w=${d}`} ${d}w`).join(", "),n=t.replace(/<img\s+/,"").replace(/\s*\/?>$/,"").replace(/src="[^"]*"\s*/,""),r=`${[`<img src="${e}" srcset="${o}"`,a.add_max_width?`sizes="${a.max_widths}"`:"",l?'loading="lazy" decoding="async"':"",`onload="this.classList.add('loaded')"`,n].filter(Boolean).join(" ")}>`,h=`<img class="thumbhash-placeholder" src="${s}">`;return s?`<div class="progressive_img_container">${r}${h}</div>`:r}var we=async(t,e={})=>{let a={srcset_widths:[200,400,600,800,1200,2e3,3e3],add_max_width:!1,max_widths:"(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 800px",enable_lazy_loading:!0,lazy_skip_first:2,supported_domains:["assets.vluv.space","s3.bitiful.net","bitiful.com"],exclude_formats:["svg","gif"],cache:{enable:!1,...e.cache},...e},l=global.bitifulCacheInstance,s=/<img[^>]+src="(.+?)"[^>]*>/gm,o=[...t.matchAll(s)],n=await Promise.all(o.map(([,h])=>he(h,a.supported_domains,a.exclude_formats)?Ce(h,l):null)),r=0;return t.replace(s,(h,d)=>{if(!he(d,a.supported_domains,a.exclude_formats))return h;let p=a.enable_lazy_loading&&++r>a.lazy_skip_first,c=n[r-1];return ve(h,d,a,p,c)})};fe.exports=we});var de=ue(),qe=te(),Re=require("fs").promises.readFile,{join:Ae}=require("path"),I=hexo.log,pe,ke=((pe=hexo.config.bitiful_toolkit)==null?void 0:pe.env_name)||"CI";function De(){var e;if(!hexo.config.bitiful_toolkit||!hexo.config.bitiful_toolkit.enable||process.env[ke]!=="true")return;let t=null;(e=hexo.config.bitiful_toolkit.cache)!=null&&e.enable&&(t=new qe({...hexo.config.bitiful_toolkit.cache,hexo_root:hexo.base_dir}),global.bitifulCacheInstance=t,hexo.extend.filter.register("before_generate",async()=>{I.info("[BITIFUL] Initializing thumbhash cache..."),await t.loadCacheFromFile();let a=t.getStats();I.info(`[BITIFUL] Cache initialized with ${a.totalItems} items`)},8),hexo.extend.filter.register("after_generate",async()=>{if(t){let a=t.getStats();I.info("[BITIFUL] \u56FE\u7247\u5904\u7406\u5B8C\u6210\u7EDF\u8BA1:"),I.info(`[BITIFUL] - \u603B\u8BF7\u6C42\u6570: ${a.totalRequests}`),I.info(`[BITIFUL] - \u5B9E\u9645API\u8BF7\u6C42\u6570: ${a.apiRequests}`),I.info(`[BITIFUL] - \u7F13\u5B58\u547D\u4E2D\u6570: ${a.cacheHits}`),I.info(`[BITIFUL] - \u7F13\u5B58\u547D\u4E2D\u7387: ${a.cacheHitRate}%`),I.info(`[BITIFUL] - \u7F13\u5B58\u603B\u6761\u76EE\u6570: ${a.totalItems}`),a.isDirty?(I.info("[BITIFUL] Saving updated cache to local file..."),await t.saveCacheToFile()):I.info("[BITIFUL] No cache updates, skipping save")}})),hexo.config.bitiful_toolkit.inject_css&&(hexo.extend.generator.register("bitiful_assets",async()=>{let a=await Re(Ae(__dirname,"style/progressive_image.css"),"utf-8");return[{path:"css/progressive_image.css",data:()=>a}]}),hexo.extend.injector.register("head_end",()=>`<link rel="stylesheet" href="${hexo.config.root}css/progressive_image.css">`)),hexo.config.bitiful_toolkit.all?(I.info("[BITIFUL] process all image"),hexo.extend.filter.register("after_render:html",async a=>(I.info("html: ",a),a=await de(a,hexo.config.bitiful_toolkit),a),15)):(I.info("[BITIFUL] process post images"),hexo.extend.filter.register("before_post_render",async a=>(a.content=await de(a.content,hexo.config.bitiful_toolkit),a),15))}De();
1
+ var Q=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var at=Q((qt,nt)=>{function pt(e,t,s){if(e>100||t>100)throw new Error(`${e}x${t} doesn't fit in 100x100`);let{PI:c,round:o,max:n,cos:r,abs:l}=Math,p=0,g=0,h=0,i=0;for(let _=0,x=0;_<e*t;_++,x+=4){let b=s[x+3]/255;p+=b/255*s[x],g+=b/255*s[x+1],h+=b/255*s[x+2],i+=b}i&&(p/=i,g/=i,h/=i);let f=i<e*t,y=f?5:7,m=n(1,o(y*e/n(e,t))),d=n(1,o(y*t/n(e,t))),$=[],L=[],H=[],W=[];for(let _=0,x=0;_<e*t;_++,x+=4){let b=s[x+3]/255,D=p*(1-b)+b/255*s[x],a=g*(1-b)+b/255*s[x+1],u=h*(1-b)+b/255*s[x+2];$[_]=(D+a+u)/3,L[_]=(D+a)/2-u,H[_]=D-a,W[_]=b}let N=(_,x,b)=>{let D=0,a=[],u=0,F=[];for(let I=0;I<b;I++)for(let S=0;S*b<x*(b-I);S++){let O=0;for(let C=0;C<e;C++)F[C]=r(c/e*S*(C+.5));for(let C=0;C<t;C++)for(let K=0,dt=r(c/t*I*(C+.5));K<e;K++)O+=_[K+C*e]*F[K]*dt;O/=e*t,S||I?(a.push(O),u=n(u,l(O))):D=O}if(u)for(let I=0;I<a.length;I++)a[I]=.5+.5/u*a[I];return[D,a,u]},[G,k,V]=N($,n(3,m),n(3,d)),[X,J,Y]=N(L,3,3),[M,R,P]=N(H,3,3),[j,z,E]=f?N(W,5,5):[],q=e>t,w=o(63*G)|o(31.5+31.5*X)<<6|o(31.5+31.5*M)<<12|o(31*V)<<18|f<<23,A=(q?d:m)|o(63*Y)<<3|o(63*P)<<9|q<<15,v=[w&255,w>>8&255,w>>16,A&255,A>>8],B=f?6:5,U=0;f&&v.push(o(15*j)|o(15*E)<<4);for(let _ of f?[k,J,R,z]:[k,J,R])for(let x of _)v[B+(U>>1)]|=o(15*x)<<((U++&1)<<2);return new Uint8Array(v)}function st(e){let{PI:t,min:s,max:c,cos:o,round:n}=Math,r=e[0]|e[1]<<8|e[2]<<16,l=e[3]|e[4]<<8,p=(r&63)/63,g=(r>>6&63)/31.5-1,h=(r>>12&63)/31.5-1,i=(r>>18&31)/31,f=r>>23,y=(l>>3&63)/63,m=(l>>9&63)/63,d=l>>15,$=c(3,d?f?5:7:l&7),L=c(3,d?l&7:f?5:7),H=f?(e[5]&15)/15:1,W=(e[5]>>4)/15,N=f?6:5,G=0,k=(q,w,A)=>{let v=[];for(let B=0;B<w;B++)for(let U=B?0:1;U*w<q*(w-B);U++)v.push(((e[N+(G>>1)]>>((G++&1)<<2)&15)/7.5-1)*A);return v},V=k($,L,i),X=k(3,3,y*1.25),J=k(3,3,m*1.25),Y=f&&k(5,5,W),M=ot(e),R=n(M>1?32:32*M),P=n(M>1?32/M:32),j=new Uint8Array(R*P*4),z=[],E=[];for(let q=0,w=0;q<P;q++)for(let A=0;A<R;A++,w+=4){let v=p,B=g,U=h,_=H;for(let a=0,u=c($,f?5:3);a<u;a++)z[a]=o(t/R*(A+.5)*a);for(let a=0,u=c(L,f?5:3);a<u;a++)E[a]=o(t/P*(q+.5)*a);for(let a=0,u=0;a<L;a++)for(let F=a?0:1,I=E[a]*2;F*L<$*(L-a);F++,u++)v+=V[u]*z[F]*I;for(let a=0,u=0;a<3;a++)for(let F=a?0:1,I=E[a]*2;F<3-a;F++,u++){let S=z[F]*I;B+=X[u]*S,U+=J[u]*S}if(f)for(let a=0,u=0;a<5;a++)for(let F=a?0:1,I=E[a]*2;F<5-a;F++,u++)_+=Y[u]*z[F]*I;let x=v-2/3*B,b=(3*v-x+U)/2,D=b-U;j[w]=c(0,255*s(1,b)),j[w+1]=c(0,255*s(1,D)),j[w+2]=c(0,255*s(1,x)),j[w+3]=c(0,255*s(1,_))}return{w:R,h:P,rgba:j}}function gt(e){let{min:t,max:s}=Math,c=e[0]|e[1]<<8|e[2]<<16,o=(c&63)/63,n=(c>>6&63)/31.5-1,r=(c>>12&63)/31.5-1,p=c>>23?(e[5]&15)/15:1,g=o-2/3*n,h=(3*o-g+r)/2,i=h-r;return{r:s(0,t(1,h)),g:s(0,t(1,i)),b:s(0,t(1,g)),a:p}}function ot(e){let t=e[3],s=e[2]&128,c=e[4]&128,o=c?s?5:7:t&7,n=c?t&7:s?5:7;return o/n}function ct(e,t,s){let c=e*4+1,o=6+t*(5+c),n=[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,e>>8,e&255,0,0,t>>8,t&255,8,6,0,0,0,0,0,0,0,o>>>24,o>>16&255,o>>8&255,o&255,73,68,65,84,120,1],r=[0,498536548,997073096,651767980,1994146192,1802195444,1303535960,1342533948,-306674912,-267414716,-690576408,-882789492,-1687895376,-2032938284,-1609899400,-1111625188],l=1,p=0;for(let g=0,h=0,i=c-1;g<t;g++,i+=c-1)for(n.push(g+1<t?0:1,c&255,c>>8,~c&255,c>>8^255,0),p=(p+l)%65521;h<i;h++){let f=s[h]&255;n.push(f),l=(l+f)%65521,p=(p+l)%65521}n.push(p>>8,p&255,l>>8,l&255,0,0,0,0,0,0,0,0,73,69,78,68,174,66,96,130);for(let[g,h]of[[12,29],[37,41+o]]){let i=-1;for(let f=g;f<h;f++)i^=n[f],i=i>>>4^r[i&15],i=i>>>4^r[i&15];i=~i,n[h++]=i>>>24,n[h++]=i>>16&255,n[h++]=i>>8&255,n[h++]=i&255}return`data:image/png;base64,${btoa(String.fromCharCode(...n))}`}function mt(e){let t=st(e);return ct(t.w,t.h,t.rgba)}nt.exports={rgbaToThumbHash:pt,thumbHashToRGBA:st,thumbHashToAverageRGBA:gt,thumbHashToApproximateAspectRatio:ot,rgbaToDataURL:ct,thumbHashToDataURL:mt}});var et=Q((At,it)=>{var Z=require("node:fs/promises"),bt=require("node:path"),tt=class{constructor(t={}){this.config={cache_file:"thumbcache.json",hexo_root:process.cwd(),...t},this.cache={},this.hasUpdated=!1,this.cacheFilePath=bt.join(this.config.hexo_root,this.config.cache_file),this.stats={apiRequests:0,cacheHits:0}}async loadCacheFromFile(){try{if(console.log("[BITIFUL] Loading cache from local file..."),await Z.access(this.cacheFilePath).then(()=>!0).catch(()=>!1)){let t=await Z.readFile(this.cacheFilePath,"utf8");this.cache=JSON.parse(t||"{}"),console.log(`[BITIFUL] Cache loaded from ${this.cacheFilePath}, ${Object.keys(this.cache).length} items`)}else console.log("[BITIFUL] Cache file not found, starting with empty cache"),this.cache={};return!0}catch(t){return console.warn("[BITIFUL] Failed to load cache from file:",t.message),this.cache={},!1}}async saveCacheToFile(){if(!this.config.enable||!this.hasUpdated)return console.log("[BITIFUL] Skipping cache save: disabled or no changes"),!1;try{console.log("[BITIFUL] Saving cache to local file...");let t=JSON.stringify(this.cache,null,2);return await Z.writeFile(this.cacheFilePath,t,"utf8"),console.log(`[BITIFUL] Cache saved to ${this.cacheFilePath}, ${Object.keys(this.cache).length} items`),this.hasUpdated=!1,!0}catch(t){return console.warn("[BITIFUL] Failed to save cache to file:",t.message),!1}}getCachedDataURL(t){if(this.cache[t]){this.stats.cacheHits++;let s=this.cache[t];return typeof s=="string"?s:s&&typeof s=="object"&&s.dataURL||null}return this.stats.apiRequests++,null}getCachedDimensions(t){let s=this.cache[t];return s?(this.stats.cacheHits++,typeof s=="object"&&Number.isFinite(s?.width)&&Number.isFinite(s?.height)?{width:s.width,height:s.height}:null):(this.stats.apiRequests++,null)}setCachedDataURL(t,s){if(!s)return;let c=this.cache[t];typeof c=="object"?c.dataURL!==s&&(this.cache[t]={...c,dataURL:s},this.hasUpdated=!0):c!==s&&(this.cache[t]={dataURL:s},this.hasUpdated=!0)}setCachedDimensions(t,s,c){if(!Number.isFinite(s)||!Number.isFinite(c))return;let o=this.cache[t];(o.width!==s||o.height!==c)&&(this.cache[t]={...o,width:s,height:c},this.hasUpdated=!0)}getStats(){let t=this.stats.apiRequests+this.stats.cacheHits;return{totalItems:Object.keys(this.cache).length,isDirty:this.hasUpdated,cacheEnabled:this.config.enable,...this.stats,totalRequests:t,cacheHitRate:t>0?(this.stats.cacheHits/t*100).toFixed(1):0}}};it.exports=tt});var lt=Q((kt,rt)=>{var{thumbHashToDataURL:yt}=at(),Dt=et();async function xt(e,t=null){try{if(t){let l=t.getCachedDimensions(e);if(l)return l}console.log(`[BITIFUL] \u274C ${e.split("/").pop().split("?")[0]} \u5C3A\u5BF8\u7F13\u5B58\u672A\u547D\u4E2D\uFF0C\u6B63\u5728\u83B7\u53D6...`);let s=`${e.split("?")[0]}?fmt=info`,o=await(await fetch(s)).json(),n=o.ImageWidth,r=o.ImageHeight;return Number.isFinite(n)&&Number.isFinite(r)?(t&&t.setCachedDimensions(e,Number(n),Number(r)),{width:Number(n),height:Number(r)}):null}catch(s){return console.warn("Failed to generate thumbhash:",s.message),null}}async function _t(e,t=null){try{if(t){let l=t.getCachedDataURL(e);if(l)return l}console.log(`[BITIFUL] \u274C ${e.split("/").pop().split("?")[0]} \u7F13\u5B58\u672A\u547D\u4E2D\uFF0C\u6B63\u5728\u83B7\u53D6...`);let s=`${e.split("?")[0]}?fmt=thumbhash`,o=await(await fetch(s)).text(),n=It(o.trim()),r=yt(n);return t&&r&&(console.log(`[BITIFUL] \u5C06${e}\u53CA\u5176\u5BF9\u5E94\u7684dataUrl\u5199\u5165\u7F13\u5B58`),t.setCachedDataURL(e,r)),r}catch(s){return console.warn("Failed to generate thumbhash:",s.message),null}}function It(e){try{let t=e.replace(/\s/g,""),s=atob(t),c=new Uint8Array(s.length);for(let o=0;o<s.length;o++)c[o]=s.charCodeAt(o);return c}catch(t){throw new Error(`Base64 decode failed: ${t.message}`)}}rt.exports={getDimension:xt,getDataURL:_t}});var ft=Q((Rt,ht)=>{var{getDimension:Ft,getDataURL:wt}=lt();function $t(e,t,s){let c=t.some(r=>e.includes(r)),o=e.split(".").pop().split("?")[0].toLowerCase(),n=!s.includes(o);return c&&n}function Lt(e,t,s,c,o,n){let r=s.srcset_widths.map(m=>`${t.includes("?")?`${t}&w=${m}`:`${t}?w=${m}`} ${m}w`).join(", "),l=e.replace(/<img\s+/,"").replace(/\s*\/?>$/,"").replace(/src="[^"]*"\s*/,""),p=null,g=e.match(/\bwidth\s*=\s*["']?([^"'\s>]+)/i);if(g)p=g[1];else{let m=e.match(/style\s*=\s*["']([^"']*?)["']/i);if(m){let d=m[1].match(/width\s*:\s*([^;]+)/i);d&&(p=d[1].trim())}}o&&(l=l.replace(/\bwidth\s*=\s*["']?[^"'\s>]+["']?\s*/gi,""),l=l.replace(/style\s*=\s*["']([^"']*?)["']/gi,(m,d)=>{let $=d.replace(/width\s*:\s*[^;]+;?\s*/gi,"").trim();return $?`style="${$}"`:""}));let h=`${[`<img src="${t}" srcset="${r}"`,c?'loading="lazy" decoding="async"':'loading="eager"',o?'style="width: 100%; height: 100%; object-fit: cover;"':"",`onload="this.style.opacity=1; this.parentElement.style.backgroundImage='none';"`,l].filter(Boolean).join(" ")}>`,i=o?`background-image: url('${o}');`:"",f=n?`${n.width} / ${n.height}`:"auto",y=p||"100%";return o&&n?`<div class="pic" style="
2
+ position: relative;
3
+ overflow: hidden;
4
+ max-width: ${n.width}px;
5
+ width: ${y};
6
+ aspect-ratio: ${f};
7
+ ${i}
8
+ background-size: cover;
9
+ background-repeat: no-repeat;">${h}</div>`:h}async function Tt(e,t){let s={srcset_widths:[400,600,800,1200,2e3,3e3],add_max_width:!1,max_widths:"(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 800px",enable_lazy_loading:!0,lazy_skip_first:2,supported_domains:["assets.vluv.space","s3.bitiful.net","bitiful.com"],exclude_formats:["svg","gif"],...t},c=global.bitifulCacheInstance,o=/<img[^>]+src="(.+?)"[^>]*>/gm,n=Array.from(e.matchAll(o));if(n.length===0)return e;let r=new Map;n.forEach(y=>{let[m,d]=y;$t(d,s.supported_domains,s.exclude_formats)&&(r.has(d)||r.set(d,{src:d}))});let l=Array.from(r.keys()),p=await Promise.all(l.map(async y=>{let[m,d]=await Promise.all([wt(y,c),Ft(y,c)]);return{src:y,thumbhash:m,dimensions:d}})),g=new Map(p.map(y=>[y.src,y])),h="",i=0,f=0;for(let y of n){let[m,d]=y,$=y.index;h+=e.slice(i,$);let L=g.get(d);if(L){let H=s.enable_lazy_loading&&++f>s.lazy_skip_first;h+=Lt(m,d,s,H,L.thumbhash,L.dimensions)}else h+=m;i=$+m.length}return h+=e.slice(i),h}ht.exports=Tt});var ut=ft(),vt=et(),T=hexo.log,Bt=hexo.config.bitiful_toolkit?.env_name||"CI";function Ut(){if(!hexo.config.bitiful_toolkit||!hexo.config.bitiful_toolkit.enable||process.env[Bt]!=="true")return;let e=new vt({...hexo.config.bitiful_toolkit.cache,hexo_root:hexo.base_dir});global.bitifulCacheInstance=e,hexo.extend.filter.register("before_generate",async()=>{T.info("[BITIFUL] Initializing thumbhash cache..."),await e.loadCacheFromFile();let t=e.getStats();T.info(`[BITIFUL] Cache initialized with ${t.totalItems} items`)},8),hexo.extend.filter.register("after_generate",async()=>{if(e){let t=e.getStats();T.info("[BITIFUL] \u56FE\u7247\u5904\u7406\u5B8C\u6210\u7EDF\u8BA1:"),T.info(`[BITIFUL] - \u603B\u8BF7\u6C42\u6570: ${t.totalRequests}`),T.info(`[BITIFUL] - \u5B9E\u9645API\u8BF7\u6C42\u6570: ${t.apiRequests}`),T.info(`[BITIFUL] - \u7F13\u5B58\u547D\u4E2D\u6570: ${t.cacheHits}`),T.info(`[BITIFUL] - \u7F13\u5B58\u547D\u4E2D\u7387: ${t.cacheHitRate}%`),T.info(`[BITIFUL] - \u7F13\u5B58\u603B\u6761\u76EE\u6570: ${t.totalItems}`),t.isDirty?(T.info("[BITIFUL] Saving updated cache to local file..."),await e.saveCacheToFile()):T.info("[BITIFUL] No cache updates, skipping save")}}),hexo.config.bitiful_toolkit.all?(T.info("[BITIFUL] process all image"),hexo.extend.filter.register("after_render:html",async t=>(t=await ut(t,hexo.config.bitiful_toolkit),t),15)):(T.info("[BITIFUL] process post images"),hexo.extend.filter.register("before_post_render",async t=>(t.content=await ut(t.content,hexo.config.bitiful_toolkit),t),15))}Ut();
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
- "name": "hexo-bitiful-toolkit",
3
- "main": "dist/index.js",
4
- "version": "0.1.3",
5
- "files": [
6
- "dist/index.js",
7
- "README.md"
8
- ],
9
- "scripts": {
10
- "build": "node build.js",
11
- "test": "node test/test_replace.js",
12
- "test-cache": "node test/test_local_cache.js"
13
- },
14
- "keywords": [ "hexo" ],
15
- "devDependencies": {
16
- "esbuild": "^0.27.2"
17
- }
2
+ "name": "hexo-bitiful-toolkit",
3
+ "main": "dist/index.js",
4
+ "version": "1.0.0",
5
+ "files": [
6
+ "dist/index.js",
7
+ "README.md"
8
+ ],
9
+ "scripts": {
10
+ "build": "node build.js",
11
+ "test": "node test/test_replace.js",
12
+ "test-cache": "node test/test_local_cache.js"
13
+ },
14
+ "keywords": [
15
+ "hexo"
16
+ ],
17
+ "devDependencies": {
18
+ "esbuild": "^0.27.2"
19
+ }
18
20
  }