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