pin-diary-script
code:script.js
//pin-diary.js
function oe(e,t){let r=0,o=[];for(let n=0;n<t.length;n++){let s=e.findIndex((a,i)=>i>=r&&a.trim()===tn.trim());if(s<0){o.push(tn);continue}o.push(...e.slice(r,s+1)),r=s+1}return o.push(...e.slice(r)),o}function ne(e,t){let r=-1;for(let o of t){let n=e.findIndex((s,a)=>a>r&&s.trim()===o.trim());if(n<0)return-1;r=n}return r}function se(e,t,r){let o=oe(oe(e,t).reverse(),...r.reverse()).reverse(),n=ne(o,t),s=o.length-1-ne(...o.reverse(),...r.reverse());return[...o.slice(0,n+1),"",...o.slice(n+1,s).join(` ).trim().split(
),"",...o.slice(s)]}var b=e=>{let{fetch:t=globalThis.fetch,hostName:r="scrapbox.io",...o}=e;return{fetch:t,hostName:r,...o}};var x=class extends Error{constructor(t){super(${t.status} ${t.statusText} when fetching ${t.path.toString()});this.name="UnexpectedResponseError";this.status=t.status,this.statusText=t.statusText,this.body=t.body,this.path=t.path,Error.captureStackTrace&&Error.captureStackTrace(this,x)}};var ie=async e=>{let{sid:t,hostName:r,fetch:o}=b(e??{}),n=https://${r}/api/users/me,s=await o(n,t?{headers:{Cookie:E(t)}}:void 0);if(!s.ok)throw new x({path:new URL(n),...s,body:await s.text()});return await s.json()};var E=e=>connect.sid=${e};var ot=e=>typeof e=="object"&&e!==null,nt=e=>ot(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,v=e=>{try{let t=typeof e=="string"?JSON.parse(e):e;return nt(t)?t:!1}catch(t){if(t instanceof SyntaxError)return!1;throw t}};var B=e=>e.replaceAll(" ","_").toLowerCase();var ae=e=>[...e].map((t,r)=>t===" "?"_":!st.includes(t)||r===e.length-1&&it.includes(t)?encodeURIComponent(t):t).join(""),st='@$&+=:;",',it=':;",';var De=async(e,t,r)=>{let{sid:o,hostName:n,fetch:s,followRename:a}=b(r??{}),i=https://${n}/api/pages/${e}/${ae(t)}?followRename=${a??!0},c=await s(i,o?{headers:{Cookie:E(o)}}:void 0);if(!c.ok){let p=await c.text(),h=v(p);if(!h)throw new x({path:new URL(i),...c,body:p});return{ok:!1,value:h}}let d=await c.json();return{ok:!0,value:d}},ce=async(e,t)=>{let{sid:r,hostName:o,fetch:n,sort:s,limit:a,skip:i}=b(t??{}),c=new URLSearchParams;s!==void 0&&c.append("sort",s),a!==void 0&&c.append("limit",${a}),i!==void 0&&c.append("skip",${i});let d=https://${o}/api/pages/${e}?${c.toString()},p=await n(d,r?{headers:{Cookie:E(r)}}:void 0);if(!p.ok){let g=await p.text(),k=v(g);if(!k)throw new x({path:new URL(d),...p,body:g});return{ok:!1,value:k}}let h=await p.json();return{ok:!0,value:h}};var Oe=async(e,t)=>{let{sid:r,hostName:o,fetch:n}=b(t??{}),s=https://${o}/api/projects/${e},a=await n(s,r?{headers:{Cookie:E(r)}}:void 0);if(!a.ok){let c=await a.text(),d=v(c);if(!d)throw new x({path:new URL(s),...a,body:c});return{ok:!1,value:d}}let i=await a.json();return{ok:!0,value:i}};var Se=(e,t)=>{if(!(e instanceof HTMLDivElement))throw new TypeError("${t}" must be HTMLDivElememt but actual is "${e}")};var Ne=()=>at(document.getElementsByClassName("status-bar")?.[0],"div.status-bar"),at=(e,t)=>{if(!!e)return Se(e,t),e};var q=e=>new Promise(t=>setTimeout(()=>t(),e));var ue=()=>{let e=Ne();if(!e)throw new Error("div.status-bar can't be found");let t=document.createElement("div");return e.append(t),{render:(...r)=>{t.textContent="";let o=Ce(...r);o&&t.append(o)},dispose:()=>t.remove()}},Ce=(...e)=>{let t=e.flatMap(o=>{switch(o.type){case"spinner":return[lt()];case"check-circle":return[mt()];case"exclamation-triangle":return[ft()];case"text":return[K(o.text)];case"group":{let n=Ce(...o.items);return n?[n]:[]}}});if(t.length===0)return;if(t.length===1)return t[0];let r=document.createElement("span");return r.classList.add("item-group"),r.append(...t),r},K=e=>{let t=document.createElement("span");return t.classList.add("item"),t.append(e),t},lt=()=>{let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),K(e)},mt=()=>{let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),K(e)},ft=()=>{let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),K(e)};var ht="4.2.0";async function D(){let t=(await xt())("https://scrapbox.io",{reconnectionDelay:5e3,transports:["websocket"]});return await new Promise((r,o)=>{let n=s=>o(s);t.once("connect",()=>{t.off("disconnect",n),r()}),t.once("disconnect",n)}),t}function xt(){let e=https://cdnjs.cloudflare.com/ajax/libs/socket.io/${ht}/socket.io.min.js;if(document.querySelector(scriptsrc="${e}"))return Promise.resolve(window.io);let t=document.createElement("script");return t.src=e,new Promise((r,o)=>{t.onload=()=>r(window.io),t.onerror=n=>o(n),document.head.append(t)})}function P(e,t=9e4){function r(n,s){let a;return new Promise((i,c)=>{let d=p=>{clearTimeout(a),c(new Error(p))};e.emit(n,s,p=>{clearTimeout(a),e.off("disconnect",d),p.error&&c(new Error(JSON.stringify(p.error))),"data"in p?i(p?.data):i(void 0)}),a=setTimeout(()=>{e.off("disconnect",d),c(new Error(Timeout: exceeded ${t}ms))},t),e.once("disconnect",d)})}async function*o(...n){let s,a=()=>new Promise(c=>s=c),i=c=>{s?.(c)};for(let c of n)e.on(c,i);try{for(;;)yield await a()}finally{for(let c of n)e.off(c,i)}}return{request:r,response:o}}var le=()=>D(),U=async e=>{if(e.connected)return;let t=new Promise(r=>e.once("connect",()=>r()));e.connect(),await t},O=async e=>{if(e.disconnected)return;let t=new Promise(r=>{let o=n=>{n==="io client disconnect"&&(r(),e.off("disconnect",o))};e.on("disconnect",o)});e.disconnect(),await t};var z,j=async()=>{if(z!==void 0)return z;let e=await ie();if(e.isGuest)throw new Error("this script can only be executed by Logged in users");return z=e.id,z},Pe=new Map,$=async e=>{let t=Pe.get(e);if(t!==void 0)return t;let r=await Oe(e);if(!r.ok){let{name:n,message:s}=r.value;throw new Error(${n} ${s})}let{id:o}=r.value;return Pe.set(e,o),o},Re=e=>e.padStart(8,"0"),Ae=e=>{let t=Math.floor(new Date().getTime()/1e3).toString(16),r=Math.floor(16777214*Math.random()).toString(16);return${Re(t).slice(-8)}${e.slice(-6)}0000${Re(r)}};var Ue=(e,t)=>{let r=e.length>t.length,o=r?t:e,n=r?e:t,s=o.length+1,a=o.length+n.length+3,i=new Array(a);i.fill(-1);let c=[];function d(m,I,C){let L=Math.max(I,C),Y=L-m;for(;Y<o.length&&L<n.length&&o[Y]===n[L];)++Y,++L;return i[m+s]=c.length,c.push([{x:Y,y:L},i[m+(I>C?-1:1)+s]]),L}let p=new Array(a);p.fill(-1);let h=-1,g=n.length-o.length;do{++h;for(let m=-h;m<=g-1;++m)p[m+s]=d(m,p[m-1+s]+1,p[m+1+s]);for(let m=g+h;m>=g+1;--m)p[m+s]=d(m,p[m-1+s]+1,p[m+1+s]);p[g+s]=d(g,p[g-1+s]+1,p[g+1+s])}while(p[g+s]!==n.length);let k=[],T=i[g+s];for(;T!==-1;)k.push(c[T][0]),T=c[T][1];return{from:e,to:t,editDistance:g+h*2,buildSES:function*(){let m=0,I=0;for(let{x:C,y:L}of wt(k))for(;m<C||I<L;)L-C>I-m?(yield{value:n[I],type:r?"deleted":"added"},++I):L-C<I-m?(yield{value:o[m],type:r?"added":"deleted"},++m):(yield{value:o[m],type:"common"},++m,++I)}}};function*$e(e){let t=[],r=[];function*o(){if(t.length>r.length){for(let n=0;n<r.length;n++)yield He(t[n],r[n]);for(let n=r.length;n<t.length;n++)yield t[n]}else{for(let n=0;n<t.length;n++)yield He(t[n],r[n]);for(let n=t.length;n<r.length;n++)yield r[n]}t=[],r=[]}for(let n of e)switch(n.type){case"added":t.push(n);break;case"deleted":r.push(n);break;case"common":yield*o(),yield n;break}yield*o()}var He=(e,t)=>({value:e.value,oldValue:t.value,type:"replaced"});function*wt(e){for(let t=e.length-1;t>=0;t--)yield e[t]}function*je(e,t,{userId:r}){let{buildSES:o}=Ue(e.map(({text:a})=>a),t),n=0,s=e[0].id;for(let a of $e(o())){switch(a.type){case"added":yield{_insert:s,lines:{id:Ae(r),text:a.value}};continue;case"deleted":yield{_delete:s,lines:-1};break;case"replaced":yield{_update:s,lines:{text:a.value}};break}n++,s=e[n]?.id??"_end"}}var yt=e=>({type:"title",text:e.rows[0].text}),bt=e=>{let{rows:[t,...r]}=e,{indent:o=0,text:n=""}=t??{},s=n.replace(/^\s*code:/,"");return{indent:o,type:"codeBlock",fileName:s,content:r.map(a=>a.text.substring(o+1)).join( )}},w=(e,{parseOnNested:t,parseOnQuoted:r,patterns:o})=>(n,s,a)=>{var i,c,d,p,h,g;if(!t&&s.nested)return(i=a?.())!==null&&i!==void 0?i:[];if(!r&&s.quoted)return(c=a?.())!==null&&c!==void 0?c:[];for(let k of o){let T=k.exec(n);if(T===null)continue;let m=n.substring(0,T.index),I=n.substring(T.index+((p=(d=T[0])===null||d===void 0?void 0:d.length)!==null&&p!==void 0?p:0)),C=e((h=T[0])!==null&&h!==void 0?h:"",s);return[...R(m,s),...C,...R(I,s)]}return(g=a?.())!==null&&g!==void 0?g:[]},y=e=>[{type:"plain",raw:e,text:e}],Et=w(y,{parseOnNested:!0,parseOnQuoted:!0,patterns:[/^()(.*)()$/]}),kt=/^>.*$/,vt=(e,t)=>t.context==="table"?y(e,t):[{type:"quote",raw:e,nodes:R(e.substring(1),{...t,quoted:!0})}],Tt=w(vt,{parseOnNested:!1,parseOnQuoted:!1,patterns:[kt]}),It=/^\? .+$/,Lt=(e,t)=>t.context==="table"?y(e,t):[{type:"helpfeel",raw:e,text:e.substring(2)}],Dt=w(Lt,{parseOnNested:!1,parseOnQuoted:!1,patterns:[It]}),Ot=/\[\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)\]\]/i,St=/\[\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}\]\]/,Nt=(e,t)=>{if(t.context==="table")return y(e,t);let r=e.substring(2,e.length-2),o=/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(r);return[{type:"strongImage",raw:e,src:o?${r}/thumb/1000:r}]},Mt=w(Nt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Ot,St]}),Ct=/\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;function Fe(e){return(t,r)=>{if(e==="strongIcon"&&r.context==="table")return y(t,r);let o=e==="icon"?t.substring(1,t.length-1):t.substring(2,t.length-2),n=o.lastIndexOf(".icon"),s=o.substring(0,n),a=s.startsWith("/")?"root":"relative",i=o.substring(n+5,o.length),c=i.startsWith("*")?parseInt(i.substring(1),10):1;return new Array(c).fill({}).map(()=>({path:s,pathType:a,type:e,raw:t}))}}var Pt=Fe("icon"),Rt=w(Pt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Ct]}),At=/\[\[[^[\]]*\.icon(?:\*\d+)?\]\]/,Ut=Fe("strongIcon"),$t=w(Ut,{parseOnNested:!1,parseOnQuoted:!0,patterns:[At]}),Ht=/\[\[(?:[^[]|\[[^[]).*?\]*\]\]/,jt=(e,t)=>t.context==="table"?y(e,t):[{type:"strong",raw:e,nodes:R(e.substring(2,e.length-2),{...t,nested:!0})}],Ft=w(jt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Ht]}),_t=/\[\$ .+? \]/,Bt=/\[\$ [^\]]+\]/,qt=(e,t)=>t.context==="table"?y(e,t):[{type:"formula",raw:e,formula:e.substring(3,e.length-(e.endsWith(" ]")?2:1))}],Wt=w(qt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[_t,Bt]}),Yt=/\[[!"#%&'()*+,\-./{|}<>_~]+ (?:\[[^[\]]+\]|[^\]])+\]/,Kt=(e,t)=>{if(t.context==="table")return y(e,t);let r=e.indexOf(" "),o=e.substring(1,r),n=e.substring(r+1,e.length-1),s=new Set(o);if(s.has("*")){let a=o.split("*").length-1;s.delete("*"),s.add(*-${Math.min(a,10)})}return[{type:"decoration",raw:e,rawDecos:o,decos:Array.from(s),nodes:R(n,{...t,nested:!0})}]},zt=w(Kt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Yt]}),Gt=/.*?/,Qt=(e,t)=>t.context==="table"?y(e,t):[{type:"code",raw:e,text:e.substring(1,e.length-1)}],Xt=w(Qt,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Gt]}),Vt=/^[$%] .+$/,Jt=(e,t)=>{var r;if(t.context==="table")return y(e,t);let o=(r=e[0])!==null&&r!==void 0?r:"",n=e.substring(2);return[{type:"commandLine",raw:e,symbol:o,text:n}]},Zt=w(Jt,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Vt]}),er=/\[\s+\]/,tr=(e,t)=>t.context==="table"?y(e,t):[{type:"blank",raw:e,text:e.substring(1,e.length-1)}],rr=w(tr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[er]}),or=/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i,nr=/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i,sr=/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/,ir=/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/,ar=e=>/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(e)||cr(e),cr=e=>/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}(\/raw)?$/.test(e),dr=(e,t)=>{if(t.context==="table")return y(e,t);let r=e.search(/\s/),o=r!==-1?e.substring(1,r):e.substring(1,e.length-1),n=r!==-1?e.substring(r,e.length-1).trimLeft():"",[s,a]=ar(n)?[n,o]:[o,n];return[{type:"image",raw:e,src:/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(s)?${s}/thumb/1000:s,link:a}]},pr=w(dr,{parseOnNested:!0,parseOnQuoted:!0,patterns:[or,nr,sr,ir]}),ur=/\[https?:\/\/[^\s\]]+\s+[^\]]*[^\s]\]/,lr=/\[[^[\]]*[^\s]\s+https?:\/\/[^\s\]]+\]/,mr=/\[https?:\/\/[^\s\]]+\]/,fr=/https?:\/\/[^\s]+/,gr=(e,t)=>{if(t.context==="table")return y(e,t);let r=e.startsWith("[")&&e.endsWith("]")?e.substring(1,e.length-1):e,o=/^https?:\/\/[^\s\]]/.test(r),n=(o?/^https?:\/\/[^\s\]]+/:/https?:\/\/[^\s\]]+$/).exec(r);if(n?.[0]===void 0)return[];let s=o?r.substring(n[0].length):r.substring(0,n.index-1);return[{type:"link",raw:e,pathType:"absolute",href:n[0],content:s.trim()}]},hr=w(gr,{parseOnNested:!0,parseOnQuoted:!0,patterns:[ur,lr,mr,fr]}),_e=/\[([^\]]*[^\s])\s+([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)\]/,Be=/\[([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)(?:\s+([^\]]*[^\s]))?\]/,xr=e=>{let[t="",r="",o=""]=e.split(","),n=parseFloat(t.replace(/^N/,"").replace(/^S/,"-")),s=parseFloat(r.replace(/^E/,"").replace(/^W/,"-")),a=/^Z\d+$/.test(o)?parseInt(o.replace(/^Z/,""),10):14;return{latitude:n,longitude:s,zoom:a}},wr=(e,t)=>{var r;if(t.context==="table")return y(e,t);let o=(r=e.match(_e))!==null&&r!==void 0?r:e.match(Be);if(o===null)return[];let n=e.startsWith("[N")||e.startsWith("[S"),[,s="",a=""]=n?o:[o[0],o[2],o[1]],{latitude:i,longitude:c,zoom:d}=xr(s),p=a!==""?https://www.google.com/maps/place/${encodeURIComponent(a)}/@${i},${c},${d}z:https://www.google.com/maps/@${i},${c},${d}z;return[{type:"googleMap",raw:e,latitude:i,longitude:c,zoom:d,place:a,url:p}]},yr=w(wr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[_e,Be]}),br=/\[\/?[^[\]]+\]/,Er=e=>{let t=e.substring(1,e.length-1);return[{type:"link",raw:e,pathType:t.startsWith("/")?"root":"relative",href:t,content:""}]},kr=w(Er,{parseOnNested:!0,parseOnQuoted:!0,patterns:[br]}),vr=/(?:^|\s)#\S+/,Tr=(e,t)=>{if(t.context==="table")return y(e,t);if(e.startsWith("#"))return[{type:"hashTag",raw:e,href:e.substring(1)}];let r=e.substring(0,1),o=e.substring(1);return[...y(r,t),{type:"hashTag",raw:o,href:o.substring(1)}]},Ir=w(Tr,{parseOnNested:!0,parseOnQuoted:!0,patterns:[vr]}),Lr=/^[0-9]+\. .*$/,Dr=(e,t)=>{if(t.context==="table")return y(e,t);let r=e.indexOf(" "),o=e.substring(0,r-1),n=parseInt(o,10),s=e.substring(r+1,e.length);return[{type:"numberList",raw:e,rawNumber:o,number:n,nodes:R(s,{...t,nested:!0})}]},Or=w(Dr,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Lr]}),Sr=(e,t,r)=>{var o;return e===""?[]:(o=r?.())!==null&&o!==void 0?o:[]},Nr=(...e)=>(t,r)=>e.reduceRight((o,n)=>()=>n(t,r,o),()=>Et(t,r))(),R=Nr(Sr,Tt,Dt,Xt,Zt,Wt,rr,zt,Mt,$t,Ft,pr,hr,Rt,yr,kr,Ir,Or),Mr=e=>{let{rows:[t,...r]}=e,{indent:o=0,text:n=""}=t??{},s=n.replace(/^\s*table:/,"");return{indent:o,type:"table",fileName:s,cells:r.map(a=>a.text.substring(o+1)).map(a=>a.split(" ").map(i=>R(i,{nested:!1,quoted:!1,context:"table"})))}},Cr=e=>{let{indent:t,text:r}=e.rows[0];return{indent:t,type:"line",nodes:R(r.substring(t),{nested:!1,quoted:!1,context:"line"})}},qe=e=>{switch(e.type){case"title":return yt(e);case"codeBlock":return bt(e);case"table":return Mr(e);case"line":return Cr(e)}},We=e=>e.split( ).map(t=>{var r,o,n;return{indent:(n=(o=(r=/^\s+/.exec(t))===null||r===void 0?void 0:r[0])===null||o===void 0?void 0:o.length)!==null&&n!==void 0?n:0,text:t}}),Pr=(e,t)=>{var r,o;return(e.type==="codeBlock"||e.type==="table")&&t.indent>((o=(r=e.rows[0])===null||r===void 0?void 0:r.indent)!==null&&o!==void 0?o:0)},Ye=(e,t)=>{let r=e[e.length-1];return r!==void 0&&Pr(r,t)?(r.rows.push(t),e):(e.push({type:/^\s*code:/.test(t.text)?"codeBlock":/^\s*table:/.test(t.text)?"table":"line",rows:[t]}),e)},Ke=(e,t)=>{var r;if(!((r=t.hasTitle)!==null&&r!==void 0)||r){let[o,...n]=e;return o===void 0?[]:[{type:"title",rows:[o]},...n.reduce(Ye,[])]}return e.reduce(Ye,[])};var Rr=/https?:\/\/(?:www\.|)youtube\.com\/watch\?((?:[^\s]+&|)v=([a-zA-Z\d_-]+)(?:&[^\s]+|))/,Ar=/https?:\/\/youtu\.be\/([a-zA-Z\d_-]+)(?:\?([^\s]{0,100})|)/,Ur=/https?:\/\/(?:www\.|)youtube\.com\/playlist\?((?:[^\s]+&|)list=([a-zA-Z\d_-]+)(?:&[^\s]+|))/,ze=e=>{{let t=e.match(Rr);if(t){let[,r,o]=t,n=new URLSearchParams(r);return n.delete("v"),n.append("autoplay","0"),{videoId:o,params:n}}}{let t=e.match(Ar);if(t){let[,r]=t;return{videoId:r,params:new URLSearchParams("autoplay=0")}}}{let t=e.match(Ur);if(t){let[,r,o]=t,n=new URLSearchParams(r),s=n.get("v");return s?(n.delete("v"),n.append("autoplay","0"),n.append("list",o),{videoId:s,params:n}):void 0}}};function*Ge(e,t,{userId:r,head:o}){let n=t.flatMap(d=>d.split(
));for(let d of je(e,n,{userId:r}))yield d;(e[0].text!==n[0]||!o.persistent)&&(yield{title:n[0]});let s=e.slice(1,6).map(d=>d.text),a=n.slice(1,6);s.join("")!==a.join("")&&(yield{descriptions:a});let[i,c]=$r(n.join(
));(o.links.length!==i.length||!o.links.every(d=>i.includes(d)))&&(yield{links:i}),o.image!==c&&(yield{image:c})}var $r=e=>{let t=We(e),r=Ke(t,{hasTitle:!0}).flatMap(i=>{switch(i.type){case"codeBlock":case"title":return[];case"line":case"table":return[qe(i)]}}),o=new Map,n=[],s=null,a=i=>{switch(i.type){case"hashTag":if(o.has(B(i.href)))return;o.set(B(i.href),!1),n.push(i.href);return;case"link":switch(i.pathType){case"relative":if(o.get(B(i.href)))return;o.set(B(i.href),!0),n.push(i.href);return;case"absolute":{let c=ze(i.href);return c?https://i.ytimg.com/vi/${c.videoId}/mqdefault.jpg:void 0}default:return}case"image":case"strongImage":{s??=i.src.endsWith("/thumb/1000")?i.src.replace(/\/thumb\/1000$/,"/raw"):i.src;return}case"strong":case"quote":case"decoration":{for(let c of i.nodes)a(c);return}default:return}};for(let i of Hr(r))a(i);return[n,s]};function*Hr(e){for(let t of e)switch(t.type){case"codeBlock":case"title":continue;case"line":for(let r of t.nodes)yield r;continue;case"table":{for(let r of t.cells)for(let o of r)for(let n of o)yield n;continue}}}var N=async(e,t)=>{let r=await De(e,t);if(!r.ok)throw new Error(You have no privilege of editing "/${e}/${t}".);let{commitId:o,persistent:n,image:s,links:a,lines:i,id:c,pin:d}=r.value;return{commitId:o,pageId:c,persistent:n,image:s,links:a,pin:d,lines:i}};var G=async(e,t,r)=>t.length===0?{commitId:r.parentId}:await e("socket.io-request",{method:"commit",data:{kind:"page",...r,changes:t,cursor:null,freeze:!0}}),H=async(e,t,{project:r,title:o,retry:n=3,parentId:s,...a})=>{try{s=(await G(e,t,{parentId:s,...a})).commitId}catch{console.log("Faild to push a commit. Retry after pulling new commits");for(let c=0;c<n;c++){let{commitId:d}=await N(r,o);s=d;try{s=(await G(e,t,{parentId:s,...a})).commitId,console.log("Success in retrying");break}catch{continue}}throw Error("Faild to retry pushing.")}return s};var me=async(e,t,r,o)=>{let[n,s,a]=await Promise.all([N(e,t),$(e),j()]),i=n,c=o?.socket,d=c??await D();await U(d);try{let{request:p}=P(d);for(let h=0;h<3;h++)try{let g=r(i.lines,i),k=g instanceof Promise?await g:g;if(!k)return;k.length===0&&await H(p,[{deleted:!0}],{projectId:s,pageId:i.pageId,parentId:i.commitId,userId:a,project:e,title:t});let T=[...Ge(i.lines,k,{userId:a,head:i})];await G(p,T,{parentId:i.commitId,projectId:s,pageId:i.pageId,userId:a});break}catch{if(h===2)throw Error("Faild to retry pushing.");console.log("Faild to push a commit. Retry after pulling new commits");try{i=await N(e,t)}catch(k){throw k}}}finally{c||await O(d)}};var fe=async(e,t,r)=>{let[o,n,s]=await Promise.all([N(e,t),$(e),j()]);if(o.pin>0||!o.persistent&&!(r?.create??!1))return;let a={parentId:o.commitId,projectId:n,pageId:o.pageId,userId:s,project:e,title:t},i=r?.socket,c=i??await D();await U(c);let{request:d}=P(c);if(!o.persistent){let p=await H(d,[{title:t}],a);a.parentId=p}try{await H(d,[{pin:jr()}],a)}finally{i||await O(c)}},ge=async(e,t,r)=>{let[o,n,s]=await Promise.all([N(e,t),$(e),j()]);if(o.pin==0||!o.persistent)return;let a={parentId:o.commitId,projectId:n,pageId:o.pageId,userId:s,project:e,title:t},i=r?.socket,c=i??await D();await U(c);let{request:d}=P(c);try{await H(d,[{pin:0}],a)}finally{i||await O(c)}},jr=()=>Number.MAX_SAFE_INTEGER-Math.floor(Date.now()/1e3);async function*he(e,t=0){let{count:r,pages:o}=await Fr(e,t);for(let n of o)n.pin!==0&&(yield n);(o.at(-1)?.pin??0)!==0&&(yield*he(e,t+1e3))}async function Fr(e,t){let r=await ce(e,{limit:1e3,skip:t});if(!r.ok){let o=new Error;throw o.name=r.value.name,o.message=r.value.message,o}return r.value}function Qe(e,t){let r=t.interval??24*3600*1e3,o=()=>scrapbox.Project.name===e?_r(e,r,t):Ve();o(),scrapbox.addListener("project:changed",o)}var Xe;async function _r(e,t,r){Ve(),await Je(e,new Date,r),Xe=setInterval(()=>Je(e,new Date,r),t)}function Ve(){clearInterval(Xe)}async function Je(e,t,{makeDiary:r,isOldDiary:o}){let{render:n,dispose:s}=ue(),a;try{n({type:"spinner"},{type:"text",text:"unpin other diary pages..."}),a=await le();for await(let{title:p}of he(e))!o(p,t)||await ge(e,p,{socket:a});let{title:i,header:c,footer:d}=r(t);n({type:"spinner"},{type:"text",text:pin "/${e}/${i}"...}),await fe(e,i,{socket:a,create:!0}),n({type:"spinner"},{type:"text",text:format "/${e}/${i}"...}),await me(e,i,p=>[p[0].text,...se(p.slice(1).map(h=>h.text),c,d)],{socket:a}),n({type:"check-circle"},{type:"text",text:Pinned "/${e}/${i}".})}catch(i){n({type:"exclamation-triangle"},{type:"text",text:i instanceof Error?${i.name} ${i.message}:"Unknown error! (see developper console)"}),console.error(i)}finally{a&&await O(a),await q(1e3),s()}}function u(e,t){if(t.length<e)throw new TypeError(e+" argument"+(e>1?"s":"")+" required, but only "+t.length+" present")}function l(e){u(1,arguments);let t=Object.prototype.toString.call(e);return e instanceof Date||typeof e=="object"&&t==="[object Date]"?new Date(e.getTime()):typeof e=="number"||t==="[object Number]"?new Date(e):((typeof e=="string"||t==="[object String]")&&typeof console!="undefined"&&(console.warn("Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use parseISO to parse strings. See: https://git.io/fjule"),console.warn(new Error().stack)),new Date(NaN))}function xe(e){u(1,arguments);let r=l(e).getFullYear();return r%400==0||r%4==0&&r%100!=0}function Q(e){u(1,arguments);let t=l(e);return String(new Date(t))==="Invalid Date"?NaN:xe(t)?366:365}function we(e){u(1,arguments);let t=l(e),r=new Date(0);return r.setFullYear(t.getFullYear(),0,1),r.setHours(0,0,0,0),r}function F(e){let t=new Date(Date.UTC(e.getFullYear(),e.getMonth(),e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),e.getMilliseconds()));return t.setUTCFullYear(e.getFullYear()),e.getTime()-t.getTime()}function X(e){u(1,arguments);let t=l(e);return t.setHours(0,0,0,0),t}var Br=864e5;function ye(e,t){u(2,arguments);let r=X(e),o=X(t),n=r.getTime()-F(r),s=o.getTime()-F(o);return Math.round((n-s)/Br)}function V(e){u(1,arguments);let t=l(e);return ye(t,we(t))+1}function f(e){if(e===null||e===!0||e===!1)return NaN;let t=Number(e);return isNaN(t)?t:t<0?Math.ceil(t):Math.floor(t)}function _(e,t){u(2,arguments);let r=l(e),o=f(t);return isNaN(o)?new Date(NaN):(o&&r.setDate(r.getDate()+o),r)}function J(e,t){u(2,arguments);let r=f(t);return _(e,-r)}function A(e,t){u(1,arguments);let r=t||{},o=r.locale,n=o&&o.options&&o.options.weekStartsOn,s=n==null?0:f(n),a=r.weekStartsOn==null?s:f(r.weekStartsOn);if(!(a>=0&&a<=6))throw new RangeError("weekStartsOn must be between 0 and 6 inclusively");let i=l(e),c=i.getDay(),d=(c<a?7:0)+c-a;return i.setDate(i.getDate()-d),i.setHours(0,0,0,0),i}function be(e,t){u(1,arguments);let r=l(e),o=r.getFullYear(),n=t?.locale?.options?.firstWeekContainsDate,s=n==null?1:f(n),a=t?.firstWeekContainsDate==null?s:f(t.firstWeekContainsDate);if(!(a>=1&&a<=7))throw new RangeError("firstWeekContainsDate must be between 1 and 7 inclusively");let i=new Date(0);i.setFullYear(o+1,0,a),i.setHours(0,0,0,0);let c=A(i,t),d=new Date(0);d.setFullYear(o,0,a),d.setHours(0,0,0,0);let p=A(d,t);return r.getTime()>=c.getTime()?o+1:r.getTime()>=p.getTime()?o:o-1}function Ee(e,t){u(1,arguments);var r=t||{},o=r.locale,n=o&&o.options&&o.options.firstWeekContainsDate,s=n==null?1:f(n),a=r.firstWeekContainsDate==null?s:f(r.firstWeekContainsDate),i=be(e,t),c=new Date(0);c.setFullYear(i,0,a),c.setHours(0,0,0,0);var d=A(c,t);return d}var qr=6048e5;function Z(e,t){u(1,arguments);let r=l(e),o=A(r,t).getTime()-Ee(r,t).getTime();return Math.round(o/qr)+1}function ke(e,t){u(2,arguments);let r=l(e),o=f(t);if(isNaN(o))return new Date(NaN);if(!o)return r;let n=r.getDate(),s=new Date(r.getTime());s.setMonth(r.getMonth()+o+1,0);let a=s.getDate();return n>=a?s:(r.setFullYear(s.getFullYear(),s.getMonth(),n),r)}function ve(e,t){u(2,arguments);let r=f(t);return ke(e,r*12)}function ee(e,t){u(2,arguments);let r=f(t);return ve(e,-r)}function te(e){return u(1,arguments),l(e).getDay()}function re(e){return u(1,arguments),l(e).getFullYear()}function S(e,t){for(var r=e<0?"-":"",o=Math.abs(e).toString();o.length<t;)o="0"+o;return r+o}var Wr={y(e,t){let r=e.getUTCFullYear(),o=r>0?r:1-r;return S(t==="yy"?o%100:o,t.length)},M(e,t){let r=e.getUTCMonth();return t==="M"?String(r+1):S(r+1,2)},d(e,t){return S(e.getUTCDate(),t.length)},a(e,t){let r=e.getUTCHours()/12>=1?"pm":"am";switch(t){case"a":case"aa":return r.toUpperCase();case"aaa":return r;case"aaaaa":return r[0];case"aaaa":default:return r==="am"?"a.m.":"p.m."}},h(e,t){return S(e.getUTCHours()%12||12,t.length)},H(e,t){return S(e.getUTCHours(),t.length)},m(e,t){return S(e.getUTCMinutes(),t.length)},s(e,t){return S(e.getUTCSeconds(),t.length)},S(e,t){let r=t.length,o=e.getUTCMilliseconds(),n=Math.floor(o*Math.pow(10,r-3));return S(n,t.length)}},Ze=Wr;function Te(e){u(1,arguments);var t=l(e);return!isNaN(t)}function Ie(e,t){u(2,arguments);let r=l(e).getTime(),o=f(t);return new Date(r+o)}function Le(e,t){u(2,arguments);let r=f(t);return Ie(e,-r)}var Yr=/(\w)\1*|''|'(''|[^'])+('|$)|./g,Kr=/^'([^]*?)'?$/,zr=/''/g,Gr=/[a-zA-Z]/;function M(e,t){u(2,arguments);let r=l(e);if(!Te(r))throw new RangeError("Invalid time value");let o=F(r),n=Le(r,o),s=t.match(Yr);return s?s.map(i=>{if(i==="''")return"'";let c=i[0];if(c==="'")return Qr(i);let d=Ze[c];if(d)return d(n,i);if(c.match(Gr))throw new RangeError("Format string contains an unescaped latin alphabet character "+c+"");return i}).join(""):""}function Qr(e){let t=e.match(Kr);return t?t[1].replace(zr,"'"):e}var W="yyyy/MM/dd";function et(e){return{title:rt(e),header: [第${Z(e)}週: ${Xr(e)}, ${re(e)}年 ${(V(e) * 100 / Q(e)).toFixed(2)}%経過, "", "今日のn年前", ${M(ee(e, 1), W)},""],footer:[${M(J(e,1),W)}←${M(e,W)}→${M(_(e,1),W)},/villagepump/${M(e,"yyyy/MM")}.icon]}}function Xr(e){let t=te(e);return[..."日月火水木金土"].map((r,o)=>o===t?${r}`:r).join("")}var Vr=/^\d{4}\/\d{2}\/\d{2}$/;function tt(e,t){return Vr.test(e)?rt(t)!==e:!1}function rt(e){return M(e,W)}Qe("yuyasurarin",{makeDiary:et,isOldDiary:tt});