scrapMerge
code:dist.js
function U(a,o){let n={};for(let r=0;r<o.length;r++){let c=or;nc?nc.push(r):nc=r}let e={buffer1index:-1,buffer2index:-1,chain:null};for(let r=0;r<a.length;r++){let c=ar,l=nc||[],s=0,t=e0;for(let f=0;f<l.length;f++){let b=lf,i;for(i=s;i<e.length&&!(ei.buffer2index<b&&(i===e.length-1||ei+1.buffer2index>b));i++);if(i<e.length){let u={buffer1index:r,buffer2index:b,chain:ei};if(s===e.length?e.push(t):es=t,s=i+1,t=u,s===e.length)break}}es=t}return ee.length-1}function I(a,o){let n=U(a,o),e=[],r=a.length,c=o.length;for(let l=n;l!==null;l=l.chain){let s=r-l.buffer1index-1,t=c-l.buffer2index-1;r=l.buffer1index,c=l.buffer2index,(s||t)&&e.push({buffer1:r+1,s,buffer1Content:a.slice(r+1,r+1+s),buffer2:c+1,t,buffer2Content:o.slice(c+1,c+1+t)})}return e.reverse(),e}function j(a,o,n){let e=[];function r(t,f){e.push({ab:f,oStart:t.buffer10,oLength:t.buffer11,abStart:t.buffer20,abLength:t.buffer21})}I(o,a).forEach(t=>r(t,"a")),I(o,n).forEach(t=>r(t,"b")),e.sort((t,f)=>t.oStart-f.oStart);let c=[],l=0;function s(t){t>l&&(c.push({stable:!0,buffer:"o",bufferStart:l,bufferLength:t-l,bufferContent:o.slice(l,t)}),l=t)}for(;e.length;){let t=e.shift(),f=t.oStart,b=t.oStart+t.oLength,i=t;for(s(f);e.length;){let u=e0,p=u.oStart;if(p>b)break;b=Math.max(b,p+u.oLength),i.push(e.shift())}if(i.length===1){if(t.abLength>0){let u=t.ab==="a"?a:n;c.push({stable:!0,buffer:t.ab,bufferStart:t.abStart,bufferLength:t.abLength,bufferContent:u.slice(t.abStart,t.abStart+t.abLength)})}}else{let u={a:a.length,-1,o.length,-1,b:n.length,-1,o.length,-1};for(;i.length;){t=i.shift();let $=t.oStart,w=$+t.oLength,k=t.abStart,M=k+t.abLength,h=ut.ab;h0=Math.min(k,h0),h1=Math.max(M,h1),h2=Math.min($,h2),h3=Math.max(w,h3)}let p=u.a0+(f-u.a2),C=u.a1+(b-u.a3),m=u.b0+(f-u.b2),S=u.b1+(b-u.b3),P={stable:!1,aStart:p,aLength:C-p,aContent:a.slice(p,C),oStart:f,oLength:b-f,oContent:o.slice(f,b),bStart:m,bLength:S-m,bContent:n.slice(m,S)};c.push(P)}l=b}return s(o.length),c}function y(a,o,n,e){e=Object.assign({excludeFalseConflicts:!0,stringSeparator:/\s+/},e),typeof a=="string"&&(a=a.split(e.stringSeparator)),typeof o=="string"&&(o=o.split(e.stringSeparator)),typeof n=="string"&&(n=n.split(e.stringSeparator));let r=[],c=j(a,o,n),l=[];function s(){l.length&&r.push({ok:l}),l=[]}function t(f,b){if(f.length!==b.length)return!1;for(let i=0;i<f.length;i++)if(fi!==bi)return!1;return!0}return c.forEach(f=>{f.stable?l.push(...f.bufferContent):e.excludeFalseConflicts&&t(f.aContent,f.bContent)?l.push(...f.aContent):(s(),r.push({conflict:{a:f.aContent,aIndex:f.aStart,o:f.oContent,oIndex:f.oStart,b:f.bContent,bIndex:f.bStart}}))}),s(),r}function L(a,o,n,e){e=Object.assign({excludeFalseConflicts:!0,stringSeparator:/\s+/,label:{}},e);let r="<<<<<<<"+(e.label.a? ${e.label.a}:""),c="|||||||"+(e.label.o? ${e.label.o}:""),l="=======",s=">>>>>>>"+(e.label.b? ${e.label.b}:""),t=y(a,o,n,e),f=!1,b=[];return t.forEach(i=>{i.ok?b=b.concat(i.ok):i.conflict&&(f=!0,b=b.concat(r,i.conflict.a,c,i.conflict.o,l,i.conflict.b,s))}),{conflict:f,result:b}}function x(a,o){return fetch("https://scrapbox.io/api/pages/"+a+"/"+encodeURIComponent(o)+"/text").then(n=>n.ok?n.text():n.text().then(e=>{console.error(n.status,e),alert(${o}:${n.status}:${e})})).then(n=>n.split(` ).slice(1).join(
))}function E(a,o){return fetch("https://scrapbox.io/api/pages/"+a+"/"+encodeURIComponent(o)).then(n=>n.ok?n.json():n.text().then(e=>{console.error(n.status,e),alert(${o}:${n.status}:${e})})).then(n=>n.persistent)}var g=document.querySelector("ul.user-menu li.list-header").innerText,d=scrapbox.Project.name;scrapbox.PageMenu.addMenu({title:"branchTool",image:"https://i.gyazo.com/5587d1731c863ec1fdb2d46f706d8a08.png"});scrapbox.PageMenu("branchTool").addItem({title:"branch",onClick:()=>{let a=${scrapbox.Page.title} (${g}),o=${scrapbox.Page.title} (${g} backup);E(d,a).then(n=>{if(n)window.open(encodeURIComponent(a));else{let e="",r=scrapbox.Page.lines.slice(1);r.unshift({text:from ${scrapbox.Page.title}});for(let c of r)e+=c.text+
;console.log(e),scrapbox.Page.insertLine(${g} branch:${a},1),scrapbox.Page.insertLine( backup:${o},2),window.open(encodeURIComponent(o)+"?body="+encodeURIComponent(e)),window.open(encodeURIComponent(a)+"?body="+encodeURIComponent(e))}})}});scrapbox.PageMenu("branchTool").addItem({title:"merge",onClick:()=>{let a=scrapbox.Page.title,o=${scrapbox.Page.title} (${g}),n=${scrapbox.Page.title} (${g} backup);x(d,a).then(e=>[e]).then(([e])=>x(d,n).then(r=>[e,r])).then(([e,r])=>x(d,o).then(c=>[e,r,c])).then(([e,r,c])=>{let l=L(e,r,c,{excludeFalseConflicts:!0,stringSeparator:/\n/,label:{a:"main",o:"backup",b:g}});console.log(l);let s=${scrapbox.Page.title}(${g} merged);window.open(encodeURIComponent(n)),window.open(encodeURIComponent(o)),window.open(encodeURIComponent(s)+"?body="+encodeURIComponent(l.result.join( `)))})}});