pin-diary
code:script.js
var de=(e,t)=>{let r=0,n=[];for(let o=0;o<t.length;o++){let s=e.findIndex((i,a)=>a>=r&&i.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},me=(e,t)=>{let r=-1;for(let n of t){let o=e.findIndex((s,i)=>i>r&&s.trim()===n.trim());if(o<0)return-1;r=o}return r};var fe=(e,t,r)=>{let n=de(de(e,t).reverse(),...r.reverse()).reverse(),o=me(n,t),s=n.length-1-me(...n.reverse(),...r.reverse());return[...n.slice(0,o+1),"",...n.slice(o+1,s).join(` ).trim().split(
),"",...n.slice(s)]};var y=e=>{let{fetch:t=globalThis.fetch,hostName:r="scrapbox.io",...n}=e;return{fetch:t,hostName:r,...n}};var Yt=e=>typeof e=="object"&&e!==null,Kt=e=>Yt(e)?(e.name===void 0||typeof e.name=="string")&&typeof e.message=="string":!1,Ve=e=>{try{let t=typeof e=="string"?JSON.parse(e):e;return Kt(t)?t:!1}catch(t){if(t instanceof SyntaxError)return!1;throw t}};var U=class e extends Error{constructor(r){super(${r.status} ${r.statusText} when fetching ${r.url});this.response=r;Error.captureStackTrace&&Error.captureStackTrace(this,e)}name="UnexpectedResponseError"},w=async e=>{let t=e.clone(),r=await t.text(),n=Ve(r);if(!n)throw new U(t);return{ok:!1,value:n}};var ge=async e=>{let{sid:t,hostName:r,fetch:n}=y(e??{}),o=new Request(https://${r}/api/users/me,t?{headers:{Cookie:b(t)}}:void 0),s=await n(o);if(!s.ok)throw new U(s);return await s.json()};var b=e=>connect.sid=${e};var C=e=>e.replaceAll(" ","_").toLowerCase();var P=e=>[...e].map((t,r)=>t===" "?"_":!Wt.includes(t)||r===e.length-1&&Gt.includes(t)?encodeURIComponent(t):t).join(""),Wt='@$&+=:;",',Gt=':;",';var Je=(e,t,r)=>{let{sid:n,hostName:o,followRename:s,projects:i}=y(r??{}),a=new URLSearchParams;a.append("followRename",${s??!0});for(let m of i??[])a.append("projects",m);let u=https://${o}/api/pages/${e}/${P(t)}?${a.toString()};return new Request(u,n?{headers:{Cookie:b(n)}}:void 0)},Xe=async e=>e.ok?{ok:!0,value:await e.json()}:e.status===414?{ok:!1,value:{name:"TooLongURIError",message:"project ids may be too much."}}:w(e),X=async(e,t,r)=>{let{fetch:n}=y(r??{}),o=Je(e,t,r),s=await n(o);return await Xe(s)};X.toRequest=Je;X.fromResponse=Xe;var Ze=(e,t)=>{let{sid:r,hostName:n,sort:o,limit:s,skip:i}=y(t??{}),a=new URLSearchParams;o!==void 0&&a.append("sort",o),s!==void 0&&a.append("limit",${s}),i!==void 0&&a.append("skip",${i});let u=https://${n}/api/pages/${e}?${a.toString()};return new Request(u,r?{headers:{Cookie:b(r)}}:void 0)},et=async e=>e.ok?{ok:!0,value:await e.json()}:w(e),W=async(e,t)=>{let{fetch:r}=y(t??{}),n=await r(Ze(e,t));return await et(n)};W.toRequest=Ze;W.fromResponse=et;var tt=(e,t,r,n)=>{let{sid:o,hostName:s}=y(n??{}),i=https://${s}/api/table/${e}/${P(t)}/${encodeURIComponent(r)}.csv;return new Request(i,o?{headers:{Cookie:b(o)}}:void 0)},rt=async e=>e.ok?{ok:!0,value:await e.text()}:e.status===404?{ok:!1,value:{name:"NotFoundError",message:"Table not found."}}:w(e),nt=async(e,t,r,n)=>{let{fetch:o}=y(n??{}),s=tt(e,t,r,n),i=await o(s);return await rt(i)};nt.toRequest=tt;nt.fromResponse=rt;var ot=(e,t)=>{let{sid:r,hostName:n}=y(t??{});return new Request(https://${n}/api/projects/${e},r?{headers:{Cookie:b(r)}}:void 0)},st=async e=>e.ok?{ok:!0,value:await e.json()}:w(e),Z=async(e,t)=>{let{fetch:r}=y(t??{}),n=ot(e,t),o=await r(n);return st(o)};Z.toRequest=ot;Z.fromResponse=st;var it=(e,t)=>{let{sid:r,hostName:n}=y(t??{}),o=new URLSearchParams;for(let s of e)o.append("ids",s);return new Request(https://${n}/api/projects?${o.toString()},r?{headers:{Cookie:b(r)}}:void 0)},at=async e=>e.ok?{ok:!0,value:await e.json()}:w(e),ct=async(e,t)=>{let{fetch:r}=y(t??{}),n=await r(it(e,t));return at(n)};ct.toRequest=it;ct.fromResponse=at;var te=async(e,t)=>{let r=await X(e,t);if(!r.ok)throw new Error(You have no privilege of editing "/${e}/${t}".);return r.value};var ut=(e,t,r,n)=>{let{sid:o,hostName:s}=y(n??{}),i=https://${s}/api/code/${e}/${P(t)}/${P(r)};return new Request(i,o?{headers:{Cookie:b(o)}}:void 0)},pt=async e=>e.ok?{ok:!0,value:await e.text()}:e.status===404&&e.headers.get("Content-Type")?.includes?.("text/plain")?{ok:!1,value:{name:"NotFoundError",message:"Code block is not found"}}:w(e),lt=async(e,t,r,n)=>{let{fetch:o}=y(n??{}),s=ut(e,t,r,n),i=await o(s);return await pt(i)};lt.toRequest=ut;lt.fromResponse=pt;var Ys=new TextEncoder().encode("0123456789abcdef");var dt=!1;scrapbox.addListener("lines:changed",()=>dt=!1);scrapbox.addListener("layout:changed",()=>dt=!1);var mt=(e,t)=>{if(!(e instanceof HTMLDivElement))throw new TypeError("${t}" must be HTMLDivElememt but actual is "${e}")};var ft=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError("${t}" must be HTMLTextAreaElement but actual is "${e}")};var $=()=>{let e=document.getElementById("text-input");if(e)return ft(e,"textarea#text-input"),e};var gt=()=>Jt(document.getElementsByClassName("status-bar")?.[0],"div.status-bar"),Jt=(e,t)=>{if(e)return mt(e,t),e};var F=e=>new Promise(t=>setTimeout(()=>t(),e));var he=()=>{let e=gt();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=xt(...r);n&&t.append(n)},dispose:()=>t.remove()}},xt=(...e)=>{let t=e.flatMap(n=>{switch(n.type){case"spinner":return[or()];case"check-circle":return[sr()];case"exclamation-triangle":return[ir()];case"text":return[re(n.text)];case"group":{let o=xt(...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},re=e=>{let t=document.createElement("span");return t.classList.add("item"),t.append(e),t},or=()=>{let e=document.createElement("i");return e.classList.add("fa","fa-spinner"),re(e)},sr=()=>{let e=document.createElement("i");return e.classList.add("kamon","kamon-check-circle"),re(e)},ir=()=>{let e=document.createElement("i");return e.classList.add("fas","fa-exclamation-triangle"),re(e)};var yt=e=>{if(e===0)return;let t={};return e&1&&(t.capture=!0),e&2&&(t.once=!0),e&4&&(t.passive=!0),t};var cr=new Map;scrapbox.on("layout:changed",()=>{let e=$();if(e)for(let[t,r]of cr)for(let[n,o]of r)for(let s of o)e.addEventListener(t,n,yt(s))});var Et=e=>ur.includes(e.name),ur=["SocketIOError","DuplicateTitleError","NotFastForwardError"];var G=async()=>{let t=(await lr())("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},pr="4.2.0",bt=https://cdnjs.cloudflare.com/ajax/libs/socket.io/${pr}/socket.io.min.js,xe,lr=async()=>{if(xe)throw xe;if(!document.querySelector(scriptsrc="${bt}")){let e=document.createElement("script");e.src=bt,await new Promise((t,r)=>{e.onload=()=>t(),e.onerror=n=>{xe=n,r(n)},document.head.append(e)})}return new Promise(e=>{let t=setInterval(()=>{io&&(clearInterval(t),e(io))},500)})};var ye=(e,t=9e4)=>{let r=(o,s)=>{let i;return new Promise((a,u)=>{let m=l=>{clearTimeout(i),u(new Error(l))};e.emit(o,s,l=>{switch(clearTimeout(i),e.off("disconnect",m),o){case"socket.io-request":"error"in l?typeof l.error=="object"&&l.error&&"name"in l.error&&typeof l.error.name=="string"&&Et({name:l.error.name})?a({ok:!1,value:l.error}):a({ok:!1,value:{name:"UnexpectedError",value:l.error}}):"data"in l&&a({ok:!0,value:l.data});break;case"cursor":"error"in l?a({ok:!1,value:{name:"UnexpectedError",value:l.error}}):"data"in l&&a({ok:!0,value:l.data});break}u(new Error('Invalid response: missing "data" or "error" field'))}),i=setTimeout(()=>{e.off("disconnect",m),a({ok:!1,value:{name:"TimeoutError",message:Timeout: exceeded ${t}ms}})},t),e.once("disconnect",m)})};async function*n(...o){let s,i=()=>new Promise(u=>s=u),a=u=>{s?.(u)};for(let u of o)e.on(u,a);try{for(;;)yield await i()}finally{for(let u of o)e.off(u,a)}}return{request:r,response:n}};var Ee=()=>G(),be=async e=>{if(e.connected)return;let t=new Promise(r=>e.once("connect",()=>r()));e.connect(),await t},q=async 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 we=(e,t)=>{let r=e.length>t.length,n=r?t:e,o=r?e:t,s=n.length+1,i=n.length+o.length+3,a=new Array(i);a.fill(-1);let u=[];function m(d,E,k){let I=Math.max(E,k),J=I-d;for(;J<n.length&&I<o.length&&n[J]===o[I];)++J,++I;return a[d+s]=u.length,u.push([{x:J,y:I},a[d+(E>k?-1:1)+s]]),I}let l=new Array(i);l.fill(-1);let h=-1,x=o.length-n.length;do{++h;for(let d=-h;d<=x-1;++d)l[d+s]=m(d,l[d-1+s]+1,l[d+1+s]);for(let d=x+h;d>=x+1;--d)l[d+s]=m(d,l[d-1+s]+1,l[d+1+s]);l[x+s]=m(x,l[x-1+s]+1,l[x+1+s])}while(l[x+s]!==o.length);let v=[],f=a[x+s];for(;f!==-1;)v.push(u[f][0]),f=u[f][1];return{from:e,to:t,editDistance:x+h*2,buildSES:function*(){let d=0,E=0;for(let{x:k,y:I}of dr(v))for(;d<k||E<I;)I-k>E-d?(yield{value:o[E],type:r?"deleted":"added"},++E):I-k<E-d?(yield{value:n[d],type:r?"added":"deleted"},++d):(yield{value:n[d],type:"common"},++d,++E)}}};function*ve(e){let t=[],r=[];function*n(){if(t.length>r.length){for(let o=0;o<r.length;o++)yield wt(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 wt(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()}var wt=(e,t)=>({value:e.value,oldValue:t.value,type:"replaced"});function*dr(e){for(let t=e.length-1;t>=0;t--)yield e[t]}var ne,Lt=async()=>{if(ne!==void 0)return ne;let e=await ge();if(e.isGuest)throw new Error("this script can only be executed by Logged in users");return ne=e.id,ne},vt=new Map,ke=async e=>{let t=vt.get(e);if(t!==void 0)return t;let r=await Z(e);if(!r.ok){let{name:o,message:s}=r.value;throw new Error(${o} ${s})}let{id:n}=r.value;return vt.set(e,n),n},kt=e=>e.padStart(8,"0"),Le=e=>{let t=Math.floor(new Date().getTime()/1e3).toString(16),r=Math.floor(16777214*Math.random()).toString(16);return${kt(t).slice(-8)}${e.slice(-6)}0000${kt(r)}};function*Te(e,t,{userId:r}){let{buildSES:n}=we(e.map(({text:i})=>i),t),o=0,s=e[0].id;for(let i of ve(n())){switch(i.type){case"added":yield{_insert:s,lines:{id:Le(r),text:i.value}};continue;case"deleted":yield{_delete:s,lines:-1};break;case"replaced":yield{_update:s,lines:{text:i.value}};break}o++,s=e[o]?.id??"_end"}}var mr=e=>({type:"title",text:e.rows[0].text}),fr=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(i=>i.text.substring(n+1)).join( )}},L=(e,{parseOnNested:t,parseOnQuoted:r,patterns:n})=>(o,s,i)=>{var a,u,m,l,h,x;if(!t&&s.nested)return(a=i?.())!==null&&a!==void 0?a:[];if(!r&&s.quoted)return(u=i?.())!==null&&u!==void 0?u:[];for(let v of n){let f=v.exec(o);if(f===null)continue;let d=o.substring(0,f.index),E=o.substring(f.index+((l=(m=f[0])===null||m===void 0?void 0:m.length)!==null&&l!==void 0?l:0)),k=e((h=f[0])!==null&&h!==void 0?h:"",s);return[...N(d,s),...k,...N(E,s)]}return(x=i?.())!==null&&x!==void 0?x:[]},T=e=>[{type:"plain",raw:e,text:e}],gr=L(T,{parseOnNested:!0,parseOnQuoted:!0,patterns:[/^()(.*)()$/]}),hr=/^>.*$/,xr=(e,t)=>t.context==="table"?T(e,t):[{type:"quote",raw:e,nodes:N(e.substring(1),{...t,quoted:!0})}],yr=L(xr,{parseOnNested:!1,parseOnQuoted:!1,patterns:[hr]}),Er=/^\? .+$/,br=(e,t)=>t.context==="table"?T(e,t):[{type:"helpfeel",raw:e,text:e.substring(2)}],wr=L(br,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Er]}),vr=/\[\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)\]\]/i,kr=/\[\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}\]\]/,Lr=(e,t)=>{if(t.context==="table")return T(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}]},Tr=L(Lr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[vr,kr]}),Ir=/\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;function It(e){return(t,r)=>{if(e==="strongIcon"&&r.context==="table")return T(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),i=s.startsWith("/")?"root":"relative",a=n.substring(o+5,n.length),u=a.startsWith("*")?parseInt(a.substring(1),10):1;return new Array(u).fill({}).map(()=>({path:s,pathType:i,type:e,raw:t}))}}var Cr=It("icon"),Dr=L(Cr,{parseOnNested:!0,parseOnQuoted:!0,patterns:[Ir]}),Rr=/\[\[[^[\]]*\.icon(?:\*\d+)?\]\]/,Mr=It("strongIcon"),Pr=L(Mr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Rr]}),Nr=/\[\[(?:[^[]|\[[^[]).*?\]*\]\]/,Sr=(e,t)=>t.context==="table"?T(e,t):[{type:"strong",raw:e,nodes:N(e.substring(2,e.length-2),{...t,nested:!0})}],Or=L(Sr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Nr]}),Fr=/\[\$ .+? \]/,Ar=/\[\$ [^\]]+\]/,Br=(e,t)=>t.context==="table"?T(e,t):[{type:"formula",raw:e,formula:e.substring(3,e.length-(e.endsWith(" ]")?2:1))}],jr=L(Br,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Fr,Ar]}),Ur=/\[[!"#%&'()*+,\-./{|}<>_~]+ (?:\[[^[\]]+\]|[^\]])+\]/,$r=(e,t)=>{if(t.context==="table")return T(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 i=n.split("*").length-1;s.delete("*"),s.add(*-${Math.min(i,10)})}return[{type:"decoration",raw:e,rawDecos:n,decos:Array.from(s),nodes:N(o,{...t,nested:!0})}]},qr=L($r,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Ur]}),Hr=/.*?/,_r=(e,t)=>t.context==="table"?T(e,t):[{type:"code",raw:e,text:e.substring(1,e.length-1)}],Yr=L(_r,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Hr]}),Kr=/^[$%] .+$/,Wr=(e,t)=>{var r;if(t.context==="table")return T(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}]},Gr=L(Wr,{parseOnNested:!1,parseOnQuoted:!1,patterns:[Kr]}),zr=/\[\s+\]/,Qr=(e,t)=>t.context==="table"?T(e,t):[{type:"blank",raw:e,text:e.substring(1,e.length-1)}],Vr=L(Qr,{parseOnNested:!1,parseOnQuoted:!0,patterns:[zr]}),Jr=/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i,Xr=/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i,Zr=/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/,en=/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/,tn=e=>/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(e)||rn(e),rn=e=>/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}(\/raw)?$/.test(e),nn=(e,t)=>{if(t.context==="table")return T(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,i]=tn(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:i}]},on=L(nn,{parseOnNested:!0,parseOnQuoted:!0,patterns:[Jr,Xr,Zr,en]}),sn=/\[https?:\/\/[^\s\]]+\s+[^\]]*[^\s]\]/,an=/\[[^[\]]*[^\s]\s+https?:\/\/[^\s\]]+\]/,cn=/\[https?:\/\/[^\s\]]+\]/,un=/https?:\/\/[^\s]+/,pn=(e,t)=>{if(t.context==="table")return T(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()}]},ln=L(pn,{parseOnNested:!0,parseOnQuoted:!0,patterns:[sn,an,cn,un]}),Ct=/\[([^\]]*[^\s])\s+([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)\]/,Dt=/\[([NS]\d+(?:\.\d+)?,[EW]\d+(?:\.\d+)?(?:,Z\d+)?)(?:\s+([^\]]*[^\s]))?\]/,dn=e=>{let[t="",r="",n=""]=e.split(","),o=parseFloat(t.replace(/^N/,"").replace(/^S/,"-")),s=parseFloat(r.replace(/^E/,"").replace(/^W/,"-")),i=/^Z\d+$/.test(n)?parseInt(n.replace(/^Z/,""),10):14;return{latitude:o,longitude:s,zoom:i}},mn=(e,t)=>{var r;if(t.context==="table")return T(e,t);let n=(r=e.match(Ct))!==null&&r!==void 0?r:e.match(Dt);if(n===null)return[];let o=e.startsWith("[N")||e.startsWith("[S"),[,s="",i=""]=o?n:[n[0],n[2],n[1]],{latitude:a,longitude:u,zoom:m}=dn(s),l=i!==""?https://www.google.com/maps/place/${encodeURIComponent(i)}/@${a},${u},${m}z:https://www.google.com/maps/@${a},${u},${m}z;return[{type:"googleMap",raw:e,latitude:a,longitude:u,zoom:m,place:i,url:l}]},fn=L(mn,{parseOnNested:!1,parseOnQuoted:!0,patterns:[Ct,Dt]}),gn=/\[\/?[^[\]]+\]/,hn=e=>{let t=e.substring(1,e.length-1);return[{type:"link",raw:e,pathType:t.startsWith("/")?"root":"relative",href:t,content:""}]},xn=L(hn,{parseOnNested:!0,parseOnQuoted:!0,patterns:[gn]}),yn=/(?:^|\s)#\S+/,En=(e,t)=>{if(t.context==="table")return T(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[...T(r,t),{type:"hashTag",raw:n,href:n.substring(1)}]},bn=L(En,{parseOnNested:!0,parseOnQuoted:!0,patterns:[yn]}),wn=/^[0-9]+\. .*$/,vn=(e,t)=>{if(t.context==="table")return T(e,t);let r=e.indexOf(" "),n=e.substring(0,r-1),o=parseInt(n,10),s=e.substring(r+1,e.length);return[{type:"numberList",raw:e,rawNumber:n,number:o,nodes:N(s,{...t,nested:!0})}]},kn=L(vn,{parseOnNested:!1,parseOnQuoted:!1,patterns:[wn]}),Ln=(e,t,r)=>{var n;return e===""?[]:(n=r?.())!==null&&n!==void 0?n:[]},Tn=(...e)=>(t,r)=>e.reduceRight((n,o)=>()=>o(t,r,n),()=>gr(t,r))(),N=Tn(Ln,yr,wr,Yr,Gr,jr,Vr,qr,Tr,Pr,Or,on,ln,Dr,fn,xn,bn,kn),In=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(i=>i.text.substring(n+1)).map(i=>i.split(" ").map(a=>N(a,{nested:!1,quoted:!1,context:"table"})))}},Cn=e=>{let{indent:t,text:r}=e.rows[0];return{indent:t,type:"line",nodes:N(r.substring(t),{nested:!1,quoted:!1,context:"line"})}},Dn=e=>{switch(e.type){case"title":return mr(e);case"codeBlock":return fr(e);case"table":return In(e);case"line":return Cn(e)}},Rn=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}}),Mn=(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)},Tt=(e,t)=>{let r=e[e.length-1];return r!==void 0&&Mn(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)},Pn=(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(Tt,[])]}return e.reduce(Tt,[])},Rt=(e,t)=>{var r;let n=Rn(e);return Pn(n,{hasTitle:(r=t?.hasTitle)!==null&&r!==void 0?r:!0}).map(Dn)};var Nn=/https?:\/\/(?:www\.|music\.|)youtube\.com\/watch/,Sn=/https?:\/\/youtu\.be\/([a-zA-Z\d_-]+)(?:\?([^\s]{0,100})|)/,On=/https?:\/\/(?:www\.|)youtube\.com\/shorts\/([a-zA-Z\d_-]+)(?:\?([^\s]+)|)/,Fn=/https?:\/\/(?:www\.|music\.|)youtube\.com\/playlist\?((?:[^\s]+&|)list=([a-zA-Z\d_-]+)(?:&[^\s]+|))/,Ie=e=>{if(Nn.test(e)){let t=new URL(e).searchParams,r=t.get("v");if(r)return{pathType:"com",videoId:r,params:t}}{let t=e.match(Sn);if(t){let[,r,n]=t;return{videoId:r,params:new URLSearchParams(n),pathType:"dotbe"}}}{let t=e.match(On);if(t){let[,r,n]=t;return{videoId:r,params:new URLSearchParams(n),pathType:"short"}}}{let t=e.match(Fn);if(t){let[,r,n]=t;return{listId:n,params:new URLSearchParams(r),pathType:"list"}}}};var Pt=e=>{let t=Rt(e,{hasTitle:!0}).flatMap(f=>{switch(f.type){case"codeBlock":case"title":return[];case"line":case"table":return f}}),r=new Map,n=[],o=new Set,s=[],i=new Set,a=[],u=null,m=new Set,l=new Set,h=new RegExp(${location?.origin??"https://scrapbox.io"}/files/([a-z0-9]{24})(?:|\\.[a-zA-Z0-9]+)(?:|\\?[^\\s]*)$),x=f=>{switch(f.type){case"hashTag":if(r.has(C(f.href)))return;r.set(C(f.href),!1),n.push(f.href);return;case"link":switch(f.pathType){case"relative":{let d=Mt(f.href);if(r.get(C(d)))return;r.set(C(d),!0),n.push(d);return}case"root":{let d=Mt(f.href);if(/^\/[\w\d-]+\/?$/.test(d)||o.has(C(d)))return;o.add(C(d)),s.push(d);return}case"absolute":{if(f.content)return;let d=Ie(f.href);if(d&&d.pathType!=="list"){u??=https://i.ytimg.com/vi/${d.videoId}/mqdefault.jpg;return}let E=f.href.match(h)?.[1];E&&m.add(E);return}default:return}case"icon":case"strongIcon":{if(f.pathType==="root"||i.has(C(f.path)))return;i.add(C(f.path)),a.push(f.path);return}case"image":case"strongImage":{u??=f.src.endsWith("/thumb/1000")?f.src.replace(/\/thumb\/1000$/,"/raw"):f.src;{let d=f.src.match(h)?.[1];d&&m.add(d)}if(f.type==="image"){let d=f.link.match(h)?.[1];d&&m.add(d)}return}case"helpfeel":l.add(f.text);return;case"numberList":case"strong":case"quote":case"decoration":{for(let d of f.nodes)x(d);return}default:return}},v=[];for(let f of t)switch(f.type){case"line":for(let d of f.nodes)x(d);continue;case"table":{for(let d of f.cells)for(let E of d)for(let k of E)x(k);if(!["infobox","cosense"].includes(f.fileName))continue;v.push(...f.cells.map(d=>d.map(E=>E.map(k=>k.raw).join("")).join(" ").trim()));continue}}return[n,s,a,u,[...m],[...l],v]},Mt=e=>e.replace(/#[a-f\d]{24,32}$/,""),Nt=e=>e.flatMap(({text:t})=>/^\s*\? .*$/.test(t)?[t.trimStart().slice(2)]:[]);var A=(e,t)=>e.length===t.length&&e.every(r=>t.includes(r));function*St(e,t,r){let n=t.flatMap(v=>v.split( ));for(let v of Te(e.lines,n,{userId:r}))yield v;(e.lines[0].text!==n[0]||!e.persistent)&&(yield{title:n[0]});let o=e.lines.slice(1,6).map(v=>v.text),s=n.slice(1,6);o.join("")!==s.join("")&&(yield{descriptions:s});let[i,a,u,m,l,h,x]=Pt(n.join(
));A(e.links,i)||(yield{links:i}),A(e.projectLinks,a)||(yield{projectLinks:a}),A(e.icons,u)||(yield{icons:u}),e.image!==m&&(yield{image:m}),A(e.files,l)||(yield{files:l}),A(Nt(e.lines),h)||(yield{helpfeels:h}),A(e.infoboxDefinition,x)||(yield{infoboxDefinition:x})}var S=async(e,t,r,n)=>{let[o,s,i]=await Promise.all([te(e,t),ke(e),Lt()]),a={...o,projectId:s,userId:i},u=n?.socket,m=u??await G();await be(m);try{let{request:l}=ye(m),h=0,x=[],v;for(;n?.maxAttempts===void 0||h<n.maxAttempts;){let f=r(a,h,x,v);if(x=f instanceof Promise?await f:f,h++,x.length===0)return{ok:!0,value:a.commitId};let d={kind:"page",projectId:s,pageId:a.id,parentId:a.commitId,userId:i,changes:x,cursor:null,freeze:!0};for(;;){let E=await l("socket.io-request",{method:"commit",data:d});if(E.ok)return a.commitId=E.value.commitId,{ok:!0,value:a.commitId};let k=E.value.name;if(k==="UnexpectedError"){let I=new Error;throw I.name=E.value.name,I.message=JSON.stringify(E.value),I}if(k==="TimeoutError"||k==="SocketIOError"){await F(3e3);break}k==="NotFastForwardError"&&(a={...await te(e,t),projectId:s,userId:i}),v=k}}return{ok:!1,value:{name:"RetryError",attempts:h,message:Retrying exceeded the maxAttempts (${h}).}}}finally{u||await q(m)}};var Ot=e=>{let t=e.match(/(.+?)(?:_(\d+))?$/),r=t?.[1]??e,n=t?.[2]?parseInt(t[2])+1:2;return${r}_${n}};var Ce=(e,t,r,n)=>S(e,t,async(o,s,i,a)=>{if(a==="DuplicateTitleError"){let l=Ot(t);return i.map(h=>("title"in h&&(h.title=l),h))}let u=r(o.lines,{...o,attempts:s}),m=u instanceof Promise?await u:u;return m===void 0?[]:m.length===0?[{deleted:!0}]:[...St(o,m,o.userId)]},n);var De=(e,t,r)=>S(e,t,n=>{if(n.pin>0||!n.persistent&&!(r?.create??!1))return[];let o=[{pin:An()}];return n.persistent||o.unshift({title:t}),o},r),Re=(e,t,r)=>S(e,t,n=>n.pin==0||!n.persistent?[]:[{pin:0}],r),An=()=>Number.MAX_SAFE_INTEGER-Math.floor(Date.now()/1e3);async function*Me(e,t=0){let{count:r,pages:n}=await jn(e,t);for(let o of n)o.pin!==0&&(yield o);(n.at(-1)?.pin??0)!==0&&(yield*Me(e,t+1e3))}var jn=async(e,t)=>{let r=await W(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};var At=(e,t)=>{let r=t.interval??864e5,n=()=>scrapbox.Project.name===e?Un(e,r,t):jt();n(),scrapbox.addListener("project:changed",n)},Bt,Un=async(e,t,r)=>{jt(),await Ft(e,new Date,r),Bt=setInterval(()=>Ft(e,new Date,r),t)},jt=()=>clearInterval(Bt),Ft=async(e,t,{makeDiary:r,isOldDiary:n})=>{let{render:o,dispose:s}=he(),i;try{o({type:"spinner"},{type:"text",text:"unpin other diary pages..."}),i=await Ee();for await(let{title:l}of Me(e))n(l,t)&&await Re(e,l,{socket:i});let{title:a,header:u,footer:m}=r(t);o({type:"spinner"},{type:"text",text:pin "/${e}/${a}"...}),await De(e,a,{socket:i,create:!0}),o({type:"spinner"},{type:"text",text:format "/${e}/${a}"...}),await Ce(e,a,l=>[l[0].text,...fe(l.slice(1).map(h=>h.text),u,m)],{socket:i}),o({type:"check-circle"},{type:"text",text:Pinned "/${e}/${a}".})}catch(a){o({type:"exclamation-triangle"},{type:"text",text:a instanceof Error?${a.name} ${a.message}:"Unknown error! (see developper console)"}),console.error(a)}finally{i&&await q(i),await F(1e3),s()}};function c(e,t){if(t.length<e)throw new TypeError(e+" argument"+(e>1?"s":"")+" required, but only "+t.length+" present")}function p(e){c(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<"u"&&(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 Pe(e){c(1,arguments);let r=p(e).getFullYear();return r%400===0||r%4===0&&r%100!==0}function oe(e){c(1,arguments);let t=p(e);return String(new Date(t))==="Invalid Date"?NaN:Pe(t)?366:365}function Ne(e){c(1,arguments);let t=p(e),r=new Date(0);return r.setFullYear(t.getFullYear(),0,1),r.setHours(0,0,0,0),r}function H(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 se(e){c(1,arguments);let t=p(e);return t.setHours(0,0,0,0),t}var $n=864e5;function z(e,t){c(2,arguments);let r=se(e),n=se(t),o=r.getTime()-H(r),s=n.getTime()-H(n);return Math.round((o-s)/$n)}function ie(e){c(1,arguments);let t=p(e);return z(t,Ne(t))+1}function g(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){c(2,arguments);let r=p(e),n=g(t);return isNaN(n)?new Date(NaN):(n&&r.setDate(r.getDate()+n),r)}function Y(e,t){c(2,arguments);let r=g(t);return _(e,-r)}function O(e,t){c(1,arguments);let r=t||{},n=r.locale,o=n&&n.options&&n.options.weekStartsOn,s=o==null?0:g(o),i=r.weekStartsOn==null?s:g(r.weekStartsOn);if(!(i>=0&&i<=6))throw new RangeError("weekStartsOn must be between 0 and 6 inclusively");let a=p(e),u=a.getDay(),m=(u<i?7:0)+u-i;return a.setDate(a.getDate()-m),a.setHours(0,0,0,0),a}function Se(e,t){c(1,arguments);let r=p(e),n=r.getFullYear(),o=t?.locale?.options?.firstWeekContainsDate,s=o==null?1:g(o),i=t?.firstWeekContainsDate==null?s:g(t.firstWeekContainsDate);if(!(i>=1&&i<=7))throw new RangeError("firstWeekContainsDate must be between 1 and 7 inclusively");let a=new Date(0);a.setFullYear(n+1,0,i),a.setHours(0,0,0,0);let u=O(a,t),m=new Date(0);m.setFullYear(n,0,i),m.setHours(0,0,0,0);let l=O(m,t);return r.getTime()>=u.getTime()?n+1:r.getTime()>=l.getTime()?n:n-1}function Oe(e,t){c(1,arguments);var r=t||{},n=r.locale,o=n&&n.options&&n.options.firstWeekContainsDate,s=o==null?1:g(o),i=r.firstWeekContainsDate==null?s:g(r.firstWeekContainsDate),a=Se(e,t),u=new Date(0);u.setFullYear(a,0,i),u.setHours(0,0,0,0);var m=O(u,t);return m}var qn=6048e5;function ae(e,t){c(1,arguments);let r=p(e),n=O(r,t).getTime()-Oe(r,t).getTime();return Math.round(n/qn)+1}function Q(e,t){c(2,arguments);let r=p(e),n=g(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 i=s.getDate();return o>=i?s:(r.setFullYear(s.getFullYear(),s.getMonth(),o),r)}function Fe(e,t){c(2,arguments);let r=g(t);return Q(e,r*12)}function ce(e,t){c(2,arguments);let r=g(t);return Fe(e,-r)}function ue(e){return c(1,arguments),p(e).getDay()}function pe(e){return c(1,arguments),p(e).getFullYear()}function D(e,t){for(var r=e<0?"-":"",n=Math.abs(e).toString();n.length<t;)n="0"+n;return r+n}var Hn={y(e,t){let r=e.getUTCFullYear(),n=r>0?r:1-r;return D(t==="yy"?n%100:n,t.length)},M(e,t){let r=e.getUTCMonth();return t==="M"?String(r+1):D(r+1,2)},d(e,t){return D(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 D(e.getUTCHours()%12||12,t.length)},H(e,t){return D(e.getUTCHours(),t.length)},m(e,t){return D(e.getUTCMinutes(),t.length)},s(e,t){return D(e.getUTCSeconds(),t.length)},S(e,t){let r=t.length,n=e.getUTCMilliseconds(),o=Math.floor(n*Math.pow(10,r-3));return D(o,t.length)}},Ut=Hn;function K(e){c(1,arguments);var t=p(e);return!isNaN(t)}function Ae(e,t){c(2,arguments);let r=p(e).getTime(),n=g(t);return new Date(r+n)}function Be(e,t){c(2,arguments);let r=g(t);return Ae(e,-r)}var _n=/(\w)\1*|''|'(''|[^'])+('|$)|./g,Yn=/^'([^]*?)'?$/,Kn=/''/g,Wn=/[a-zA-Z]/;function M(e,t){c(2,arguments);let r=p(e);if(!K(r))throw new RangeError("Invalid time value");let n=H(r),o=Be(r,n),s=t.match(_n);return s?s.map(a=>{if(a==="''")return"'";let u=a[0];if(u==="'")return Gn(a);let m=Ut[u];if(m)return m(o,a);if(u.match(Wn))throw new RangeError("Format string contains an unescaped latin alphabet character "+u+"");return a}).join(""):""}function Gn(e){let t=e.match(Yn);return t?t[1].replace(Kn,"'"):e}function R(e,t){c(2,arguments);let r=p(e),n=p(t),o=r.getTime()-n.getTime();return o<0?-1:o>0?1:o}function je(e,t){c(2,arguments);let r=p(e),n=p(t);return r.getFullYear()-n.getFullYear()}function Ue(e,t){c(2,arguments);let r=p(e),n=p(t),o=R(r,n),s=Math.abs(je(r,n));r.setFullYear(1584),n.setFullYear(1584);let i=R(r,n)===-o,a=o*(s-Number(i));return a===0?0:a}function $e(e,t){c(2,arguments);let r=p(e),n=p(t),o=r.getFullYear()-n.getFullYear(),s=r.getMonth()-n.getMonth();return o*12+s}function qe(e){c(1,arguments);let t=p(e);return t.setHours(23,59,59,999),t}function He(e){c(1,arguments);let t=p(e),r=t.getMonth();return t.setFullYear(t.getFullYear(),r+1,0),t.setHours(23,59,59,999),t}function _e(e){c(1,arguments);let t=p(e);return qe(t).getTime()===He(t).getTime()}function Ye(e,t){c(2,arguments);let r=p(e),n=p(t),o=R(r,n),s=Math.abs($e(r,n)),i;if(s<1)i=0;else{r.getMonth()===1&&r.getDate()>27&&r.setDate(30),r.setMonth(r.getMonth()-o*s);let a=R(r,n)===-o;_e(p(e))&&s===1&&R(e,n)===1&&(a=!1),i=o*(s-Number(a))}return i===0?0:i}function $t(e,t){let r=e.getFullYear()-t.getFullYear()||e.getMonth()-t.getMonth()||e.getDate()-t.getDate()||e.getHours()-t.getHours()||e.getMinutes()-t.getMinutes()||e.getSeconds()-t.getSeconds()||e.getMilliseconds()-t.getMilliseconds();return r<0?-1:r>0?1:r}function Ke(e,t){c(2,arguments);let r=p(e),n=p(t),o=$t(r,n),s=Math.abs(z(r,n));r.setDate(r.getDate()-o*s);let i=+($t(r,n)===-o),a=o*(s-i);return a===0?0:a}function B(e,t){c(2,arguments);let r=p(e),n=p(t);return r.getTime()-n.getTime()}var zn=36e5;function We(e,t){c(2,arguments);let r=B(e,t)/zn;return r>0?Math.floor(r):Math.ceil(r)}var Qn=6e4;function Ge(e,t){c(2,arguments);var r=B(e,t)/Qn;return r>0?Math.floor(r):Math.ceil(r)}function ze(e,t){c(2,arguments);let r=B(e,t)/1e3;return r>0?Math.floor(r):Math.ceil(r)}function Qe(e,t){c(2,arguments);let r=g(t);return Q(e,-r)}function j(e,t){if(c(2,arguments),!t||typeof t!="object")return new Date(NaN);let r="years"in t?g(t.years):0,n="months"in t?g(t.months):0,o="weeks"in t?g(t.weeks):0,s="days"in t?g(t.days):0,i="hours"in t?g(t.hours):0,a="minutes"in t?g(t.minutes):0,u="seconds"in t?g(t.seconds):0,m=Qe(p(e),n+r*12),l=Y(m,s+o*7),h=a+i*60,v=(u+h*60)*1e3;return new Date(l.getTime()-v)}function le({start:e,end:t}){c(1,arguments);let r=p(e),n=p(t);if(!K(r))throw new RangeError("Start Date is invalid");if(!K(n))throw new RangeError("End Date is invalid");let o={years:0,months:0,days:0,hours:0,minutes:0,seconds:0},s=R(r,n);o.years=Math.abs(Ue(r,n));let i=j(r,{years:s*o.years});o.months=Math.abs(Ye(i,n));let a=j(i,{months:s*o.months});o.days=Math.abs(Ke(a,n));let u=j(a,{days:s*o.days});o.hours=Math.abs(We(u,n));let m=j(u,{hours:s*o.hours});o.minutes=Math.abs(Ge(m,n));let l=j(m,{minutes:s*o.minutes});return o.seconds=Math.abs(ze(l,n)),o}var V="yyyy/MM/dd",qt=e=>{let t=le({start:new Date(2020,9,9),end:e}).years;return{title:_t(e),header:[第${ae(e)}週: ${Vn(e)},${pe(e)}年 ${(ie(e)*100/oe(e)).toFixed(2)}%経過,"","今日のn年前",...[...Array(t).keys()].map(r=> ${M(ce(e,r+1),V)})],footer:[${M(Y(e,1),V)}←${M(e,V)}→${M(_(e,1),V)},${M(e,"yyyy/MM")}.icon]}},Vn=e=>{let t=ue(e);return[..."日月火水木金土"].map((r,n)=>n===t?${r}`:r).join("")},Jn=/^\d{4}\/\d{2}\/\d{2}$/,Ht=(e,t)=>Jn.test(e)?_t(t)!==e:!1,_t=e=>M(e,V);At("villagepump",{makeDiary:qt,isOldDiary:Ht});