extract-pdf 0.1.267 → 0.1.269
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.
- package/dist/pdf-to-html.cjs.js +1 -1
- package/dist/pdf-to-html.es.js +1 -1
- package/package.json +15 -15
package/dist/pdf-to-html.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function t(t){for(;32===t.charCodeAt(0);)t=t.substring(1,t.length);return t}function e(t){for(;32===t.charCodeAt(t.length-1);)t=t.substring(0,t.length-1);return t}function s(t){return t>=48&&t<=57}function n(t){for(var e=0;e<t.length;e++)if(!s(t.charCodeAt(e)))return!1;return!0}function r(t,e){const s=e.charCodeAt(0);for(var n=0;n<t.length;n++)if(t.charCodeAt(n)!==s)return!1;return!0}function o(t){return function(t){const e=[];for(var s=0;s<t.length;s++)e.push(t.charCodeAt(s));return e}(t=t.toUpperCase()).filter(t=>32!==t&&9!==t&&46!==t)}function a(t){if(t.length>1)return!1;const e=t.charAt(0);return"-"===e||"•"===e||"–"===e}function i(t){return/^[\s]*[-•–][\s].*$/g.test(t)}function c(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function l(t,e){const s=new Set(t.toUpperCase().split(" ")),n=new Set(e.toUpperCase().split(" "));return new Set([...s].filter(t=>n.has(t))).size/Math.max(s.size,n.size)}var h=(t,e,s)=>Math.floor(t/e*s),m=(s,r,o)=>{for(const{str:a}of s){const s=e(t(a));n(s)&&(r[o]||(r[o]=[]),r[o].push(Number(s)))}return r},p=(t,e,s)=>{const n=h(1,6,s.length),r=h(5,6,s.length),o=m(s.slice(0,n),t,e);return m(s.slice(r),o,e)},u=t=>{let e=0;const s=Object.keys(t);if(0!==s.length&&1!==s.length)for(let n=0;n<s.length-1;n++){const r=t[s[n]],o=t[s[n+1]],a=e;for(let t=0;t<r.length&&e<2;t++)for(let a=0;a<o.length&&e<2;a++){const i=Number(s[n+1])-Number(s[n]);(r[t]+1===o[a]||i>1&&r[t]+i===o[a])&&e++}let i=n>0?Object.entries(t)[n-1]:Object.entries(t)[n];if(a===e)e=0,i=Object.entries(t)[n];else if(e>=2)return{pageIndex:Number(i[0]),pageNum:i[1][0]}}},d=(t,e)=>{const s={items:[...t.items]},n=h(1,6,s.items.length),r=h(5,6,s.items.length);return s.items=s.items.filter((t,o)=>{const a=o>0&&o<n,i=o>r&&o<s.items.length;return a||i?Number(t.str)!==Number(e):t}),s},f=class t{type;annotation;parsedElements;constructor(e){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.type=e.type??null,this.annotation=e.annotation??null,this.parsedElements=e.parsedElements??null}},g=class extends f{x;y;width;height;text;font;lineFormat;unopenedFormat;unclosedFormat;constructor(t){super(t),this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,this.text=t.text,this.font=t.font,this.lineFormat=t.lineFormat??null,this.unopenedFormat=t.unopenedFormat??null,this.unclosedFormat=t.unclosedFormat??null}},y=class{index;items;constructor(t){this.index=t.index,this.items=t.items||[]}};async function w(t,e={}){const s=e.timeout?1e3*e.timeout:1e4,n=new AbortController,r=setTimeout(()=>n.abort(),s);try{const s=await fetch(t,{signal:n.signal});if(clearTimeout(r),!s.ok)throw new Error(`HTTP ${s.status}`);return"arraybuffer"===e.responseType?await s.arrayBuffer():await s.text()}catch(o){throw clearTimeout(r),o}}var x=class t{name;itemType;constructor(e,s){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");if(this.transform===t.prototype.transform)throw new TypeError("Please implement abstract method 'transform()'.");this.name=e,this.itemType=s}transform(t){throw new TypeError("Do not call abstract method foo from child.")}completeTransform(t){return t.messages=[],t}},v=class{category;color;constructor(t){this.category=t.category,this.color=t.color}},T=new v({category:"Added",color:"green"}),E=new v({category:"Removed",color:"red"}),b=new v({category:"Unchanged",color:"brown"}),O=new v({category:"Detected",color:"green"}),L=new v({category:"Modified",color:"green"}),S=class t extends x{constructor(e){if(super(e,g.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},I=class{pages;globals;messages;constructor(t){this.pages=t.pages||[],this.globals=t.globals||{},this.messages=t.messages||[]}},C=class{string;type;format;constructor(t){this.string=t.string,this.type=t.type??null,this.format=t.format??null}},k=class extends f{x;y;width;height;words;constructor(t){super(t),this.x=t.x??0,this.y=t.y??0,this.width=t.width??0,this.height=t.height??0,this.words=t.words||[],t.text&&!t.words&&(this.words=t.text.split(" ").filter(t=>t.trim().length>0).map(t=>new C({string:t})))}text(){return this.wordStrings().join(" ")}wordStrings(){return this.words.map(t=>t.string)}},F=class t{results;stash;constructor(){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.results=[],this.stash=[]}consumeAll(t){t.forEach(t=>this.consume(t))}consume(t){this.shouldStash(t)?(this.matchesStash(t)||this.flushStash(),this.pushOnStash(t)):(this.stash.length>0&&this.flushStash(),this.results.push(t))}pushOnStash(t){this.onPushOnStash(t),this.stash.push(t)}complete(){return this.stash.length>0&&this.flushStash(),this.results}matchesStash(t){if(0===this.stash.length)return!0;const e=this.stash[this.stash.length-1];return this.doMatchesStash(e,t)}flushStash(){this.stash.length>0&&(this.doFlushStash(this.stash,this.results),this.stash=[])}onPushOnStash(t){}shouldStash(t){throw new TypeError(" Do not call abstract method foo from child."+t)}doMatchesStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}doFlushStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}},N=class{footnoteLinks;footnotes;containLinks;formattedWords;constructor(t){this.footnoteLinks=t.footnoteLinks||[],this.footnotes=t.footnotes||[],this.containLinks=t.containLinks??!1,this.formattedWords=t.formattedWords??0}add(t){this.footnoteLinks=this.footnoteLinks.concat(t.footnoteLinks),this.footnotes=this.footnotes.concat(t.footnotes),this.containLinks=this.containLinks||t.containLinks,this.formattedWords+=t.formattedWords}};function _(t){t.sort((t,e)=>t.x-e.x)}var D={BOLD:{name:"BOLD",startSymbol:"<strong>",endSymbol:"</strong>"},OBLIQUE:{name:"OBLIQUE",startSymbol:"<em>",endSymbol:"</em>"},BOLD_OBLIQUE:{name:"BOLD_OBLIQUE",startSymbol:"<strong><em>",endSymbol:"</em></strong>"}},M={LINK:{name:"LINK",toText:t=>`<a href="${t}">${t}</a>`},FOOTNOTE_LINK:{name:"FOOTNOTE_LINK",attachWithoutWhitespace:!0,plainTextFormat:!0,toText:t=>`<sup><a href="#${t}">${t}</a></sup>`},FOOTNOTE:{name:"FOOTNOTE",toText:t=>`<p id="${t}">^${t}</p>`}},$=class{fontToFormats;constructor(t){this.fontToFormats=t}compact(t){_(t);const e=new B(this.fontToFormats);e.consumeAll(t.map(t=>new g({...t})));const s=e.complete();var n=0,r=0;return t.forEach(t=>{n=Math.max(n,t.height),r+=t.width}),new k({x:t[0].x,y:t[0].y,height:n,width:r,words:s,parsedElements:new N({footnoteLinks:e.footnoteLinks,footnotes:e.footnotes,containLinks:e.containLinks,formattedWords:e.formattedWords})})}},B=class extends F{fontToFormats;footnoteLinks;footnotes;formattedWords;containLinks;stashedNumber;firstY;currentItem;constructor(t){super(),this.fontToFormats=t,this.footnoteLinks=[],this.footnotes=[],this.formattedWords=0,this.containLinks=!1,this.stashedNumber=!1,this.currentItem=null}shouldStash(t){return this.firstY||(this.firstY=t.y),this.currentItem=t,!0}onPushOnStash(t){this.stashedNumber=n(t.text.trim())}doMatchesStash(t,e){if(this.fontToFormats.get(t.font)!==this.fontToFormats.get(e.font))return!1;const s=n(e.text.trim());return this.stashedNumber===s}doFlushStash(t,e){if(this.stashedNumber){const s=t.map(t=>t.text).join("").trim();t[0].y>this.firstY?(e.push(new C({string:`${s}`,type:M.FOOTNOTE_LINK})),this.footnoteLinks.push(parseInt(s))):this.currentItem&&this.currentItem.y<t[0].y?(e.push(new C({string:`${s}`,type:M.FOOTNOTE})),this.footnotes.push(s)):this.copyStashItemsAsText(t,e)}else this.copyStashItemsAsText(t,e)}copyStashItemsAsText(t,e){const s=this.fontToFormats.get(t[0].font);e.push(...this.itemsToWords(t,s))}itemsToWords(t,e){const s=(n=t,r="",o=null,n.forEach(t=>{var e=t.text;r.endsWith(" ")||e.startsWith(" ")||(o?t.x-o.x-o.width>5&&(r+=" "):a(t.text)&&(e+=" ")),r+=e,o=t}),r).split(" ");var n,r,o;const i=e?D[e]:null;return s.filter(t=>t.trim().length>0).map(t=>{var e=null;return t.startsWith("http:")?(this.containLinks=!0,e=M.LINK):t.startsWith("www.")&&(this.containLinks=!0,t=`http://${t}`,e=M.LINK),i&&this.formattedWords++,new C({string:t,type:e,format:i})})}};var P=class extends S{fontMap;constructor(t){super("Calculate Global Stats"),this.fontMap=t}transform(t){const e={},s={};var n,r=0;t.pages.forEach(t=>{t.items.forEach(t=>{t.height&&(e[t.height]=e[t.height]?e[t.height]+1:1,s[t.font]=s[t.font]?s[t.font]+1:1,t.height>r&&(r=t.height,n=t.font))})});const o=parseInt(H(e)),a=H(s),i={};t.pages.forEach(t=>{var e;t.items.forEach(t=>{if(t.height===o&&t.text.trim().length>0){if(e&&t.y!==e.y){const s=e.y-t.y;s>0&&(i[s]=i[s]?i[s]+1:1)}e=t}else e=null})});const c=parseInt(H(i)),l=[],h=new Map;this.fontMap?.forEach(function(t,e){l.push(e+" = "+t.name);const s=t.name.toLowerCase();var r=null;e===a?r=null:s.includes("bold")&&(s.includes("oblique")||s.includes("italic"))?r=D.BOLD_OBLIQUE:s.includes("bold")?r=D.BOLD:s.includes("oblique")||s.includes("italic")?r=D.OBLIQUE:s===n&&(r=D.BOLD),r&&h.set(e,r.name)}),l.sort();const m=t.pages.map(t=>({...t,items:t.items.map(t=>({...t}))}));return new I({...t,pages:m,globals:{mostUsedHeight:o,mostUsedFont:a,mostUsedDistance:c,maxHeight:r,maxHeightFont:n,fontToFormats:h},messages:["Items per height: "+JSON.stringify(e),"Items per font: "+JSON.stringify(s),"Items per distance: "+JSON.stringify(i),"Fonts:"+JSON.stringify(l)]})}};function H(t){var e=0,s="";return Object.keys(t).map(n=>{(!s||t[n]>e)&&(e=t[n],s=n)}),s}var U=class t extends x{constructor(e){if(super(e,k.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},A=class{mostUsedDistance;constructor(t){this.mostUsedDistance=t.mostUsedDistance||12}group(t){const e=[];var s=[];return t.forEach(t=>{s.length>0&&Math.abs(s[0].y-t.y)>=this.mostUsedDistance/2&&(e.push(s),s=[]),s.push(t)}),e.push(s),e.forEach(t=>{_(t)}),e}},j={H1:{name:"H1",headline:!0,headlineLevel:1,toText:t=>"<h1>"+W(t.items,!0)+"</h1>"},H2:{name:"H2",headline:!0,headlineLevel:2,toText:t=>"<h2>"+W(t.items,!0)+"</h2>"},H3:{name:"H3",headline:!0,headlineLevel:3,toText:t=>"<h3>"+W(t.items,!0)+"</h3>"},H4:{name:"H4",headline:!0,headlineLevel:4,toText:t=>"<h4>"+W(t.items,!0)+"</h4>"},H5:{name:"H5",headline:!0,headlineLevel:5,toText:t=>"<h5>"+W(t.items,!0)+"</h5>"},H6:{name:"H6",headline:!0,headlineLevel:6,toText:t=>"<h6>"+W(t.items,!0)+"</h6>"},TOC:{name:"TOC",mergeToBlock:!0,toText:t=>W(t.items,!0)},FOOTNOTES:{name:"FOOTNOTES",mergeToBlock:!0,mergeFollowingNonTypedItems:!0,toText:t=>"<p>"+W(t.items,!1)+"</p>"},CODE:{name:"CODE",mergeToBlock:!0,toText:t=>"<code>"+W(t.items,!0)+"</code>"},LIST:{name:"LIST",mergeToBlock:!1,mergeFollowingNonTypedItemsWithSmallDistance:!0,toText:t=>"<ul>\n"+W(t.items,!1)+"</ul>"},PARAGRAPH:{name:"PARAGRAPH",toText:t=>"<p>"+W(t.items,!1)+"</p>"}};function W(t,e){var s,n="";const r=()=>{n+=s.endSymbol,s=null};return t.forEach((o,a)=>{var i;o&&(o.words.forEach((t,o)=>{const a=t.type,i=t.format;var c;!s||i&&i===s||r(),o>0&&(!a||!a.attachWithoutWhitespace)&&(1!==(c=t.string).length||"."!==c[0]&&"!"!==c[0]&&"?"!==c[0])&&(n+=" "),!i||s||e||(n+=(s=i).startSymbol),!a||e&&!a.plainTextFormat?n+=t.string:n+=a.toText(t.string)}),!s||a!==t.length-1&&(0===(i=t[a+1]).words.length?null:i.words[0].format)===s||r(),n+="\n")}),n}j.enumValueOf=function(t){return j[t]},j.isHeadline=function(t){return t&&2===t.name.length&&"H"===t.name[0]},j.blockToText=function(t){return t.type?t.type.toText(t):W(t.items,!1)},j.headlineByLevel=function(t){return 1===t?j.H1:2===t?j.H2:3===t?j.H3:4===t?j.H4:5===t?j.H5:(t>6&&console.warn("Unsupported headline level: "+t+" (supported are 1-6), defaulting to level 6"),j.H6)};var q=class extends U{constructor(){super("Compact To Lines")}transform(t){const{mostUsedDistance:e,fontToFormats:s}=t.globals,n=[],r=[];var o=0,a=0;const i=new A({mostUsedDistance:e}),c=new $(s);return t.pages.forEach(t=>{if(t.items.length>0){const e=[];i.group(t.items).forEach(s=>{const i=c.compact(s);if(s.length>1&&(i.annotation=T,s.forEach(t=>{t.annotation=E,e.push(new k({...t}))})),0===i.words.length&&(i.annotation=E),e.push(i),i.parsedElements.formattedWords&&(a+=i.parsedElements.formattedWords),i.parsedElements.containLinks&&o++,i.parsedElements.footnoteLinks.length>0){const e=i.parsedElements.footnoteLinks.map(e=>({footnoteLink:e,page:t.index+1}));r.push.apply(r,e)}if(i.parsedElements.footnotes.length>0){i.type=j.FOOTNOTES;const e=i.parsedElements.footnotes.map(e=>({footnote:e,page:t.index+1}));n.push.apply(n,e)}}),t.items=e}}),new I({...t,messages:["Detected "+a+" formatted words","Found "+o+" links","Detected "+r.length+" footnotes links","Detected "+n.length+" footnotes"]})}};function R(t){var e=0;if(0===t.trim().length)return e;for(var n=0;n<t.length;n++){const r=t.charCodeAt(n);s(r)||32===r||160===r||(e=(e<<5)-e+r,e|=0)}return e}var Y=class extends U{constructor(){super("Remove Repetitive Elements")}transform(t){const e=[],s={},n={};t.pages.forEach(t=>{const r=t.items.reduce((t,e)=>(e.y<t.minY?(t.minElements=[e],t.minY=e.y):e.y===t.minY&&t.minElements.push(e),e.y>t.maxY?(t.maxElements=[e],t.maxY=e.y):e.y===t.maxY&&t.maxElements.push(e),t),{minY:999,maxY:0,minElements:[],maxElements:[]}),o=R(r.minElements.reduce((t,e)=>t+e.text().toUpperCase(),"")),a=R(r.maxElements.reduce((t,e)=>t+e.text().toUpperCase(),""));e.push({minElements:r.minElements,maxElements:r.maxElements,minLineHash:o,maxLineHash:a}),s[o]=s[o]?s[o]+1:1,n[a]=n[a]?n[a]+1:1});var r=0,o=0;return t.pages.forEach((a,i)=>{s[e[i].minLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].minElements.forEach(t=>{t.annotation=E}),o++),n[e[i].maxLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].maxElements.forEach(t=>{t.annotation=E}),r++)}),new I({...t,messages:["Removed Header: "+r,"Removed Footers: "+o]})}},V=class extends U{constructor(){super("Vertical to Horizontal Text")}transform(t){var e=0;return t.pages.forEach(t=>{const s=new z;s.consumeAll(t.items),t.items=s.complete(),e+=s.foundVerticals}),new I({...t,messages:["Converted "+e+" verticals"]})}},z=class extends F{foundVerticals;constructor(){super(),this.foundVerticals=0}shouldStash(t){return 1===t.words.length&&1===t.words[0].string.length}doMatchesStash(t,e){return t.y-e.y>5&&t.words[0].type===e.words[0].type}doFlushStash(t,e){if(t.length>5){var s=[],n=999,r=0,o=0,a=0;t.forEach(t=>{t.annotation=E,e.push(t),s.push(t.words[0]),n=Math.min(n,t.x),r=Math.max(r,t.y),o+=t.width,a=Math.max(a,t.height)}),e.push(new k({...t[0],x:n,y:r,width:o,height:a,words:s,annotation:T})),this.foundVerticals++}else e.push(...t)}},J=class{headlineCharCodes;stackedLineItems;stackedChars;constructor(t){this.headlineCharCodes=o(t.headline),this.stackedLineItems=[],this.stackedChars=0}consume(t){const e=o(t.text());if(this.matchAll(e)){if(this.stackedLineItems.push(t),this.stackedChars+=e.length,this.stackedChars===this.headlineCharCodes.length)return this.stackedLineItems}else this.stackedChars>0&&(this.stackedChars=0,this.stackedLineItems=[],this.consume(t));return null}matchAll(t){for(var e=0;e<t.length;e++){const s=this.headlineCharCodes[this.stackedChars+e];if(t[e]!==s)return!1}return!0}},K=class extends U{constructor(){super("Detect TOC")}transform(t){const e=[],o=Math.min(20,t.pages.length),a=new G;var i,c,h=[];t.pages.slice(0,o).forEach(t=>{var o=0;const l=new Set,m=[];var p,u;if(t.items.filter(t=>t.words.includes(t=>r(t.string,"."))).forEach(t=>{var e=t.words.filter(t=>!r(t.string,"."));const a=[];for(;e.length>0&&n(e[e.length-1].string);){const t=e.pop();a.unshift(t.string)}if(0===a.length&&e.length>0){const t=e[e.length-1];for(;s(t.string.charCodeAt(t.string.length-1));)a.unshift(t.string.charAt(t.string.length-1)),t.string=t.string.substring(0,t.string.length-1)}var i=a.length>0;i?(i=!0,p&&(e.push(...p),p=null),m.push(new Z({pageNumber:parseInt(a.join("")),lineItem:new k({...t,words:e})})),o++):c?(p&&l.add(u),p=e,u=t):c=t}),100*o/t.items.length>75){e.push(t.index+1),i=t,a.levelPageItems(m),h.push(...m);const s=[];t.items.forEach(t=>{l.has(t)||(t.annotation=E),s.push(t),t===c&&s.push(new k({...t,type:j.H2,annotation:T}))}),t.items=s}else c=null});var m=h.length;const p=[],u=[],d={};if(e.length>0){h.forEach(t=>{i.items.push(new k({words:[new C({string:" ".repeat(3*t.level)+"-"})].concat(t.lineItem.words),type:j.TOC,annotation:T}))});const e=function(t,e){for(var s of e){const e=Q(t,s.lineItem.text());if(e)return e.index-s.pageNumber}return null}(t.pages.filter(t=>t.index>i.index),h);h.forEach(s=>{var n,r=t.pages[s.pageNumber+e];r&&((n=X(r,s.lineItem.text()))||(r=t.pages[s.pageNumber+e+1])&&(n=X(r,s.lineItem.text()))),n?function(t,e,s,n){s.headlineItems.forEach(t=>t.annotation=E);const r=j.headlineByLevel(e.level+2),o=s.headlineItems.reduce((t,e)=>Math.max(t,e.height),0);t.items.splice(s.lineIndex+1,0,new k({...s.headlineItems[0],words:e.lineItem.words,height:o,type:r,annotation:T}));var a=n[r.name];a?(a.min=Math.min(a.min,o),a.max=Math.max(a.max,o)):(a={min:o,max:o},n[r.name]=a)}(r,s,n,d):p.push(s)});var f=i.index+2,g=[];const s=e=>{g.length>0&&(g.forEach(s=>{const n=j.headlineByLevel(s.level+2),r=d[n.name];if(r){const[o,a]=function(t,e,s,n,r){const o=e.lineItem.text().toUpperCase();for(var a=n;a<=r;a++){const e=t[a-1];if(e){const t=e.items.findIndex(t=>!t.type&&!t.annotation&&t.height>=s.min&&t.height<=s.max&&l(o,t.text())>=.5);if(t>-1)return[a-1,t]}}return[-1,-1]}(t.pages,s,r,f,e);if(a>-1){const e=t.pages[o];e.items[a].annotation=E,e.items.splice(a+1,0,new k({...s.lineItem,type:n,annotation:T})),u.push(s)}}}),g=[])};p.length>0&&(h.forEach(t=>{p.includes(t)?g.push(t):(s(t.pageNumber),f=t.pageNumber)}),g.length>0&&s(t.pages.length))}const y=[];return y.push("Detected "+e.length+" table of content pages"),e.length>0&&(y.push("TOC headline heights: "+JSON.stringify(d)),y.push("Found TOC headlines: "+(m-p.length+u.length)+"/"+m)),p.length>0&&(y.push("Found TOC headlines (by size): "+u.map(t=>t.lineItem.text())),y.push("Missing TOC headlines: "+p.filter(t=>!u.includes(t)).map(t=>t.lineItem.text()+"=>"+t.pageNumber))),new I({...t,globals:{...t.globals,tocPages:e,headlineTypeToHeightRange:d},messages:y})}};function Q(t,e){for(var s of t)if(X(s,e))return s;return null}function X(t,e){const s=new J({headline:e});var n=0;for(var r of t.items){const t=s.consume(r);if(t)return{lineIndex:n,headlineItems:t};n++}return null}var G=class{levelByMethod;uniqueFonts;constructor(){this.levelByMethod=null,this.uniqueFonts=[]}levelPageItems(t){if(!this.levelByMethod)if(this.calculateUniqueX(t).length>1)this.levelByMethod=this.levelByXDiff;else{const e=this.calculateUniqueFonts(t);e.length>1?(this.uniqueFonts=e,this.levelByMethod=this.levelByFont):this.levelByMethod=this.levelToZero}this.levelByMethod(t)}levelByXDiff(t){const e=this.calculateUniqueX(t);t.forEach(t=>{t.level=e.indexOf(t.lineItem.x)})}levelByFont(t){t.forEach(t=>{t.level=this.uniqueFonts.indexOf(t.lineItem.font)})}levelToZero(t){t.forEach(t=>{t.level=0})}calculateUniqueX(t){var e=t.reduce(function(t,e){return t.indexOf(e.lineItem.x)<0&&t.push(e.lineItem.x),t},[]);return e.sort((t,e)=>t-e),e}calculateUniqueFonts(t){return t.reduce(function(t,e){return t.indexOf(e.lineItem.font)<0&&t.push(e.lineItem.font),t},[])}},Z=class{lineItem;pageNumber;level;constructor(t){this.lineItem=t.lineItem,this.pageNumber=t.pageNumber,this.level=0}},tt=class extends U{constructor(){super("Detect List Items")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{const n=[];t.items.forEach(t=>{if(n.push(t),!t.type){var r=t.text();if(a(t.words[0].string))if(e++,"-"===t.words[0].string)t.annotation=O,t.type=j.LIST;else{t.annotation=E;const e=t.words.map(t=>new C({...t}));e[0].string="-",n.push(new k({...t,words:e,annotation:T,type:j.LIST}))}else/^[\s]*[\d]*[.][\s].*$/g.test(r)&&(s++,t.annotation=O,t.type=j.LIST)}}),t.items=n}),new I({...t,messages:["Detected "+e+" plain list items.","Detected "+s+" numbered list items."]})}},et=class extends U{constructor(){super("Detect Headers")}transform(t){const{tocPages:e,headlineTypeToHeightRange:s,mostUsedHeight:n,mostUsedDistance:r,mostUsedFont:o,maxHeight:a}=t.globals,c=e.length>0;var l=0;const h=function(t,e){const s=new Set;return t.forEach(t=>{t.items.forEach(n=>{n.type||n.height!==e||s.add(t)})}),s}(t.pages,a),m=n+(a-n)/4;if(h.forEach(t=>{t.items.forEach(t=>{const e=t.height;!t.type&&e>m&&(t.type=e===a?j.H1:j.H2,t.annotation=O,l++)})}),c)Object.keys(s).forEach(e=>{var r=s[e];r.max>n&&t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==r.max||(t.annotation=O,t.type=j.enumValueOf(e),l++)})})});else{const e=[];t.pages.forEach(t=>{t.items.forEach(t=>{!t.type&&t.height>n&&!i(t.text())&&(e.includes(t.height)||e.push(t.height))})}),e.sort((t,e)=>e-t),e.forEach((e,s)=>{if(s+2<=6){const n=j.headlineByLevel(2+s);t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==e||i(t.text())||(l++,t.annotation=O,t.type=n)})})}})}var p=1;if(t.pages.forEach(t=>{t.items.forEach(t=>{t.type&&t.type.headline&&(p=Math.max(p,t.type.headlineLevel))})}),p<6){const e=j.headlineByLevel(p+1);t.pages.forEach(t=>{var s;t.items.forEach(t=>{!t.type&&t.height===n&&t.font!==o&&(!s||s.y<t.y||s.type&&s.type.headline||s.y-t.y>2*r)&&t.text()===t.text().toUpperCase()&&(l++,t.annotation=O,t.type=e),s=t})})}return new I({...t,messages:["Detected "+l+" headlines."]})}};var st=class extends f{items;constructor(t){super(t),this.items=[],t.items&&t.items.forEach(t=>this.addItem(t))}addItem(t){if(this.type&&t.type&&this.type!==t.type)throw new Error(`Adding item of type ${t.type.name} to block of type ${this.type.name}`);this.type||(this.type=t.type),t.parsedElements&&(this.parsedElements?this.parsedElements.add(t.parsedElements):this.parsedElements=t.parsedElements);const e=new k({...t});e.type=null,this.items.push(e)}},nt=class t extends x{constructor(e){if(super(e,st.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},rt=class extends nt{constructor(){super("Gather Blocks")}transform(t){const{mostUsedDistance:e}=t.globals;var s=0,n=0;return t.pages.map(t=>{n+=t.items.length;const r=[];var o=new st({});const a=()=>{o.items.length>1&&(o.annotation=O),r.push(o),o=new st({}),s++};var i=function(t){var e=999;return t.forEach(t=>{e=Math.min(e,t.x)}),999===e?null:e}(t.items);t.items.forEach(t=>{o.items.length>0&&function(t,e,s,n){if(t.type&&t.type.mergeFollowingNonTypedItems&&!e.type)return!1;const r=t.items[t.items.length-1],o=function(t,e,s,n){const r=t.y-e.y;if(r<0-n/2)return!0;var o=n+1;t.x>s&&e.x>s&&(o=n+n/2);return r>o}(r,e,s,n);if(t.type&&t.type.mergeFollowingNonTypedItemsWithSmallDistance&&!e.type&&!o)return!1;if(e.type!==t.type)return!0;return e.type?!e.type.mergeToBlock:o}(o,t,i,e)&&a(),o.addItem(t)}),o.items.length>0&&a(),t.items=r}),new I({...t,messages:["Gathered "+s+" blocks out of "+n+" line items"]})}};var ot=class extends nt{constructor(){super("$1")}transform(t){const e=t.globals.mostUsedHeight??0;var s=0;return t.pages.forEach(t=>{var n=function(t){var e=999;return t.forEach(t=>{t.items.forEach(t=>{e=Math.min(e,t.x)})}),999===e?null:e}(t.items);t.items.forEach(t=>{!t.type&&function(t,e,s){if(0===e.length)return!1;if(1===e.length)return e[0].x>t&&e[0].height<=s+1;for(var n of e)if(n.x===t)return!1;return!0}(n,t.items,e)&&(t.annotation=O,t.type=j.CODE,s++)})}),new I({...t,messages:["Detected "+s+" code/quote items."]})}};var at,it=class extends nt{constructor(){super("Level Lists")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{t.items.filter(t=>t.type===j.LIST).forEach(t=>{var n,r=0;const o={};var a=!1;t.items.forEach(t=>{n?t.x>n?(r++,o[t.x]=r):t.x<n&&(r=o[t.x]):o[t.x]=0,r>0&&(t.words=[new C({string:" ".repeat(3*r)})].concat(t.words),a=!0),n=t.x}),e++,a?(s++,t.annotation=L):t.annotation=b})}),new I({...t,messages:["Modified "+s+" / "+e+" list blocks."]})}},ct=class extends x{constructor(){super("To Text Blocks","TextBlock")}transform(t){return t.pages.forEach(t=>{const e=[];t.items.forEach(t=>{const s=t.type?t.type.name:"Unknown";e.push({category:s,text:j.blockToText(t)})}),t.items=e}),new I({...t})}},lt=class extends x{constructor(){super("To HTML","String")}transform(t){return t.pages.forEach(t=>{var e="";t.items.forEach(t=>{let s;s="TOC"===t.category?t.text:t.text.replace(/(\r\n|\n|\r)/gm,"\n"),"LIST"!==t.category&&(s=s.split("- ").join("")),"CODE"===t.category&&(s=s.split("<code>").join("").split("</code>").join("")),e+=`<p>${s}</p>\n\n`}),t.items=[e]}),new I({...t})}};async function ht(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{({LiteParse:a}=await import("@llamaindex/liteparse"))}catch(u){return{error:`method: 'liteparse' requires the optional @llamaindex/liteparse dependency (Node.js only): ${u.message}`}}const i=new a({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(u){return{error:u.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,p;return n&&(p=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:p,title:m,html:h,format:"pdf"}}async function mt(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{a=await import("@llamaindex/liteparse-wasm"),await async function(t){at||(at=t.default()),await at}(a)}catch(u){return{error:`method: 'liteparse-wasm' requires the optional @llamaindex/liteparse-wasm dependency: ${u.message}`}}const i=new a.LiteParse({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(u){return{error:u.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,p;return n&&(p=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:p,title:m,html:h,format:"pdf"}}var pt=null;async function ut(){return pt||(pt=import("https://cdn.jsdelivr.net/npm/pdfjs-serverless@1/+esm").catch(()=>import("pdfjs-serverless")).then(({resolvePDFJS:t})=>t())),pt}var dt=/\b(?:fig(?:ure)?s?|table|chart|graph|diagram|infographic|exhibit|illustration|plate|scheme)\s*\.?\s*(?:\d+(?:\.\d+)*|[ivxlcdm]+)\b/gi,ft=/^\s*\S[^\t\n]{0,60}?(?:\t+| {2,})\S[^\t\n]{0,60}?(?:\t+| {2,})\S/,gt=/(?:[-+]?[\d][\d.,]*\s*[%$€£]?\s+){3,}[-+]?[\d]/,yt=/[\uFFFD\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function wt(t,e={}){const{sparseTextThreshold:s=200,minTableRows:n=3,minNumericRows:r=2}=e,o=t.map((t,e)=>{const o=[],a=(t||"").replace(/\s+/g," ").trim();0===a.length?o.push("no-text"):a.length<s&&o.push("sparse-text");const i=(t||"").match(yt)?.length||0;a.length>0&&i/a.length>.05&&o.push("garbled-text");const c=[...(t||"").matchAll(dt)].map(t=>t[0].replace(/\s+/g," ").trim()),l=[...(t||"").matchAll(new RegExp(`^\\s*${dt.source}`,"gim"))].map(t=>t[0].trim());l.some(t=>/^table/i.test(t))&&o.push("table-caption"),l.some(t=>!/^table/i.test(t))&&o.push("figure-caption");const h=(t||"").split(/\r?\n/),m=h.filter(t=>ft.test(t)).length,p=h.filter(t=>gt.test(t)).length;return m>=n&&o.push("table-rows"),p>=r&&o.push("numeric-grid"),{page:e+1,needsOcr:o.length>0,reasons:o,captions:c}}),a=o.filter(t=>t.needsOcr).map(t=>t.page);return{needsOcr:a.length>0,pagesNeedingOcr:a,pages:o}}var xt="onnx-community/granite-docling-258M-ONNX",vt="Convert this page to docling.",Tt=null;async function Et(t,e={}){const{processorUrl:s,prompt:n=vt,maxTokens:r=4096}=e;if(s){const e=await fetch(`${s.replace(/\/$/,"")}/api/v1/convert-base64`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({imageBase64:t,mimeType:"image/png",prompt:n,maxTokens:r})}),o=await e.json();if(!e.ok||!o?.success)throw new Error(o?.error||`Processor error ${e.status}`);return o.result}const{model:o,processor:a,lib:i}=await async function(){return Tt||(Tt=(async()=>{const t=await import("@huggingface/transformers"),e=await t.AutoProcessor.from_pretrained(xt);return{model:await t.AutoModelForVision2Seq.from_pretrained(xt,{dtype:"fp32"}),processor:e,lib:t}})()),Tt}(),c=await i.load_image(`data:image/png;base64,${t}`),l=[{role:"user",content:[{type:"image"},{type:"text",text:n}]}],h=await a(a.apply_chat_template(l,{add_generation_prompt:!0}),[c],{do_image_splitting:!0}),m=await o.generate({...h,max_new_tokens:r}),[p]=a.batch_decode(m.slice(null,[h.input_ids.dims.at(-1),null]),{skip_special_tokens:!0});return p}async function bt(t,e=2){const s=t.getViewport({scale:e}),n=await async function(t,e){if("undefined"!=typeof document){const s=document.createElement("canvas");return s.width=t,s.height=e,s}try{return(await import("@napi-rs/canvas")).createCanvas(t,e)}catch{}if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(t,e);throw new Error("No canvas available to rasterize PDF pages — install @napi-rs/canvas in Node.js, or run in a browser/Worker with OffscreenCanvas")}(Math.ceil(s.width),Math.ceil(s.height)),r=n.getContext("2d");return await t.render({canvasContext:r,viewport:s}).promise,async function(t){if("function"==typeof t.toBuffer)return t.toBuffer("image/png").toString("base64");if("function"==typeof t.convertToBlob){const e=await t.convertToBlob({type:"image/png"}),s=new Uint8Array(await e.arrayBuffer());let n="";for(const t of s)n+=String.fromCharCode(t);return btoa(n)}return t.toDataURL("image/png").split(",")[1]}(n)}async function Ot(t,e,s={}){const n=new Map;for(const o of e)try{const e=await Et(await bt(await t.getPage(o),s.scale),s);n.set(o,Lt(e))}catch(r){console.error(`Docling OCR failed for page ${o}:`,r)}return n}function Lt(t){let e=(t||"").replace(/<\/?doctag>/g,"").replace(/<loc_\d+>/g,"").replace(/<page_break>/g,"").replace(/<end_of_utterance>/g,"").replace(/<page_(?:header|footer)>[\s\S]*?<\/page_(?:header|footer)>/g,"");e=e.replace(/<otsl>([\s\S]*?)<\/otsl>/g,(t,e)=>function(t){return`<table>${t.split("<nl>").map(t=>t.trim()).filter(Boolean).map(t=>{const e=[...t.matchAll(/<(fcel|ched|rhed|ecel|lcel|ucel|xcel)>([^<]*)/g)];return e.length?`<tr>${e.map(([,t,e])=>{const s="ched"===t||"rhed"===t?"th":"td";return`<${s}>${e.trim()}</${s}>`}).join("")}</tr>`:""}).filter(Boolean).join("")}</table>`}(e));for(const[s,n,r]of[[/<title>([\s\S]*?)<\/title>/g,"<h1>","</h1>"],[/<section_header_level_1>([\s\S]*?)<\/section_header_level_1>/g,"<h2>","</h2>"],[/<section_header_level_2>([\s\S]*?)<\/section_header_level_2>/g,"<h3>","</h3>"],[/<section_header_level_3>([\s\S]*?)<\/section_header_level_3>/g,"<h4>","</h4>"],[/<section_header_level_[4-9]>([\s\S]*?)<\/section_header_level_[4-9]>/g,"<h5>","</h5>"],[/<(?:text|paragraph)>([\s\S]*?)<\/(?:text|paragraph)>/g,"<p>","</p>"],[/<caption>([\s\S]*?)<\/caption>/g,"<figcaption>","</figcaption>"],[/<(?:picture|chart)>([\s\S]*?)<\/(?:picture|chart)>/g,"<figure>","</figure>"],[/<code>([\s\S]*?)<\/code>/g,"<pre><code>","</code></pre>"],[/<formula>([\s\S]*?)<\/formula>/g,'<code class="formula">',"</code>"],[/<footnote>([\s\S]*?)<\/footnote>/g,'<p class="footnote">',"</p>"],[/<list_item>([\s\S]*?)<\/list_item>/g,"<li>","</li>"],[/<unordered_list>([\s\S]*?)<\/unordered_list>/g,"<ul>","</ul>"],[/<ordered_list>([\s\S]*?)<\/ordered_list>/g,"<ol>","</ol>"]])e=e.replace(s,(t,e)=>`${n}${e.trim()}${r}`);return e=e.replace(/<\/?(?:[a-z][a-z0-9]*_[a-z0-9_]+|otsl|smiles)>/g,""),e.trim()}exports.convertPDFToHTML=async function(t,e={}){if("liteparse"===e.method){const{method:s,...n}=e;return ht(t,n)}if("liteparse-wasm"===e.method){const{method:s,...n}=e;return mt(t,n)}var{addPageNumbers:s=!1,addCitation:n=!0}=e;const r=e.processor??"frontend",o=/^https?:\/\//i.test(r),a=o?"docling":r,i={...e.doclingOptions,processorUrl:o?r:e.processorUrl};var c="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let l;try{const{getDocument:t}=await ut();l=await t({data:new Uint8Array(c),useSystemFonts:!0,verbosity:0}).promise}catch(k){return{error:k.message}}const h=[...Array(l.numPages).keys()].map(t=>new y({index:t}));let m,f={};for(let d=1;d<=l.numPages;d++){const t=await l.getPage(d),e=await t.getTextContent();if(!(Object.keys(f).length<10)){m=u(f);break}f=p(f,t.pageNumber-1,e.items)}let x=m?m.pageNum:0;for(let p=1;p<=l.numPages;p++){const t=await l.getPage(p);await t.getOperatorList();const e=t.getViewport({scale:1});let s=await t.getTextContent();m&&t.pageIndex>=m.pageIndex&&(s=d(s,x),x++);const n=s.items.map(t=>{const s=[1,0,0,1,0,0];for(let o=0;o<6;o++)s[o]+=t.transform[o]*e.transform[o%2?3:0],o%2&&(s[o+1]+=t.transform[o]*e.transform[1]);const n=Math.sqrt(s[2]*s[2]+s[3]*s[3]),r=t.height/n;return new g({x:Math.round(t.transform[4]),y:Math.round(t.transform[5]),width:Math.round(t.width),height:Math.round(r<=1?t.height:r),text:t.str,font:t.fontName})});h[t.pageNumber-1].items=n}const v=h.map(t=>Object.values(t.items.reduce((t,e)=>((t[e.y]=t[e.y]||[]).push(e.text),t),{})).map(t=>t.join(" ")).join("\n"));var T=new I({pages:h});let E;[new P,new q,new Y,new V,new K,new et,new tt,new rt,new ot,new it,new ct,new lt].forEach(t=>{E&&(T=E.completeTransform(T)),T=t.transform(T),E=t});var b=T.pages.map((t,e)=>`<p id="page-${e+1}">${s?` [${e+1}] `:""}${t.items.join('</p><p id="page-'+e+'">')}</p>`);const O=wt(v,e.ocrScanOptions);if("docling"===a||"hybrid"===a){const t="docling"===a?b.map((t,e)=>e+1):O.pagesNeedingOcr,e=await Ot(l,t,i);for(const[n,r]of e)b[n-1]=`<section class="ocr-page" id="page-${n}">${s?` [${n}] `:""}${r}</section>`}var L=b.join("");if(n){var{Author:S,Title:C}=(await l.getMetadata()).info;C=L.slice(0,400).match(/<h[0-9]>(.*?)<\/h[0-9]>/)?.[1]||C}return{author:S,title:C,html:L,format:"pdf",processor:a,ocrScan:O}},exports.convertPDFToHTMLWithLiteParse=ht,exports.convertPDFToHTMLWithLiteParseWasm=mt,exports.detectPdfNeedsOcr=async function(t,e={}){const s="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t,{LiteParse:n}=await import("@llamaindex/liteparse"),r=await new n({quiet:!0,...e.liteParseOptions}).isComplex(new Uint8Array(s));return{needsOcr:r.some(t=>t.needsOcr),pages:r,reasons:[...new Set(r.flatMap(t=>t.reasons))]}},exports.doctagsToHtml=Lt,exports.loadPdfJs=ut,exports.ocrImageWithDocling=Et,exports.ocrPdfPagesWithDocling=Ot,exports.renderPdfPageToPngBase64=bt,exports.scanPagesForOCR=wt;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function t(t){for(;32===t.charCodeAt(0);)t=t.substring(1,t.length);return t}function e(t){for(;32===t.charCodeAt(t.length-1);)t=t.substring(0,t.length-1);return t}function s(t){return t>=48&&t<=57}function n(t){for(var e=0;e<t.length;e++)if(!s(t.charCodeAt(e)))return!1;return!0}function r(t,e){const s=e.charCodeAt(0);for(var n=0;n<t.length;n++)if(t.charCodeAt(n)!==s)return!1;return!0}function o(t){return function(t){const e=[];for(var s=0;s<t.length;s++)e.push(t.charCodeAt(s));return e}(t=t.toUpperCase()).filter(t=>32!==t&&9!==t&&46!==t)}function a(t){if(t.length>1)return!1;const e=t.charAt(0);return"-"===e||"•"===e||"–"===e}function i(t){return/^[\s]*[-•–][\s].*$/g.test(t)}function c(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function l(t,e){const s=new Set(t.toUpperCase().split(" ")),n=new Set(e.toUpperCase().split(" "));return new Set([...s].filter(t=>n.has(t))).size/Math.max(s.size,n.size)}var h=(t,e,s)=>Math.floor(t/e*s),m=(s,r,o)=>{for(const{str:a}of s){const s=e(t(a));n(s)&&(r[o]||(r[o]=[]),r[o].push(Number(s)))}return r},p=(t,e,s)=>{const n=h(1,6,s.length),r=h(5,6,s.length),o=m(s.slice(0,n),t,e);return m(s.slice(r),o,e)},u=t=>{let e=0;const s=Object.keys(t);if(0!==s.length&&1!==s.length)for(let n=0;n<s.length-1;n++){const r=t[s[n]],o=t[s[n+1]],a=e;for(let t=0;t<r.length&&e<2;t++)for(let a=0;a<o.length&&e<2;a++){const i=Number(s[n+1])-Number(s[n]);(r[t]+1===o[a]||i>1&&r[t]+i===o[a])&&e++}let i=n>0?Object.entries(t)[n-1]:Object.entries(t)[n];if(a===e)e=0,i=Object.entries(t)[n];else if(e>=2)return{pageIndex:Number(i[0]),pageNum:i[1][0]}}},d=(t,e)=>{const s={items:[...t.items]},n=h(1,6,s.items.length),r=h(5,6,s.items.length);return s.items=s.items.filter((t,o)=>{const a=o>0&&o<n,i=o>r&&o<s.items.length;return a||i?Number(t.str)!==Number(e):t}),s},f=class t{type;annotation;parsedElements;constructor(e){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.type=e.type??null,this.annotation=e.annotation??null,this.parsedElements=e.parsedElements??null}},g=class extends f{x;y;width;height;text;font;lineFormat;unopenedFormat;unclosedFormat;constructor(t){super(t),this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,this.text=t.text,this.font=t.font,this.lineFormat=t.lineFormat??null,this.unopenedFormat=t.unopenedFormat??null,this.unclosedFormat=t.unclosedFormat??null}},y=class{index;items;constructor(t){this.index=t.index,this.items=t.items||[]}};async function w(t,e={}){const s=e.timeout?1e3*e.timeout:1e4,n=new AbortController,r=setTimeout(()=>n.abort(),s);try{const s=await fetch(t,{signal:n.signal});if(clearTimeout(r),!s.ok)throw new Error(`HTTP ${s.status}`);return"arraybuffer"===e.responseType?await s.arrayBuffer():await s.text()}catch(o){throw clearTimeout(r),o}}var x=class t{name;itemType;constructor(e,s){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");if(this.transform===t.prototype.transform)throw new TypeError("Please implement abstract method 'transform()'.");this.name=e,this.itemType=s}transform(t){throw new TypeError("Do not call abstract method foo from child.")}completeTransform(t){return t.messages=[],t}},v=class{category;color;constructor(t){this.category=t.category,this.color=t.color}},T=new v({category:"Added",color:"green"}),E=new v({category:"Removed",color:"red"}),b=new v({category:"Unchanged",color:"brown"}),O=new v({category:"Detected",color:"green"}),L=new v({category:"Modified",color:"green"}),S=class t extends x{constructor(e){if(super(e,g.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},I=class{pages;globals;messages;constructor(t){this.pages=t.pages||[],this.globals=t.globals||{},this.messages=t.messages||[]}},C=class{string;type;format;constructor(t){this.string=t.string,this.type=t.type??null,this.format=t.format??null}},k=class extends f{x;y;width;height;words;constructor(t){super(t),this.x=t.x??0,this.y=t.y??0,this.width=t.width??0,this.height=t.height??0,this.words=t.words||[],t.text&&!t.words&&(this.words=t.text.split(" ").filter(t=>t.trim().length>0).map(t=>new C({string:t})))}text(){return this.wordStrings().join(" ")}wordStrings(){return this.words.map(t=>t.string)}},F=class t{results;stash;constructor(){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.results=[],this.stash=[]}consumeAll(t){t.forEach(t=>this.consume(t))}consume(t){this.shouldStash(t)?(this.matchesStash(t)||this.flushStash(),this.pushOnStash(t)):(this.stash.length>0&&this.flushStash(),this.results.push(t))}pushOnStash(t){this.onPushOnStash(t),this.stash.push(t)}complete(){return this.stash.length>0&&this.flushStash(),this.results}matchesStash(t){if(0===this.stash.length)return!0;const e=this.stash[this.stash.length-1];return this.doMatchesStash(e,t)}flushStash(){this.stash.length>0&&(this.doFlushStash(this.stash,this.results),this.stash=[])}onPushOnStash(t){}shouldStash(t){throw new TypeError(" Do not call abstract method foo from child."+t)}doMatchesStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}doFlushStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}},N=class{footnoteLinks;footnotes;containLinks;formattedWords;constructor(t){this.footnoteLinks=t.footnoteLinks||[],this.footnotes=t.footnotes||[],this.containLinks=t.containLinks??!1,this.formattedWords=t.formattedWords??0}add(t){this.footnoteLinks=this.footnoteLinks.concat(t.footnoteLinks),this.footnotes=this.footnotes.concat(t.footnotes),this.containLinks=this.containLinks||t.containLinks,this.formattedWords+=t.formattedWords}};function _(t){t.sort((t,e)=>t.x-e.x)}var D={BOLD:{name:"BOLD",startSymbol:"<strong>",endSymbol:"</strong>"},OBLIQUE:{name:"OBLIQUE",startSymbol:"<em>",endSymbol:"</em>"},BOLD_OBLIQUE:{name:"BOLD_OBLIQUE",startSymbol:"<strong><em>",endSymbol:"</em></strong>"}},M={LINK:{name:"LINK",toText:t=>`<a href="${t}">${t}</a>`},FOOTNOTE_LINK:{name:"FOOTNOTE_LINK",attachWithoutWhitespace:!0,plainTextFormat:!0,toText:t=>`<sup><a href="#${t}">${t}</a></sup>`},FOOTNOTE:{name:"FOOTNOTE",toText:t=>`<p id="${t}">^${t}</p>`}},$=class{fontToFormats;constructor(t){this.fontToFormats=t}compact(t){_(t);const e=new B(this.fontToFormats);e.consumeAll(t.map(t=>new g({...t})));const s=e.complete();var n=0,r=0;return t.forEach(t=>{n=Math.max(n,t.height),r+=t.width}),new k({x:t[0].x,y:t[0].y,height:n,width:r,words:s,parsedElements:new N({footnoteLinks:e.footnoteLinks,footnotes:e.footnotes,containLinks:e.containLinks,formattedWords:e.formattedWords})})}},B=class extends F{fontToFormats;footnoteLinks;footnotes;formattedWords;containLinks;stashedNumber;firstY;currentItem;constructor(t){super(),this.fontToFormats=t,this.footnoteLinks=[],this.footnotes=[],this.formattedWords=0,this.containLinks=!1,this.stashedNumber=!1,this.currentItem=null}shouldStash(t){return this.firstY||(this.firstY=t.y),this.currentItem=t,!0}onPushOnStash(t){this.stashedNumber=n(t.text.trim())}doMatchesStash(t,e){if(this.fontToFormats.get(t.font)!==this.fontToFormats.get(e.font))return!1;const s=n(e.text.trim());return this.stashedNumber===s}doFlushStash(t,e){if(this.stashedNumber){const s=t.map(t=>t.text).join("").trim();t[0].y>this.firstY?(e.push(new C({string:`${s}`,type:M.FOOTNOTE_LINK})),this.footnoteLinks.push(parseInt(s))):this.currentItem&&this.currentItem.y<t[0].y?(e.push(new C({string:`${s}`,type:M.FOOTNOTE})),this.footnotes.push(s)):this.copyStashItemsAsText(t,e)}else this.copyStashItemsAsText(t,e)}copyStashItemsAsText(t,e){const s=this.fontToFormats.get(t[0].font);e.push(...this.itemsToWords(t,s))}itemsToWords(t,e){const s=(n=t,r="",o=null,n.forEach(t=>{var e=t.text;r.endsWith(" ")||e.startsWith(" ")||(o?t.x-o.x-o.width>5&&(r+=" "):a(t.text)&&(e+=" ")),r+=e,o=t}),r).split(" ");var n,r,o;const i=e?D[e]:null;return s.filter(t=>t.trim().length>0).map(t=>{var e=null;return t.startsWith("http:")?(this.containLinks=!0,e=M.LINK):t.startsWith("www.")&&(this.containLinks=!0,t=`http://${t}`,e=M.LINK),i&&this.formattedWords++,new C({string:t,type:e,format:i})})}};var P=class extends S{fontMap;constructor(t){super("Calculate Global Stats"),this.fontMap=t}transform(t){const e={},s={};var n,r=0;t.pages.forEach(t=>{t.items.forEach(t=>{t.height&&(e[t.height]=e[t.height]?e[t.height]+1:1,s[t.font]=s[t.font]?s[t.font]+1:1,t.height>r&&(r=t.height,n=t.font))})});const o=parseInt(H(e)),a=H(s),i={};t.pages.forEach(t=>{var e;t.items.forEach(t=>{if(t.height===o&&t.text.trim().length>0){if(e&&t.y!==e.y){const s=e.y-t.y;s>0&&(i[s]=i[s]?i[s]+1:1)}e=t}else e=null})});const c=parseInt(H(i)),l=[],h=new Map;this.fontMap?.forEach(function(t,e){l.push(e+" = "+t.name);const s=t.name.toLowerCase();var r=null;e===a?r=null:s.includes("bold")&&(s.includes("oblique")||s.includes("italic"))?r=D.BOLD_OBLIQUE:s.includes("bold")?r=D.BOLD:s.includes("oblique")||s.includes("italic")?r=D.OBLIQUE:s===n&&(r=D.BOLD),r&&h.set(e,r.name)}),l.sort();const m=t.pages.map(t=>({...t,items:t.items.map(t=>({...t}))}));return new I({...t,pages:m,globals:{mostUsedHeight:o,mostUsedFont:a,mostUsedDistance:c,maxHeight:r,maxHeightFont:n,fontToFormats:h},messages:["Items per height: "+JSON.stringify(e),"Items per font: "+JSON.stringify(s),"Items per distance: "+JSON.stringify(i),"Fonts:"+JSON.stringify(l)]})}};function H(t){var e=0,s="";return Object.keys(t).map(n=>{(!s||t[n]>e)&&(e=t[n],s=n)}),s}var U=class t extends x{constructor(e){if(super(e,k.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},A=class{mostUsedDistance;constructor(t){this.mostUsedDistance=t.mostUsedDistance||12}group(t){const e=[];var s=[];return t.forEach(t=>{s.length>0&&Math.abs(s[0].y-t.y)>=this.mostUsedDistance/2&&(e.push(s),s=[]),s.push(t)}),e.push(s),e.forEach(t=>{_(t)}),e}},j={H1:{name:"H1",headline:!0,headlineLevel:1,toText:t=>"<h1>"+W(t.items,!0)+"</h1>"},H2:{name:"H2",headline:!0,headlineLevel:2,toText:t=>"<h2>"+W(t.items,!0)+"</h2>"},H3:{name:"H3",headline:!0,headlineLevel:3,toText:t=>"<h3>"+W(t.items,!0)+"</h3>"},H4:{name:"H4",headline:!0,headlineLevel:4,toText:t=>"<h4>"+W(t.items,!0)+"</h4>"},H5:{name:"H5",headline:!0,headlineLevel:5,toText:t=>"<h5>"+W(t.items,!0)+"</h5>"},H6:{name:"H6",headline:!0,headlineLevel:6,toText:t=>"<h6>"+W(t.items,!0)+"</h6>"},TOC:{name:"TOC",mergeToBlock:!0,toText:t=>W(t.items,!0)},FOOTNOTES:{name:"FOOTNOTES",mergeToBlock:!0,mergeFollowingNonTypedItems:!0,toText:t=>"<p>"+W(t.items,!1)+"</p>"},CODE:{name:"CODE",mergeToBlock:!0,toText:t=>"<code>"+W(t.items,!0)+"</code>"},LIST:{name:"LIST",mergeToBlock:!1,mergeFollowingNonTypedItemsWithSmallDistance:!0,toText:t=>"<ul>\n"+W(t.items,!1)+"</ul>"},PARAGRAPH:{name:"PARAGRAPH",toText:t=>"<p>"+W(t.items,!1)+"</p>"}};function W(t,e){var s,n="";const r=()=>{n+=s.endSymbol,s=null};return t.forEach((o,a)=>{var i;o&&(o.words.forEach((t,o)=>{const a=t.type,i=t.format;var c;!s||i&&i===s||r(),o>0&&(!a||!a.attachWithoutWhitespace)&&(1!==(c=t.string).length||"."!==c[0]&&"!"!==c[0]&&"?"!==c[0])&&(n+=" "),!i||s||e||(n+=(s=i).startSymbol),!a||e&&!a.plainTextFormat?n+=t.string:n+=a.toText(t.string)}),!s||a!==t.length-1&&(0===(i=t[a+1]).words.length?null:i.words[0].format)===s||r(),n+="\n")}),n}j.enumValueOf=function(t){return j[t]},j.isHeadline=function(t){return t&&2===t.name.length&&"H"===t.name[0]},j.blockToText=function(t){return t.type?t.type.toText(t):W(t.items,!1)},j.headlineByLevel=function(t){return 1===t?j.H1:2===t?j.H2:3===t?j.H3:4===t?j.H4:5===t?j.H5:(t>6&&console.warn("Unsupported headline level: "+t+" (supported are 1-6), defaulting to level 6"),j.H6)};var q=class extends U{constructor(){super("Compact To Lines")}transform(t){const{mostUsedDistance:e,fontToFormats:s}=t.globals,n=[],r=[];var o=0,a=0;const i=new A({mostUsedDistance:e}),c=new $(s);return t.pages.forEach(t=>{if(t.items.length>0){const e=[];i.group(t.items).forEach(s=>{const i=c.compact(s);if(s.length>1&&(i.annotation=T,s.forEach(t=>{t.annotation=E,e.push(new k({...t}))})),0===i.words.length&&(i.annotation=E),e.push(i),i.parsedElements.formattedWords&&(a+=i.parsedElements.formattedWords),i.parsedElements.containLinks&&o++,i.parsedElements.footnoteLinks.length>0){const e=i.parsedElements.footnoteLinks.map(e=>({footnoteLink:e,page:t.index+1}));r.push.apply(r,e)}if(i.parsedElements.footnotes.length>0){i.type=j.FOOTNOTES;const e=i.parsedElements.footnotes.map(e=>({footnote:e,page:t.index+1}));n.push.apply(n,e)}}),t.items=e}}),new I({...t,messages:["Detected "+a+" formatted words","Found "+o+" links","Detected "+r.length+" footnotes links","Detected "+n.length+" footnotes"]})}};function R(t){var e=0;if(0===t.trim().length)return e;for(var n=0;n<t.length;n++){const r=t.charCodeAt(n);s(r)||32===r||160===r||(e=(e<<5)-e+r,e|=0)}return e}var Y=class extends U{constructor(){super("Remove Repetitive Elements")}transform(t){const e=[],s={},n={};t.pages.forEach(t=>{const r=t.items.reduce((t,e)=>(e.y<t.minY?(t.minElements=[e],t.minY=e.y):e.y===t.minY&&t.minElements.push(e),e.y>t.maxY?(t.maxElements=[e],t.maxY=e.y):e.y===t.maxY&&t.maxElements.push(e),t),{minY:999,maxY:0,minElements:[],maxElements:[]}),o=R(r.minElements.reduce((t,e)=>t+e.text().toUpperCase(),"")),a=R(r.maxElements.reduce((t,e)=>t+e.text().toUpperCase(),""));e.push({minElements:r.minElements,maxElements:r.maxElements,minLineHash:o,maxLineHash:a}),s[o]=s[o]?s[o]+1:1,n[a]=n[a]?n[a]+1:1});var r=0,o=0;return t.pages.forEach((a,i)=>{s[e[i].minLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].minElements.forEach(t=>{t.annotation=E}),o++),n[e[i].maxLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].maxElements.forEach(t=>{t.annotation=E}),r++)}),new I({...t,messages:["Removed Header: "+r,"Removed Footers: "+o]})}},V=class extends U{constructor(){super("Vertical to Horizontal Text")}transform(t){var e=0;return t.pages.forEach(t=>{const s=new z;s.consumeAll(t.items),t.items=s.complete(),e+=s.foundVerticals}),new I({...t,messages:["Converted "+e+" verticals"]})}},z=class extends F{foundVerticals;constructor(){super(),this.foundVerticals=0}shouldStash(t){return 1===t.words.length&&1===t.words[0].string.length}doMatchesStash(t,e){return t.y-e.y>5&&t.words[0].type===e.words[0].type}doFlushStash(t,e){if(t.length>5){var s=[],n=999,r=0,o=0,a=0;t.forEach(t=>{t.annotation=E,e.push(t),s.push(t.words[0]),n=Math.min(n,t.x),r=Math.max(r,t.y),o+=t.width,a=Math.max(a,t.height)}),e.push(new k({...t[0],x:n,y:r,width:o,height:a,words:s,annotation:T})),this.foundVerticals++}else e.push(...t)}},J=class{headlineCharCodes;stackedLineItems;stackedChars;constructor(t){this.headlineCharCodes=o(t.headline),this.stackedLineItems=[],this.stackedChars=0}consume(t){const e=o(t.text());if(this.matchAll(e)){if(this.stackedLineItems.push(t),this.stackedChars+=e.length,this.stackedChars===this.headlineCharCodes.length)return this.stackedLineItems}else this.stackedChars>0&&(this.stackedChars=0,this.stackedLineItems=[],this.consume(t));return null}matchAll(t){for(var e=0;e<t.length;e++){const s=this.headlineCharCodes[this.stackedChars+e];if(t[e]!==s)return!1}return!0}},K=class extends U{constructor(){super("Detect TOC")}transform(t){const e=[],o=Math.min(20,t.pages.length),a=new G;var i,c,h=[];t.pages.slice(0,o).forEach(t=>{var o=0;const l=new Set,m=[];var p,u;if(t.items.filter(t=>t.words.includes(t=>r(t.string,"."))).forEach(t=>{var e=t.words.filter(t=>!r(t.string,"."));const a=[];for(;e.length>0&&n(e[e.length-1].string);){const t=e.pop();a.unshift(t.string)}if(0===a.length&&e.length>0){const t=e[e.length-1];for(;s(t.string.charCodeAt(t.string.length-1));)a.unshift(t.string.charAt(t.string.length-1)),t.string=t.string.substring(0,t.string.length-1)}var i=a.length>0;i?(i=!0,p&&(e.push(...p),p=null),m.push(new Z({pageNumber:parseInt(a.join("")),lineItem:new k({...t,words:e})})),o++):c?(p&&l.add(u),p=e,u=t):c=t}),100*o/t.items.length>75){e.push(t.index+1),i=t,a.levelPageItems(m),h.push(...m);const s=[];t.items.forEach(t=>{l.has(t)||(t.annotation=E),s.push(t),t===c&&s.push(new k({...t,type:j.H2,annotation:T}))}),t.items=s}else c=null});var m=h.length;const p=[],u=[],d={};if(e.length>0){h.forEach(t=>{i.items.push(new k({words:[new C({string:" ".repeat(3*t.level)+"-"})].concat(t.lineItem.words),type:j.TOC,annotation:T}))});const e=function(t,e){for(var s of e){const e=Q(t,s.lineItem.text());if(e)return e.index-s.pageNumber}return null}(t.pages.filter(t=>t.index>i.index),h);h.forEach(s=>{var n,r=t.pages[s.pageNumber+e];r&&((n=X(r,s.lineItem.text()))||(r=t.pages[s.pageNumber+e+1])&&(n=X(r,s.lineItem.text()))),n?function(t,e,s,n){s.headlineItems.forEach(t=>t.annotation=E);const r=j.headlineByLevel(e.level+2),o=s.headlineItems.reduce((t,e)=>Math.max(t,e.height),0);t.items.splice(s.lineIndex+1,0,new k({...s.headlineItems[0],words:e.lineItem.words,height:o,type:r,annotation:T}));var a=n[r.name];a?(a.min=Math.min(a.min,o),a.max=Math.max(a.max,o)):(a={min:o,max:o},n[r.name]=a)}(r,s,n,d):p.push(s)});var f=i.index+2,g=[];const s=e=>{g.length>0&&(g.forEach(s=>{const n=j.headlineByLevel(s.level+2),r=d[n.name];if(r){const[o,a]=function(t,e,s,n,r){const o=e.lineItem.text().toUpperCase();for(var a=n;a<=r;a++){const e=t[a-1];if(e){const t=e.items.findIndex(t=>!t.type&&!t.annotation&&t.height>=s.min&&t.height<=s.max&&l(o,t.text())>=.5);if(t>-1)return[a-1,t]}}return[-1,-1]}(t.pages,s,r,f,e);if(a>-1){const e=t.pages[o];e.items[a].annotation=E,e.items.splice(a+1,0,new k({...s.lineItem,type:n,annotation:T})),u.push(s)}}}),g=[])};p.length>0&&(h.forEach(t=>{p.includes(t)?g.push(t):(s(t.pageNumber),f=t.pageNumber)}),g.length>0&&s(t.pages.length))}const y=[];return y.push("Detected "+e.length+" table of content pages"),e.length>0&&(y.push("TOC headline heights: "+JSON.stringify(d)),y.push("Found TOC headlines: "+(m-p.length+u.length)+"/"+m)),p.length>0&&(y.push("Found TOC headlines (by size): "+u.map(t=>t.lineItem.text())),y.push("Missing TOC headlines: "+p.filter(t=>!u.includes(t)).map(t=>t.lineItem.text()+"=>"+t.pageNumber))),new I({...t,globals:{...t.globals,tocPages:e,headlineTypeToHeightRange:d},messages:y})}};function Q(t,e){for(var s of t)if(X(s,e))return s;return null}function X(t,e){const s=new J({headline:e});var n=0;for(var r of t.items){const t=s.consume(r);if(t)return{lineIndex:n,headlineItems:t};n++}return null}var G=class{levelByMethod;uniqueFonts;constructor(){this.levelByMethod=null,this.uniqueFonts=[]}levelPageItems(t){if(!this.levelByMethod)if(this.calculateUniqueX(t).length>1)this.levelByMethod=this.levelByXDiff;else{const e=this.calculateUniqueFonts(t);e.length>1?(this.uniqueFonts=e,this.levelByMethod=this.levelByFont):this.levelByMethod=this.levelToZero}this.levelByMethod(t)}levelByXDiff(t){const e=this.calculateUniqueX(t);t.forEach(t=>{t.level=e.indexOf(t.lineItem.x)})}levelByFont(t){t.forEach(t=>{t.level=this.uniqueFonts.indexOf(t.lineItem.font)})}levelToZero(t){t.forEach(t=>{t.level=0})}calculateUniqueX(t){var e=t.reduce(function(t,e){return t.indexOf(e.lineItem.x)<0&&t.push(e.lineItem.x),t},[]);return e.sort((t,e)=>t-e),e}calculateUniqueFonts(t){return t.reduce(function(t,e){return t.indexOf(e.lineItem.font)<0&&t.push(e.lineItem.font),t},[])}},Z=class{lineItem;pageNumber;level;constructor(t){this.lineItem=t.lineItem,this.pageNumber=t.pageNumber,this.level=0}},tt=class extends U{constructor(){super("Detect List Items")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{const n=[];t.items.forEach(t=>{if(n.push(t),!t.type){var r=t.text();if(a(t.words[0].string))if(e++,"-"===t.words[0].string)t.annotation=O,t.type=j.LIST;else{t.annotation=E;const e=t.words.map(t=>new C({...t}));e[0].string="-",n.push(new k({...t,words:e,annotation:T,type:j.LIST}))}else/^[\s]*[\d]*[.][\s].*$/g.test(r)&&(s++,t.annotation=O,t.type=j.LIST)}}),t.items=n}),new I({...t,messages:["Detected "+e+" plain list items.","Detected "+s+" numbered list items."]})}},et=class extends U{constructor(){super("Detect Headers")}transform(t){const{tocPages:e,headlineTypeToHeightRange:s,mostUsedHeight:n,mostUsedDistance:r,mostUsedFont:o,maxHeight:a}=t.globals,c=e.length>0;var l=0;const h=function(t,e){const s=new Set;return t.forEach(t=>{t.items.forEach(n=>{n.type||n.height!==e||s.add(t)})}),s}(t.pages,a),m=n+(a-n)/4;if(h.forEach(t=>{t.items.forEach(t=>{const e=t.height;!t.type&&e>m&&(t.type=e===a?j.H1:j.H2,t.annotation=O,l++)})}),c)Object.keys(s).forEach(e=>{var r=s[e];r.max>n&&t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==r.max||(t.annotation=O,t.type=j.enumValueOf(e),l++)})})});else{const e=[];t.pages.forEach(t=>{t.items.forEach(t=>{!t.type&&t.height>n&&!i(t.text())&&(e.includes(t.height)||e.push(t.height))})}),e.sort((t,e)=>e-t),e.forEach((e,s)=>{if(s+2<=6){const n=j.headlineByLevel(2+s);t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==e||i(t.text())||(l++,t.annotation=O,t.type=n)})})}})}var p=1;if(t.pages.forEach(t=>{t.items.forEach(t=>{t.type&&t.type.headline&&(p=Math.max(p,t.type.headlineLevel))})}),p<6){const e=j.headlineByLevel(p+1);t.pages.forEach(t=>{var s;t.items.forEach(t=>{!t.type&&t.height===n&&t.font!==o&&(!s||s.y<t.y||s.type&&s.type.headline||s.y-t.y>2*r)&&t.text()===t.text().toUpperCase()&&(l++,t.annotation=O,t.type=e),s=t})})}return new I({...t,messages:["Detected "+l+" headlines."]})}};var st=class extends f{items;constructor(t){super(t),this.items=[],t.items&&t.items.forEach(t=>this.addItem(t))}addItem(t){if(this.type&&t.type&&this.type!==t.type)throw new Error(`Adding item of type ${t.type.name} to block of type ${this.type.name}`);this.type||(this.type=t.type),t.parsedElements&&(this.parsedElements?this.parsedElements.add(t.parsedElements):this.parsedElements=t.parsedElements);const e=new k({...t});e.type=null,this.items.push(e)}},nt=class t extends x{constructor(e){if(super(e,st.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==E),t.items.forEach(t=>t.annotation=null)}),t}},rt=class extends nt{constructor(){super("Gather Blocks")}transform(t){const{mostUsedDistance:e}=t.globals;var s=0,n=0;return t.pages.map(t=>{n+=t.items.length;const r=[];var o=new st({});const a=()=>{o.items.length>1&&(o.annotation=O),r.push(o),o=new st({}),s++};var i=function(t){var e=999;return t.forEach(t=>{e=Math.min(e,t.x)}),999===e?null:e}(t.items);t.items.forEach(t=>{o.items.length>0&&function(t,e,s,n){if(t.type&&t.type.mergeFollowingNonTypedItems&&!e.type)return!1;const r=t.items[t.items.length-1],o=function(t,e,s,n){const r=t.y-e.y;if(r<0-n/2)return!0;var o=n+1;t.x>s&&e.x>s&&(o=n+n/2);return r>o}(r,e,s,n);if(t.type&&t.type.mergeFollowingNonTypedItemsWithSmallDistance&&!e.type&&!o)return!1;if(e.type!==t.type)return!0;return e.type?!e.type.mergeToBlock:o}(o,t,i,e)&&a(),o.addItem(t)}),o.items.length>0&&a(),t.items=r}),new I({...t,messages:["Gathered "+s+" blocks out of "+n+" line items"]})}};var ot=class extends nt{constructor(){super("$1")}transform(t){const e=t.globals.mostUsedHeight??0;var s=0;return t.pages.forEach(t=>{var n=function(t){var e=999;return t.forEach(t=>{t.items.forEach(t=>{e=Math.min(e,t.x)})}),999===e?null:e}(t.items);t.items.forEach(t=>{!t.type&&function(t,e,s){if(0===e.length)return!1;if(1===e.length)return e[0].x>t&&e[0].height<=s+1;for(var n of e)if(n.x===t)return!1;return!0}(n,t.items,e)&&(t.annotation=O,t.type=j.CODE,s++)})}),new I({...t,messages:["Detected "+s+" code/quote items."]})}};var at,it=class extends nt{constructor(){super("Level Lists")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{t.items.filter(t=>t.type===j.LIST).forEach(t=>{var n,r=0;const o={};var a=!1;t.items.forEach(t=>{n?t.x>n?(r++,o[t.x]=r):t.x<n&&(r=o[t.x]):o[t.x]=0,r>0&&(t.words=[new C({string:" ".repeat(3*r)})].concat(t.words),a=!0),n=t.x}),e++,a?(s++,t.annotation=L):t.annotation=b})}),new I({...t,messages:["Modified "+s+" / "+e+" list blocks."]})}},ct=class extends x{constructor(){super("To Text Blocks","TextBlock")}transform(t){return t.pages.forEach(t=>{const e=[];t.items.forEach(t=>{const s=t.type?t.type.name:"Unknown";e.push({category:s,text:j.blockToText(t)})}),t.items=e}),new I({...t})}},lt=class extends x{constructor(){super("To HTML","String")}transform(t){return t.pages.forEach(t=>{var e="";t.items.forEach(t=>{let s;s="TOC"===t.category?t.text:t.text.replace(/(\r\n|\n|\r)/gm,"\n"),"LIST"!==t.category&&(s=s.split("- ").join("")),"CODE"===t.category&&(s=s.split("<code>").join("").split("</code>").join("")),e+=`<p>${s}</p>\n\n`}),t.items=[e]}),new I({...t})}};async function ht(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{({LiteParse:a}=await(import("@llamaindex/liteparse")))}catch(u){return{error:`method: 'liteparse' requires the optional @llamaindex/liteparse dependency (Node.js only): ${u.message}`}}const i=new a({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(u){return{error:u.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,p;return n&&(p=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:p,title:m,html:h,format:"pdf"}}async function mt(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{a=await(import("@llamaindex/liteparse-wasm")),await async function(t){at||(at=t.default()),await at}(a)}catch(u){return{error:`method: 'liteparse-wasm' requires the optional @llamaindex/liteparse-wasm dependency: ${u.message}`}}const i=new a.LiteParse({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(u){return{error:u.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,p;return n&&(p=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:p,title:m,html:h,format:"pdf"}}var pt=null;async function ut(){return pt||(pt=import("https://cdn.jsdelivr.net/npm/pdfjs-serverless@1/+esm").catch(()=>import("pdfjs-serverless")).then(({resolvePDFJS:t})=>t())),pt}var dt=/\b(?:fig(?:ure)?s?|table|chart|graph|diagram|infographic|exhibit|illustration|plate|scheme)\s*\.?\s*(?:\d+(?:\.\d+)*|[ivxlcdm]+)\b/gi,ft=/^\s*\S[^\t\n]{0,60}?(?:\t+| {2,})\S[^\t\n]{0,60}?(?:\t+| {2,})\S/,gt=/(?:[-+]?[\d][\d.,]*\s*[%$€£]?\s+){3,}[-+]?[\d]/,yt=/[\uFFFD\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function wt(t,e={}){const{sparseTextThreshold:s=200,minTableRows:n=3,minNumericRows:r=2}=e,o=t.map((t,e)=>{const o=[],a=(t||"").replace(/\s+/g," ").trim();0===a.length?o.push("no-text"):a.length<s&&o.push("sparse-text");const i=(t||"").match(yt)?.length||0;a.length>0&&i/a.length>.05&&o.push("garbled-text");const c=[...(t||"").matchAll(dt)].map(t=>t[0].replace(/\s+/g," ").trim()),l=[...(t||"").matchAll(new RegExp(`^\\s*${dt.source}`,"gim"))].map(t=>t[0].trim());l.some(t=>/^table/i.test(t))&&o.push("table-caption"),l.some(t=>!/^table/i.test(t))&&o.push("figure-caption");const h=(t||"").split(/\r?\n/),m=h.filter(t=>ft.test(t)).length,p=h.filter(t=>gt.test(t)).length;return m>=n&&o.push("table-rows"),p>=r&&o.push("numeric-grid"),{page:e+1,needsOcr:o.length>0,reasons:o,captions:c}}),a=o.filter(t=>t.needsOcr).map(t=>t.page);return{needsOcr:a.length>0,pagesNeedingOcr:a,pages:o}}var xt="onnx-community/granite-docling-258M-ONNX",vt="Convert this page to docling.",Tt=null;async function Et(t,e={}){const{processorUrl:s,prompt:n=vt,maxTokens:r=4096}=e;if(s){const e=await fetch(`${s.replace(/\/$/,"")}/api/v1/convert-base64`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({imageBase64:t,mimeType:"image/png",prompt:n,maxTokens:r})}),o=await e.json();if(!e.ok||!o?.success)throw new Error(o?.error||`Processor error ${e.status}`);return o.result}const{model:o,processor:a,lib:i}=await async function(){return Tt||(Tt=(async()=>{const t=await(import("@huggingface/transformers")),e=await t.AutoProcessor.from_pretrained(xt);return{model:await t.AutoModelForVision2Seq.from_pretrained(xt,{dtype:"fp32"}),processor:e,lib:t}})()),Tt}(),c=await i.load_image(`data:image/png;base64,${t}`),l=[{role:"user",content:[{type:"image"},{type:"text",text:n}]}],h=await a(a.apply_chat_template(l,{add_generation_prompt:!0}),[c],{do_image_splitting:!0}),m=await o.generate({...h,max_new_tokens:r}),[p]=a.batch_decode(m.slice(null,[h.input_ids.dims.at(-1),null]),{skip_special_tokens:!0});return p}async function bt(t,e=2){const s=t.getViewport({scale:e}),n=await async function(t,e){if("undefined"!=typeof document){const s=document.createElement("canvas");return s.width=t,s.height=e,s}try{return(await(import("@napi-rs/canvas"))).createCanvas(t,e)}catch{}if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(t,e);throw new Error("No canvas available to rasterize PDF pages — install @napi-rs/canvas in Node.js, or run in a browser/Worker with OffscreenCanvas")}(Math.ceil(s.width),Math.ceil(s.height)),r=n.getContext("2d");return await t.render({canvasContext:r,viewport:s}).promise,async function(t){if("function"==typeof t.toBuffer)return t.toBuffer("image/png").toString("base64");if("function"==typeof t.convertToBlob){const e=await t.convertToBlob({type:"image/png"}),s=new Uint8Array(await e.arrayBuffer());let n="";for(const t of s)n+=String.fromCharCode(t);return btoa(n)}return t.toDataURL("image/png").split(",")[1]}(n)}async function Ot(t,e,s={}){const n=new Map;for(const o of e)try{const e=await Et(await bt(await t.getPage(o),s.scale),s);n.set(o,Lt(e))}catch(r){console.error(`Docling OCR failed for page ${o}:`,r)}return n}function Lt(t){let e=(t||"").replace(/<\/?doctag>/g,"").replace(/<loc_\d+>/g,"").replace(/<page_break>/g,"").replace(/<end_of_utterance>/g,"").replace(/<page_(?:header|footer)>[\s\S]*?<\/page_(?:header|footer)>/g,"");e=e.replace(/<otsl>([\s\S]*?)<\/otsl>/g,(t,e)=>function(t){return`<table>${t.split("<nl>").map(t=>t.trim()).filter(Boolean).map(t=>{const e=[...t.matchAll(/<(fcel|ched|rhed|ecel|lcel|ucel|xcel)>([^<]*)/g)];return e.length?`<tr>${e.map(([,t,e])=>{const s="ched"===t||"rhed"===t?"th":"td";return`<${s}>${e.trim()}</${s}>`}).join("")}</tr>`:""}).filter(Boolean).join("")}</table>`}(e));for(const[s,n,r]of[[/<title>([\s\S]*?)<\/title>/g,"<h1>","</h1>"],[/<section_header_level_1>([\s\S]*?)<\/section_header_level_1>/g,"<h2>","</h2>"],[/<section_header_level_2>([\s\S]*?)<\/section_header_level_2>/g,"<h3>","</h3>"],[/<section_header_level_3>([\s\S]*?)<\/section_header_level_3>/g,"<h4>","</h4>"],[/<section_header_level_[4-9]>([\s\S]*?)<\/section_header_level_[4-9]>/g,"<h5>","</h5>"],[/<(?:text|paragraph)>([\s\S]*?)<\/(?:text|paragraph)>/g,"<p>","</p>"],[/<caption>([\s\S]*?)<\/caption>/g,"<figcaption>","</figcaption>"],[/<(?:picture|chart)>([\s\S]*?)<\/(?:picture|chart)>/g,"<figure>","</figure>"],[/<code>([\s\S]*?)<\/code>/g,"<pre><code>","</code></pre>"],[/<formula>([\s\S]*?)<\/formula>/g,'<code class="formula">',"</code>"],[/<footnote>([\s\S]*?)<\/footnote>/g,'<p class="footnote">',"</p>"],[/<list_item>([\s\S]*?)<\/list_item>/g,"<li>","</li>"],[/<unordered_list>([\s\S]*?)<\/unordered_list>/g,"<ul>","</ul>"],[/<ordered_list>([\s\S]*?)<\/ordered_list>/g,"<ol>","</ol>"]])e=e.replace(s,(t,e)=>`${n}${e.trim()}${r}`);return e=e.replace(/<\/?(?:[a-z][a-z0-9]*_[a-z0-9_]+|otsl|smiles)>/g,""),e.trim()}exports.convertPDFToHTML=async function(t,e={}){if("liteparse"===e.method){const{method:s,...n}=e;return ht(t,n)}if("liteparse-wasm"===e.method){const{method:s,...n}=e;return mt(t,n)}var{addPageNumbers:s=!1,addCitation:n=!0}=e;const r=e.processor??"frontend",o=/^https?:\/\//i.test(r),a=o?"docling":r,i={...e.doclingOptions,processorUrl:o?r:e.processorUrl};var c="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let l;try{const{getDocument:t}=await ut();l=await t({data:new Uint8Array(c),useSystemFonts:!0,verbosity:0}).promise}catch(k){return{error:k.message}}const h=[...Array(l.numPages).keys()].map(t=>new y({index:t}));let m,f={};for(let d=1;d<=l.numPages;d++){const t=await l.getPage(d),e=await t.getTextContent();if(!(Object.keys(f).length<10)){m=u(f);break}f=p(f,t.pageNumber-1,e.items)}let x=m?m.pageNum:0;for(let p=1;p<=l.numPages;p++){const t=await l.getPage(p);await t.getOperatorList();const e=t.getViewport({scale:1});let s=await t.getTextContent();m&&t.pageIndex>=m.pageIndex&&(s=d(s,x),x++);const n=s.items.map(t=>{const s=[1,0,0,1,0,0];for(let o=0;o<6;o++)s[o]+=t.transform[o]*e.transform[o%2?3:0],o%2&&(s[o+1]+=t.transform[o]*e.transform[1]);const n=Math.sqrt(s[2]*s[2]+s[3]*s[3]),r=t.height/n;return new g({x:Math.round(t.transform[4]),y:Math.round(t.transform[5]),width:Math.round(t.width),height:Math.round(r<=1?t.height:r),text:t.str,font:t.fontName})});h[t.pageNumber-1].items=n}const v=h.map(t=>Object.values(t.items.reduce((t,e)=>((t[e.y]=t[e.y]||[]).push(e.text),t),{})).map(t=>t.join(" ")).join("\n"));var T=new I({pages:h});let E;[new P,new q,new Y,new V,new K,new et,new tt,new rt,new ot,new it,new ct,new lt].forEach(t=>{E&&(T=E.completeTransform(T)),T=t.transform(T),E=t});var b=T.pages.map((t,e)=>`<p id="page-${e+1}">${s?` [${e+1}] `:""}${t.items.join('</p><p id="page-'+e+'">')}</p>`);const O=wt(v,e.ocrScanOptions);if("docling"===a||"hybrid"===a){const t="docling"===a?b.map((t,e)=>e+1):O.pagesNeedingOcr,e=await Ot(l,t,i);for(const[n,r]of e)b[n-1]=`<section class="ocr-page" id="page-${n}">${s?` [${n}] `:""}${r}</section>`}var L=b.join("");if(n){var{Author:S,Title:C}=(await l.getMetadata()).info;C=L.slice(0,400).match(/<h[0-9]>(.*?)<\/h[0-9]>/)?.[1]||C}return{author:S,title:C,html:L,format:"pdf",processor:a,ocrScan:O}},exports.convertPDFToHTMLWithLiteParse=ht,exports.convertPDFToHTMLWithLiteParseWasm=mt,exports.detectPdfNeedsOcr=async function(t,e={}){const s="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t,{LiteParse:n}=await(import("@llamaindex/liteparse")),r=await new n({quiet:!0,...e.liteParseOptions}).isComplex(new Uint8Array(s));return{needsOcr:r.some(t=>t.needsOcr),pages:r,reasons:[...new Set(r.flatMap(t=>t.reasons))]}},exports.doctagsToHtml=Lt,exports.loadPdfJs=ut,exports.ocrImageWithDocling=Et,exports.ocrPdfPagesWithDocling=Ot,exports.renderPdfPageToPngBase64=bt,exports.scanPagesForOCR=wt;
|
package/dist/pdf-to-html.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function t(t){for(;32===t.charCodeAt(0);)t=t.substring(1,t.length);return t}function e(t){for(;32===t.charCodeAt(t.length-1);)t=t.substring(0,t.length-1);return t}function s(t){return t>=48&&t<=57}function n(t){for(var e=0;e<t.length;e++)if(!s(t.charCodeAt(e)))return!1;return!0}function r(t,e){const s=e.charCodeAt(0);for(var n=0;n<t.length;n++)if(t.charCodeAt(n)!==s)return!1;return!0}function o(t){return function(t){const e=[];for(var s=0;s<t.length;s++)e.push(t.charCodeAt(s));return e}(t=t.toUpperCase()).filter(t=>32!==t&&9!==t&&46!==t)}function a(t){if(t.length>1)return!1;const e=t.charAt(0);return"-"===e||"•"===e||"–"===e}function i(t){return/^[\s]*[-•–][\s].*$/g.test(t)}function c(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function l(t,e){const s=new Set(t.toUpperCase().split(" ")),n=new Set(e.toUpperCase().split(" "));return new Set([...s].filter(t=>n.has(t))).size/Math.max(s.size,n.size)}var h=(t,e,s)=>Math.floor(t/e*s),m=(s,r,o)=>{for(const{str:a}of s){const s=e(t(a));n(s)&&(r[o]||(r[o]=[]),r[o].push(Number(s)))}return r},u=(t,e,s)=>{const n=h(1,6,s.length),r=h(5,6,s.length),o=m(s.slice(0,n),t,e);return m(s.slice(r),o,e)},p=t=>{let e=0;const s=Object.keys(t);if(0!==s.length&&1!==s.length)for(let n=0;n<s.length-1;n++){const r=t[s[n]],o=t[s[n+1]],a=e;for(let t=0;t<r.length&&e<2;t++)for(let a=0;a<o.length&&e<2;a++){const i=Number(s[n+1])-Number(s[n]);(r[t]+1===o[a]||i>1&&r[t]+i===o[a])&&e++}let i=n>0?Object.entries(t)[n-1]:Object.entries(t)[n];if(a===e)e=0,i=Object.entries(t)[n];else if(e>=2)return{pageIndex:Number(i[0]),pageNum:i[1][0]}}},d=(t,e)=>{const s={items:[...t.items]},n=h(1,6,s.items.length),r=h(5,6,s.items.length);return s.items=s.items.filter((t,o)=>{const a=o>0&&o<n,i=o>r&&o<s.items.length;return a||i?Number(t.str)!==Number(e):t}),s},f=class t{type;annotation;parsedElements;constructor(e){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.type=e.type??null,this.annotation=e.annotation??null,this.parsedElements=e.parsedElements??null}},g=class extends f{x;y;width;height;text;font;lineFormat;unopenedFormat;unclosedFormat;constructor(t){super(t),this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,this.text=t.text,this.font=t.font,this.lineFormat=t.lineFormat??null,this.unopenedFormat=t.unopenedFormat??null,this.unclosedFormat=t.unclosedFormat??null}},y=class{index;items;constructor(t){this.index=t.index,this.items=t.items||[]}};async function w(t,e={}){const s=e.timeout?1e3*e.timeout:1e4,n=new AbortController,r=setTimeout(()=>n.abort(),s);try{const s=await fetch(t,{signal:n.signal});if(clearTimeout(r),!s.ok)throw new Error(`HTTP ${s.status}`);return"arraybuffer"===e.responseType?await s.arrayBuffer():await s.text()}catch(o){throw clearTimeout(r),o}}var x=class t{name;itemType;constructor(e,s){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");if(this.transform===t.prototype.transform)throw new TypeError("Please implement abstract method 'transform()'.");this.name=e,this.itemType=s}transform(t){throw new TypeError("Do not call abstract method foo from child.")}completeTransform(t){return t.messages=[],t}},v=class{category;color;constructor(t){this.category=t.category,this.color=t.color}},E=new v({category:"Added",color:"green"}),T=new v({category:"Removed",color:"red"}),b=new v({category:"Unchanged",color:"brown"}),O=new v({category:"Detected",color:"green"}),S=new v({category:"Modified",color:"green"}),L=class t extends x{constructor(e){if(super(e,g.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},I=class{pages;globals;messages;constructor(t){this.pages=t.pages||[],this.globals=t.globals||{},this.messages=t.messages||[]}},C=class{string;type;format;constructor(t){this.string=t.string,this.type=t.type??null,this.format=t.format??null}},k=class extends f{x;y;width;height;words;constructor(t){super(t),this.x=t.x??0,this.y=t.y??0,this.width=t.width??0,this.height=t.height??0,this.words=t.words||[],t.text&&!t.words&&(this.words=t.text.split(" ").filter(t=>t.trim().length>0).map(t=>new C({string:t})))}text(){return this.wordStrings().join(" ")}wordStrings(){return this.words.map(t=>t.string)}},N=class t{results;stash;constructor(){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.results=[],this.stash=[]}consumeAll(t){t.forEach(t=>this.consume(t))}consume(t){this.shouldStash(t)?(this.matchesStash(t)||this.flushStash(),this.pushOnStash(t)):(this.stash.length>0&&this.flushStash(),this.results.push(t))}pushOnStash(t){this.onPushOnStash(t),this.stash.push(t)}complete(){return this.stash.length>0&&this.flushStash(),this.results}matchesStash(t){if(0===this.stash.length)return!0;const e=this.stash[this.stash.length-1];return this.doMatchesStash(e,t)}flushStash(){this.stash.length>0&&(this.doFlushStash(this.stash,this.results),this.stash=[])}onPushOnStash(t){}shouldStash(t){throw new TypeError(" Do not call abstract method foo from child."+t)}doMatchesStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}doFlushStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}},F=class{footnoteLinks;footnotes;containLinks;formattedWords;constructor(t){this.footnoteLinks=t.footnoteLinks||[],this.footnotes=t.footnotes||[],this.containLinks=t.containLinks??!1,this.formattedWords=t.formattedWords??0}add(t){this.footnoteLinks=this.footnoteLinks.concat(t.footnoteLinks),this.footnotes=this.footnotes.concat(t.footnotes),this.containLinks=this.containLinks||t.containLinks,this.formattedWords+=t.formattedWords}};function _(t){t.sort((t,e)=>t.x-e.x)}var $={BOLD:{name:"BOLD",startSymbol:"<strong>",endSymbol:"</strong>"},OBLIQUE:{name:"OBLIQUE",startSymbol:"<em>",endSymbol:"</em>"},BOLD_OBLIQUE:{name:"BOLD_OBLIQUE",startSymbol:"<strong><em>",endSymbol:"</em></strong>"}},B={LINK:{name:"LINK",toText:t=>`<a href="${t}">${t}</a>`},FOOTNOTE_LINK:{name:"FOOTNOTE_LINK",attachWithoutWhitespace:!0,plainTextFormat:!0,toText:t=>`<sup><a href="#${t}">${t}</a></sup>`},FOOTNOTE:{name:"FOOTNOTE",toText:t=>`<p id="${t}">^${t}</p>`}},M=class{fontToFormats;constructor(t){this.fontToFormats=t}compact(t){_(t);const e=new D(this.fontToFormats);e.consumeAll(t.map(t=>new g({...t})));const s=e.complete();var n=0,r=0;return t.forEach(t=>{n=Math.max(n,t.height),r+=t.width}),new k({x:t[0].x,y:t[0].y,height:n,width:r,words:s,parsedElements:new F({footnoteLinks:e.footnoteLinks,footnotes:e.footnotes,containLinks:e.containLinks,formattedWords:e.formattedWords})})}},D=class extends N{fontToFormats;footnoteLinks;footnotes;formattedWords;containLinks;stashedNumber;firstY;currentItem;constructor(t){super(),this.fontToFormats=t,this.footnoteLinks=[],this.footnotes=[],this.formattedWords=0,this.containLinks=!1,this.stashedNumber=!1,this.currentItem=null}shouldStash(t){return this.firstY||(this.firstY=t.y),this.currentItem=t,!0}onPushOnStash(t){this.stashedNumber=n(t.text.trim())}doMatchesStash(t,e){if(this.fontToFormats.get(t.font)!==this.fontToFormats.get(e.font))return!1;const s=n(e.text.trim());return this.stashedNumber===s}doFlushStash(t,e){if(this.stashedNumber){const s=t.map(t=>t.text).join("").trim();t[0].y>this.firstY?(e.push(new C({string:`${s}`,type:B.FOOTNOTE_LINK})),this.footnoteLinks.push(parseInt(s))):this.currentItem&&this.currentItem.y<t[0].y?(e.push(new C({string:`${s}`,type:B.FOOTNOTE})),this.footnotes.push(s)):this.copyStashItemsAsText(t,e)}else this.copyStashItemsAsText(t,e)}copyStashItemsAsText(t,e){const s=this.fontToFormats.get(t[0].font);e.push(...this.itemsToWords(t,s))}itemsToWords(t,e){const s=(n=t,r="",o=null,n.forEach(t=>{var e=t.text;r.endsWith(" ")||e.startsWith(" ")||(o?t.x-o.x-o.width>5&&(r+=" "):a(t.text)&&(e+=" ")),r+=e,o=t}),r).split(" ");var n,r,o;const i=e?$[e]:null;return s.filter(t=>t.trim().length>0).map(t=>{var e=null;return t.startsWith("http:")?(this.containLinks=!0,e=B.LINK):t.startsWith("www.")&&(this.containLinks=!0,t=`http://${t}`,e=B.LINK),i&&this.formattedWords++,new C({string:t,type:e,format:i})})}};var U=class extends L{fontMap;constructor(t){super("Calculate Global Stats"),this.fontMap=t}transform(t){const e={},s={};var n,r=0;t.pages.forEach(t=>{t.items.forEach(t=>{t.height&&(e[t.height]=e[t.height]?e[t.height]+1:1,s[t.font]=s[t.font]?s[t.font]+1:1,t.height>r&&(r=t.height,n=t.font))})});const o=parseInt(H(e)),a=H(s),i={};t.pages.forEach(t=>{var e;t.items.forEach(t=>{if(t.height===o&&t.text.trim().length>0){if(e&&t.y!==e.y){const s=e.y-t.y;s>0&&(i[s]=i[s]?i[s]+1:1)}e=t}else e=null})});const c=parseInt(H(i)),l=[],h=/* @__PURE__ */new Map;this.fontMap?.forEach(function(t,e){l.push(e+" = "+t.name);const s=t.name.toLowerCase();var r=null;e===a?r=null:s.includes("bold")&&(s.includes("oblique")||s.includes("italic"))?r=$.BOLD_OBLIQUE:s.includes("bold")?r=$.BOLD:s.includes("oblique")||s.includes("italic")?r=$.OBLIQUE:s===n&&(r=$.BOLD),r&&h.set(e,r.name)}),l.sort();const m=t.pages.map(t=>({...t,items:t.items.map(t=>({...t}))}));return new I({...t,pages:m,globals:{mostUsedHeight:o,mostUsedFont:a,mostUsedDistance:c,maxHeight:r,maxHeightFont:n,fontToFormats:h},messages:["Items per height: "+JSON.stringify(e),"Items per font: "+JSON.stringify(s),"Items per distance: "+JSON.stringify(i),"Fonts:"+JSON.stringify(l)]})}};function H(t){var e=0,s="";return Object.keys(t).map(n=>{(!s||t[n]>e)&&(e=t[n],s=n)}),s}var A=class t extends x{constructor(e){if(super(e,k.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},P=class{mostUsedDistance;constructor(t){this.mostUsedDistance=t.mostUsedDistance||12}group(t){const e=[];var s=[];return t.forEach(t=>{s.length>0&&Math.abs(s[0].y-t.y)>=this.mostUsedDistance/2&&(e.push(s),s=[]),s.push(t)}),e.push(s),e.forEach(t=>{_(t)}),e}},j={H1:{name:"H1",headline:!0,headlineLevel:1,toText:t=>"<h1>"+W(t.items,!0)+"</h1>"},H2:{name:"H2",headline:!0,headlineLevel:2,toText:t=>"<h2>"+W(t.items,!0)+"</h2>"},H3:{name:"H3",headline:!0,headlineLevel:3,toText:t=>"<h3>"+W(t.items,!0)+"</h3>"},H4:{name:"H4",headline:!0,headlineLevel:4,toText:t=>"<h4>"+W(t.items,!0)+"</h4>"},H5:{name:"H5",headline:!0,headlineLevel:5,toText:t=>"<h5>"+W(t.items,!0)+"</h5>"},H6:{name:"H6",headline:!0,headlineLevel:6,toText:t=>"<h6>"+W(t.items,!0)+"</h6>"},TOC:{name:"TOC",mergeToBlock:!0,toText:t=>W(t.items,!0)},FOOTNOTES:{name:"FOOTNOTES",mergeToBlock:!0,mergeFollowingNonTypedItems:!0,toText:t=>"<p>"+W(t.items,!1)+"</p>"},CODE:{name:"CODE",mergeToBlock:!0,toText:t=>"<code>"+W(t.items,!0)+"</code>"},LIST:{name:"LIST",mergeToBlock:!1,mergeFollowingNonTypedItemsWithSmallDistance:!0,toText:t=>"<ul>\n"+W(t.items,!1)+"</ul>"},PARAGRAPH:{name:"PARAGRAPH",toText:t=>"<p>"+W(t.items,!1)+"</p>"}};function W(t,e){var s,n="";const r=()=>{n+=s.endSymbol,s=null};return t.forEach((o,a)=>{var i;o&&(o.words.forEach((t,o)=>{const a=t.type,i=t.format;var c;!s||i&&i===s||r(),o>0&&(!a||!a.attachWithoutWhitespace)&&(1!==(c=t.string).length||"."!==c[0]&&"!"!==c[0]&&"?"!==c[0])&&(n+=" "),!i||s||e||(n+=(s=i).startSymbol),!a||e&&!a.plainTextFormat?n+=t.string:n+=a.toText(t.string)}),!s||a!==t.length-1&&(0===(i=t[a+1]).words.length?null:i.words[0].format)===s||r(),n+="\n")}),n}j.enumValueOf=function(t){return j[t]},j.isHeadline=function(t){return t&&2===t.name.length&&"H"===t.name[0]},j.blockToText=function(t){return t.type?t.type.toText(t):W(t.items,!1)},j.headlineByLevel=function(t){return 1===t?j.H1:2===t?j.H2:3===t?j.H3:4===t?j.H4:5===t?j.H5:(t>6&&console.warn("Unsupported headline level: "+t+" (supported are 1-6), defaulting to level 6"),j.H6)};var q=class extends A{constructor(){super("Compact To Lines")}transform(t){const{mostUsedDistance:e,fontToFormats:s}=t.globals,n=[],r=[];var o=0,a=0;const i=new P({mostUsedDistance:e}),c=new M(s);return t.pages.forEach(t=>{if(t.items.length>0){const e=[];i.group(t.items).forEach(s=>{const i=c.compact(s);if(s.length>1&&(i.annotation=E,s.forEach(t=>{t.annotation=T,e.push(new k({...t}))})),0===i.words.length&&(i.annotation=T),e.push(i),i.parsedElements.formattedWords&&(a+=i.parsedElements.formattedWords),i.parsedElements.containLinks&&o++,i.parsedElements.footnoteLinks.length>0){const e=i.parsedElements.footnoteLinks.map(e=>({footnoteLink:e,page:t.index+1}));r.push.apply(r,e)}if(i.parsedElements.footnotes.length>0){i.type=j.FOOTNOTES;const e=i.parsedElements.footnotes.map(e=>({footnote:e,page:t.index+1}));n.push.apply(n,e)}}),t.items=e}}),new I({...t,messages:["Detected "+a+" formatted words","Found "+o+" links","Detected "+r.length+" footnotes links","Detected "+n.length+" footnotes"]})}};function R(t){var e=0;if(0===t.trim().length)return e;for(var n=0;n<t.length;n++){const r=t.charCodeAt(n);s(r)||32===r||160===r||(e=(e<<5)-e+r,e|=0)}return e}var Y=class extends A{constructor(){super("Remove Repetitive Elements")}transform(t){const e=[],s={},n={};t.pages.forEach(t=>{const r=t.items.reduce((t,e)=>(e.y<t.minY?(t.minElements=[e],t.minY=e.y):e.y===t.minY&&t.minElements.push(e),e.y>t.maxY?(t.maxElements=[e],t.maxY=e.y):e.y===t.maxY&&t.maxElements.push(e),t),{minY:999,maxY:0,minElements:[],maxElements:[]}),o=R(r.minElements.reduce((t,e)=>t+e.text().toUpperCase(),"")),a=R(r.maxElements.reduce((t,e)=>t+e.text().toUpperCase(),""));e.push({minElements:r.minElements,maxElements:r.maxElements,minLineHash:o,maxLineHash:a}),s[o]=s[o]?s[o]+1:1,n[a]=n[a]?n[a]+1:1});var r=0,o=0;return t.pages.forEach((a,i)=>{s[e[i].minLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].minElements.forEach(t=>{t.annotation=T}),o++),n[e[i].maxLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].maxElements.forEach(t=>{t.annotation=T}),r++)}),new I({...t,messages:["Removed Header: "+r,"Removed Footers: "+o]})}},V=class extends A{constructor(){super("Vertical to Horizontal Text")}transform(t){var e=0;return t.pages.forEach(t=>{const s=new z;s.consumeAll(t.items),t.items=s.complete(),e+=s.foundVerticals}),new I({...t,messages:["Converted "+e+" verticals"]})}},z=class extends N{foundVerticals;constructor(){super(),this.foundVerticals=0}shouldStash(t){return 1===t.words.length&&1===t.words[0].string.length}doMatchesStash(t,e){return t.y-e.y>5&&t.words[0].type===e.words[0].type}doFlushStash(t,e){if(t.length>5){var s=[],n=999,r=0,o=0,a=0;t.forEach(t=>{t.annotation=T,e.push(t),s.push(t.words[0]),n=Math.min(n,t.x),r=Math.max(r,t.y),o+=t.width,a=Math.max(a,t.height)}),e.push(new k({...t[0],x:n,y:r,width:o,height:a,words:s,annotation:E})),this.foundVerticals++}else e.push(...t)}},J=class{headlineCharCodes;stackedLineItems;stackedChars;constructor(t){this.headlineCharCodes=o(t.headline),this.stackedLineItems=[],this.stackedChars=0}consume(t){const e=o(t.text());if(this.matchAll(e)){if(this.stackedLineItems.push(t),this.stackedChars+=e.length,this.stackedChars===this.headlineCharCodes.length)return this.stackedLineItems}else this.stackedChars>0&&(this.stackedChars=0,this.stackedLineItems=[],this.consume(t));return null}matchAll(t){for(var e=0;e<t.length;e++){const s=this.headlineCharCodes[this.stackedChars+e];if(t[e]!==s)return!1}return!0}},K=class extends A{constructor(){super("Detect TOC")}transform(t){const e=[],o=Math.min(20,t.pages.length),a=new G;var i,c,h=[];t.pages.slice(0,o).forEach(t=>{var o=0;const l=/* @__PURE__ */new Set,m=[];var u,p;if(t.items.filter(t=>t.words.includes(t=>r(t.string,"."))).forEach(t=>{var e=t.words.filter(t=>!r(t.string,"."));const a=[];for(;e.length>0&&n(e[e.length-1].string);){const t=e.pop();a.unshift(t.string)}if(0===a.length&&e.length>0){const t=e[e.length-1];for(;s(t.string.charCodeAt(t.string.length-1));)a.unshift(t.string.charAt(t.string.length-1)),t.string=t.string.substring(0,t.string.length-1)}var i=a.length>0;i?(i=!0,u&&(e.push(...u),u=null),m.push(new Z({pageNumber:parseInt(a.join("")),lineItem:new k({...t,words:e})})),o++):c?(u&&l.add(p),u=e,p=t):c=t}),100*o/t.items.length>75){e.push(t.index+1),i=t,a.levelPageItems(m),h.push(...m);const s=[];t.items.forEach(t=>{l.has(t)||(t.annotation=T),s.push(t),t===c&&s.push(new k({...t,type:j.H2,annotation:E}))}),t.items=s}else c=null});var m=h.length;const u=[],p=[],d={};if(e.length>0){h.forEach(t=>{i.items.push(new k({words:[new C({string:" ".repeat(3*t.level)+"-"})].concat(t.lineItem.words),type:j.TOC,annotation:E}))});const e=function(t,e){for(var s of e){const e=Q(t,s.lineItem.text());if(e)return e.index-s.pageNumber}return null}(t.pages.filter(t=>t.index>i.index),h);h.forEach(s=>{var n,r=t.pages[s.pageNumber+e];r&&((n=X(r,s.lineItem.text()))||(r=t.pages[s.pageNumber+e+1])&&(n=X(r,s.lineItem.text()))),n?function(t,e,s,n){s.headlineItems.forEach(t=>t.annotation=T);const r=j.headlineByLevel(e.level+2),o=s.headlineItems.reduce((t,e)=>Math.max(t,e.height),0);t.items.splice(s.lineIndex+1,0,new k({...s.headlineItems[0],words:e.lineItem.words,height:o,type:r,annotation:E}));var a=n[r.name];a?(a.min=Math.min(a.min,o),a.max=Math.max(a.max,o)):(a={min:o,max:o},n[r.name]=a)}(r,s,n,d):u.push(s)});var f=i.index+2,g=[];const s=e=>{g.length>0&&(g.forEach(s=>{const n=j.headlineByLevel(s.level+2),r=d[n.name];if(r){const[o,a]=function(t,e,s,n,r){const o=e.lineItem.text().toUpperCase();for(var a=n;a<=r;a++){const e=t[a-1];if(e){const t=e.items.findIndex(t=>!t.type&&!t.annotation&&t.height>=s.min&&t.height<=s.max&&l(o,t.text())>=.5);if(t>-1)return[a-1,t]}}return[-1,-1]}(t.pages,s,r,f,e);if(a>-1){const e=t.pages[o];e.items[a].annotation=T,e.items.splice(a+1,0,new k({...s.lineItem,type:n,annotation:E})),p.push(s)}}}),g=[])};u.length>0&&(h.forEach(t=>{u.includes(t)?g.push(t):(s(t.pageNumber),f=t.pageNumber)}),g.length>0&&s(t.pages.length))}const y=[];return y.push("Detected "+e.length+" table of content pages"),e.length>0&&(y.push("TOC headline heights: "+JSON.stringify(d)),y.push("Found TOC headlines: "+(m-u.length+p.length)+"/"+m)),u.length>0&&(y.push("Found TOC headlines (by size): "+p.map(t=>t.lineItem.text())),y.push("Missing TOC headlines: "+u.filter(t=>!p.includes(t)).map(t=>t.lineItem.text()+"=>"+t.pageNumber))),new I({...t,globals:{...t.globals,tocPages:e,headlineTypeToHeightRange:d},messages:y})}};function Q(t,e){for(var s of t)if(X(s,e))return s;return null}function X(t,e){const s=new J({headline:e});var n=0;for(var r of t.items){const t=s.consume(r);if(t)return{lineIndex:n,headlineItems:t};n++}return null}var G=class{levelByMethod;uniqueFonts;constructor(){this.levelByMethod=null,this.uniqueFonts=[]}levelPageItems(t){if(!this.levelByMethod)if(this.calculateUniqueX(t).length>1)this.levelByMethod=this.levelByXDiff;else{const e=this.calculateUniqueFonts(t);e.length>1?(this.uniqueFonts=e,this.levelByMethod=this.levelByFont):this.levelByMethod=this.levelToZero}this.levelByMethod(t)}levelByXDiff(t){const e=this.calculateUniqueX(t);t.forEach(t=>{t.level=e.indexOf(t.lineItem.x)})}levelByFont(t){t.forEach(t=>{t.level=this.uniqueFonts.indexOf(t.lineItem.font)})}levelToZero(t){t.forEach(t=>{t.level=0})}calculateUniqueX(t){var e=t.reduce(function(t,e){return t.indexOf(e.lineItem.x)<0&&t.push(e.lineItem.x),t},[]);return e.sort((t,e)=>t-e),e}calculateUniqueFonts(t){return t.reduce(function(t,e){return t.indexOf(e.lineItem.font)<0&&t.push(e.lineItem.font),t},[])}},Z=class{lineItem;pageNumber;level;constructor(t){this.lineItem=t.lineItem,this.pageNumber=t.pageNumber,this.level=0}},tt=class extends A{constructor(){super("Detect List Items")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{const n=[];t.items.forEach(t=>{if(n.push(t),!t.type){var r=t.text();if(a(t.words[0].string))if(e++,"-"===t.words[0].string)t.annotation=O,t.type=j.LIST;else{t.annotation=T;const e=t.words.map(t=>new C({...t}));e[0].string="-",n.push(new k({...t,words:e,annotation:E,type:j.LIST}))}else/^[\s]*[\d]*[.][\s].*$/g.test(r)&&(s++,t.annotation=O,t.type=j.LIST)}}),t.items=n}),new I({...t,messages:["Detected "+e+" plain list items.","Detected "+s+" numbered list items."]})}},et=class extends A{constructor(){super("Detect Headers")}transform(t){const{tocPages:e,headlineTypeToHeightRange:s,mostUsedHeight:n,mostUsedDistance:r,mostUsedFont:o,maxHeight:a}=t.globals,c=e.length>0;var l=0;const h=function(t,e){const s=/* @__PURE__ */new Set;return t.forEach(t=>{t.items.forEach(n=>{n.type||n.height!==e||s.add(t)})}),s}(t.pages,a),m=n+(a-n)/4;if(h.forEach(t=>{t.items.forEach(t=>{const e=t.height;!t.type&&e>m&&(t.type=e===a?j.H1:j.H2,t.annotation=O,l++)})}),c)Object.keys(s).forEach(e=>{var r=s[e];r.max>n&&t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==r.max||(t.annotation=O,t.type=j.enumValueOf(e),l++)})})});else{const e=[];t.pages.forEach(t=>{t.items.forEach(t=>{!t.type&&t.height>n&&!i(t.text())&&(e.includes(t.height)||e.push(t.height))})}),e.sort((t,e)=>e-t),e.forEach((e,s)=>{if(s+2<=6){const n=j.headlineByLevel(2+s);t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==e||i(t.text())||(l++,t.annotation=O,t.type=n)})})}})}var u=1;if(t.pages.forEach(t=>{t.items.forEach(t=>{t.type&&t.type.headline&&(u=Math.max(u,t.type.headlineLevel))})}),u<6){const e=j.headlineByLevel(u+1);t.pages.forEach(t=>{var s;t.items.forEach(t=>{!t.type&&t.height===n&&t.font!==o&&(!s||s.y<t.y||s.type&&s.type.headline||s.y-t.y>2*r)&&t.text()===t.text().toUpperCase()&&(l++,t.annotation=O,t.type=e),s=t})})}return new I({...t,messages:["Detected "+l+" headlines."]})}};var st=class extends f{items;constructor(t){super(t),this.items=[],t.items&&t.items.forEach(t=>this.addItem(t))}addItem(t){if(this.type&&t.type&&this.type!==t.type)throw new Error(`Adding item of type ${t.type.name} to block of type ${this.type.name}`);this.type||(this.type=t.type),t.parsedElements&&(this.parsedElements?this.parsedElements.add(t.parsedElements):this.parsedElements=t.parsedElements);const e=new k({...t});e.type=null,this.items.push(e)}},nt=class t extends x{constructor(e){if(super(e,st.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},rt=class extends nt{constructor(){super("Gather Blocks")}transform(t){const{mostUsedDistance:e}=t.globals;var s=0,n=0;return t.pages.map(t=>{n+=t.items.length;const r=[];var o=new st({});const a=()=>{o.items.length>1&&(o.annotation=O),r.push(o),o=new st({}),s++};var i=function(t){var e=999;return t.forEach(t=>{e=Math.min(e,t.x)}),999===e?null:e}(t.items);t.items.forEach(t=>{o.items.length>0&&function(t,e,s,n){if(t.type&&t.type.mergeFollowingNonTypedItems&&!e.type)return!1;const r=t.items[t.items.length-1],o=function(t,e,s,n){const r=t.y-e.y;if(r<0-n/2)return!0;var o=n+1;t.x>s&&e.x>s&&(o=n+n/2);return r>o}(r,e,s,n);if(t.type&&t.type.mergeFollowingNonTypedItemsWithSmallDistance&&!e.type&&!o)return!1;if(e.type!==t.type)return!0;return e.type?!e.type.mergeToBlock:o}(o,t,i,e)&&a(),o.addItem(t)}),o.items.length>0&&a(),t.items=r}),new I({...t,messages:["Gathered "+s+" blocks out of "+n+" line items"]})}};var ot=class extends nt{constructor(){super("$1")}transform(t){const e=t.globals.mostUsedHeight??0;var s=0;return t.pages.forEach(t=>{var n=function(t){var e=999;return t.forEach(t=>{t.items.forEach(t=>{e=Math.min(e,t.x)})}),999===e?null:e}(t.items);t.items.forEach(t=>{!t.type&&function(t,e,s){if(0===e.length)return!1;if(1===e.length)return e[0].x>t&&e[0].height<=s+1;for(var n of e)if(n.x===t)return!1;return!0}(n,t.items,e)&&(t.annotation=O,t.type=j.CODE,s++)})}),new I({...t,messages:["Detected "+s+" code/quote items."]})}};var at,it=class extends nt{constructor(){super("Level Lists")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{t.items.filter(t=>t.type===j.LIST).forEach(t=>{var n,r=0;const o={};var a=!1;t.items.forEach(t=>{n?t.x>n?(r++,o[t.x]=r):t.x<n&&(r=o[t.x]):o[t.x]=0,r>0&&(t.words=[new C({string:" ".repeat(3*r)})].concat(t.words),a=!0),n=t.x}),e++,a?(s++,t.annotation=S):t.annotation=b})}),new I({...t,messages:["Modified "+s+" / "+e+" list blocks."]})}},ct=class extends x{constructor(){super("To Text Blocks","TextBlock")}transform(t){return t.pages.forEach(t=>{const e=[];t.items.forEach(t=>{const s=t.type?t.type.name:"Unknown";e.push({category:s,text:j.blockToText(t)})}),t.items=e}),new I({...t})}},lt=class extends x{constructor(){super("To HTML","String")}transform(t){return t.pages.forEach(t=>{var e="";t.items.forEach(t=>{let s;s="TOC"===t.category?t.text:t.text.replace(/(\r\n|\n|\r)/gm,"\n"),"LIST"!==t.category&&(s=s.split("- ").join("")),"CODE"===t.category&&(s=s.split("<code>").join("").split("</code>").join("")),e+=`<p>${s}</p>\n\n`}),t.items=[e]}),new I({...t})}};async function ht(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{({LiteParse:a}=await import("@llamaindex/liteparse"))}catch(p){return{error:`method: 'liteparse' requires the optional @llamaindex/liteparse dependency (Node.js only): ${p.message}`}}const i=new a({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(p){return{error:p.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,u;return n&&(u=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:u,title:m,html:h,format:"pdf"}}async function mt(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{a=await import("@llamaindex/liteparse-wasm"),await async function(t){at||(at=t.default()),await at}(a)}catch(p){return{error:`method: 'liteparse-wasm' requires the optional @llamaindex/liteparse-wasm dependency: ${p.message}`}}const i=new a.LiteParse({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(p){return{error:p.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,u;return n&&(u=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:u,title:m,html:h,format:"pdf"}}var ut=null;async function pt(){return ut||(ut=import("https://cdn.jsdelivr.net/npm/pdfjs-serverless@1/+esm").catch(()=>import("pdfjs-serverless")).then(({resolvePDFJS:t})=>t())),ut}var dt=/\b(?:fig(?:ure)?s?|table|chart|graph|diagram|infographic|exhibit|illustration|plate|scheme)\s*\.?\s*(?:\d+(?:\.\d+)*|[ivxlcdm]+)\b/gi,ft=/^\s*\S[^\t\n]{0,60}?(?:\t+| {2,})\S[^\t\n]{0,60}?(?:\t+| {2,})\S/,gt=/(?:[-+]?[\d][\d.,]*\s*[%$€£]?\s+){3,}[-+]?[\d]/,yt=/[\uFFFD\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function wt(t,e={}){const{sparseTextThreshold:s=200,minTableRows:n=3,minNumericRows:r=2}=e,o=t.map((t,e)=>{const o=[],a=(t||"").replace(/\s+/g," ").trim();0===a.length?o.push("no-text"):a.length<s&&o.push("sparse-text");const i=(t||"").match(yt)?.length||0;a.length>0&&i/a.length>.05&&o.push("garbled-text");const c=[...(t||"").matchAll(dt)].map(t=>t[0].replace(/\s+/g," ").trim()),l=[...(t||"").matchAll(new RegExp(`^\\s*${dt.source}`,"gim"))].map(t=>t[0].trim());l.some(t=>/^table/i.test(t))&&o.push("table-caption"),l.some(t=>!/^table/i.test(t))&&o.push("figure-caption");const h=(t||"").split(/\r?\n/),m=h.filter(t=>ft.test(t)).length,u=h.filter(t=>gt.test(t)).length;return m>=n&&o.push("table-rows"),u>=r&&o.push("numeric-grid"),{page:e+1,needsOcr:o.length>0,reasons:o,captions:c}}),a=o.filter(t=>t.needsOcr).map(t=>t.page);return{needsOcr:a.length>0,pagesNeedingOcr:a,pages:o}}var xt="onnx-community/granite-docling-258M-ONNX",vt="Convert this page to docling.",Et=null;async function Tt(t,e={}){const{processorUrl:s,prompt:n=vt,maxTokens:r=4096}=e;if(s){const e=await fetch(`${s.replace(/\/$/,"")}/api/v1/convert-base64`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({imageBase64:t,mimeType:"image/png",prompt:n,maxTokens:r})}),o=await e.json();if(!e.ok||!o?.success)throw new Error(o?.error||`Processor error ${e.status}`);return o.result}const{model:o,processor:a,lib:i}=await async function(){return Et||(Et=(async()=>{const t=await import("@huggingface/transformers"),e=await t.AutoProcessor.from_pretrained(xt);return{model:await t.AutoModelForVision2Seq.from_pretrained(xt,{dtype:"fp32"}),processor:e,lib:t}})()),Et}(),c=await i.load_image(`data:image/png;base64,${t}`),l=[{role:"user",content:[{type:"image"},{type:"text",text:n}]}],h=await a(a.apply_chat_template(l,{add_generation_prompt:!0}),[c],{do_image_splitting:!0}),m=await o.generate({...h,max_new_tokens:r}),[u]=a.batch_decode(m.slice(null,[h.input_ids.dims.at(-1),null]),{skip_special_tokens:!0});return u}async function bt(t,e=2){const s=t.getViewport({scale:e}),n=await async function(t,e){if("undefined"!=typeof document){const s=document.createElement("canvas");return s.width=t,s.height=e,s}try{return(await import("@napi-rs/canvas")).createCanvas(t,e)}catch{}if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(t,e);throw new Error("No canvas available to rasterize PDF pages — install @napi-rs/canvas in Node.js, or run in a browser/Worker with OffscreenCanvas")}(Math.ceil(s.width),Math.ceil(s.height)),r=n.getContext("2d");return await t.render({canvasContext:r,viewport:s}).promise,async function(t){if("function"==typeof t.toBuffer)return t.toBuffer("image/png").toString("base64");if("function"==typeof t.convertToBlob){const e=await t.convertToBlob({type:"image/png"}),s=new Uint8Array(await e.arrayBuffer());let n="";for(const t of s)n+=String.fromCharCode(t);return btoa(n)}return t.toDataURL("image/png").split(",")[1]}(n)}async function Ot(t,e,s={}){const n=/* @__PURE__ */new Map;for(const o of e)try{const e=await Tt(await bt(await t.getPage(o),s.scale),s);n.set(o,St(e))}catch(r){console.error(`Docling OCR failed for page ${o}:`,r)}return n}function St(t){let e=(t||"").replace(/<\/?doctag>/g,"").replace(/<loc_\d+>/g,"").replace(/<page_break>/g,"").replace(/<end_of_utterance>/g,"").replace(/<page_(?:header|footer)>[\s\S]*?<\/page_(?:header|footer)>/g,"");e=e.replace(/<otsl>([\s\S]*?)<\/otsl>/g,(t,e)=>function(t){return`<table>${t.split("<nl>").map(t=>t.trim()).filter(Boolean).map(t=>{const e=[...t.matchAll(/<(fcel|ched|rhed|ecel|lcel|ucel|xcel)>([^<]*)/g)];return e.length?`<tr>${e.map(([,t,e])=>{const s="ched"===t||"rhed"===t?"th":"td";return`<${s}>${e.trim()}</${s}>`}).join("")}</tr>`:""}).filter(Boolean).join("")}</table>`}(e));for(const[s,n,r]of[[/<title>([\s\S]*?)<\/title>/g,"<h1>","</h1>"],[/<section_header_level_1>([\s\S]*?)<\/section_header_level_1>/g,"<h2>","</h2>"],[/<section_header_level_2>([\s\S]*?)<\/section_header_level_2>/g,"<h3>","</h3>"],[/<section_header_level_3>([\s\S]*?)<\/section_header_level_3>/g,"<h4>","</h4>"],[/<section_header_level_[4-9]>([\s\S]*?)<\/section_header_level_[4-9]>/g,"<h5>","</h5>"],[/<(?:text|paragraph)>([\s\S]*?)<\/(?:text|paragraph)>/g,"<p>","</p>"],[/<caption>([\s\S]*?)<\/caption>/g,"<figcaption>","</figcaption>"],[/<(?:picture|chart)>([\s\S]*?)<\/(?:picture|chart)>/g,"<figure>","</figure>"],[/<code>([\s\S]*?)<\/code>/g,"<pre><code>","</code></pre>"],[/<formula>([\s\S]*?)<\/formula>/g,'<code class="formula">',"</code>"],[/<footnote>([\s\S]*?)<\/footnote>/g,'<p class="footnote">',"</p>"],[/<list_item>([\s\S]*?)<\/list_item>/g,"<li>","</li>"],[/<unordered_list>([\s\S]*?)<\/unordered_list>/g,"<ul>","</ul>"],[/<ordered_list>([\s\S]*?)<\/ordered_list>/g,"<ol>","</ol>"]])e=e.replace(s,(t,e)=>`${n}${e.trim()}${r}`);return e=e.replace(/<\/?(?:[a-z][a-z0-9]*_[a-z0-9_]+|otsl|smiles)>/g,""),e.trim()}async function Lt(t,e={}){const s="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t,{LiteParse:n}=await import("@llamaindex/liteparse"),r=await new n({quiet:!0,...e.liteParseOptions}).isComplex(new Uint8Array(s));return{needsOcr:r.some(t=>t.needsOcr),pages:r,reasons:[...new Set(r.flatMap(t=>t.reasons))]}}async function It(t,e={}){if("liteparse"===e.method){const{method:s,...n}=e;return ht(t,n)}if("liteparse-wasm"===e.method){const{method:s,...n}=e;return mt(t,n)}var{addPageNumbers:s=!1,addCitation:n=!0}=e;const r=e.processor??"frontend",o=/^https?:\/\//i.test(r),a=o?"docling":r,i={...e.doclingOptions,processorUrl:o?r:e.processorUrl};var c="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let l;try{const{getDocument:t}=await pt();l=await t({data:new Uint8Array(c),useSystemFonts:!0,verbosity:0}).promise}catch(k){return{error:k.message}}const h=[...Array(l.numPages).keys()].map(t=>new y({index:t}));let m,f={};for(let d=1;d<=l.numPages;d++){const t=await l.getPage(d),e=await t.getTextContent();if(!(Object.keys(f).length<10)){m=p(f);break}f=u(f,t.pageNumber-1,e.items)}let x=m?m.pageNum:0;for(let u=1;u<=l.numPages;u++){const t=await l.getPage(u);await t.getOperatorList();const e=t.getViewport({scale:1});let s=await t.getTextContent();m&&t.pageIndex>=m.pageIndex&&(s=d(s,x),x++);const n=s.items.map(t=>{const s=[1,0,0,1,0,0];for(let o=0;o<6;o++)s[o]+=t.transform[o]*e.transform[o%2?3:0],o%2&&(s[o+1]+=t.transform[o]*e.transform[1]);const n=Math.sqrt(s[2]*s[2]+s[3]*s[3]),r=t.height/n;return new g({x:Math.round(t.transform[4]),y:Math.round(t.transform[5]),width:Math.round(t.width),height:Math.round(r<=1?t.height:r),text:t.str,font:t.fontName})});h[t.pageNumber-1].items=n}const v=h.map(t=>Object.values(t.items.reduce((t,e)=>((t[e.y]=t[e.y]||[]).push(e.text),t),{})).map(t=>t.join(" ")).join("\n"));var E=new I({pages:h});let T;[new U,new q,new Y,new V,new K,new et,new tt,new rt,new ot,new it,new ct,new lt].forEach(t=>{T&&(E=T.completeTransform(E)),E=t.transform(E),T=t});var b=E.pages.map((t,e)=>`<p id="page-${e+1}">${s?` [${e+1}] `:""}${t.items.join('</p><p id="page-'+e+'">')}</p>`);const O=wt(v,e.ocrScanOptions);if("docling"===a||"hybrid"===a){const t="docling"===a?b.map((t,e)=>e+1):O.pagesNeedingOcr,e=await Ot(l,t,i);for(const[n,r]of e)b[n-1]=`<section class="ocr-page" id="page-${n}">${s?` [${n}] `:""}${r}</section>`}var S=b.join("");if(n){var{Author:L,Title:C}=(await l.getMetadata()).info;C=S.slice(0,400).match(/<h[0-9]>(.*?)<\/h[0-9]>/)?.[1]||C}return{author:L,title:C,html:S,format:"pdf",processor:a,ocrScan:O}}export{It as convertPDFToHTML,ht as convertPDFToHTMLWithLiteParse,mt as convertPDFToHTMLWithLiteParseWasm,Lt as detectPdfNeedsOcr,St as doctagsToHtml,pt as loadPdfJs,Tt as ocrImageWithDocling,Ot as ocrPdfPagesWithDocling,bt as renderPdfPageToPngBase64,wt as scanPagesForOCR};
|
|
1
|
+
function t(t){for(;32===t.charCodeAt(0);)t=t.substring(1,t.length);return t}function e(t){for(;32===t.charCodeAt(t.length-1);)t=t.substring(0,t.length-1);return t}function s(t){return t>=48&&t<=57}function n(t){for(var e=0;e<t.length;e++)if(!s(t.charCodeAt(e)))return!1;return!0}function r(t,e){const s=e.charCodeAt(0);for(var n=0;n<t.length;n++)if(t.charCodeAt(n)!==s)return!1;return!0}function o(t){return function(t){const e=[];for(var s=0;s<t.length;s++)e.push(t.charCodeAt(s));return e}(t=t.toUpperCase()).filter(t=>32!==t&&9!==t&&46!==t)}function a(t){if(t.length>1)return!1;const e=t.charAt(0);return"-"===e||"•"===e||"–"===e}function i(t){return/^[\s]*[-•–][\s].*$/g.test(t)}function c(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function l(t,e){const s=new Set(t.toUpperCase().split(" ")),n=new Set(e.toUpperCase().split(" "));return new Set([...s].filter(t=>n.has(t))).size/Math.max(s.size,n.size)}var h=(t,e,s)=>Math.floor(t/e*s),m=(s,r,o)=>{for(const{str:a}of s){const s=e(t(a));n(s)&&(r[o]||(r[o]=[]),r[o].push(Number(s)))}return r},u=(t,e,s)=>{const n=h(1,6,s.length),r=h(5,6,s.length),o=m(s.slice(0,n),t,e);return m(s.slice(r),o,e)},p=t=>{let e=0;const s=Object.keys(t);if(0!==s.length&&1!==s.length)for(let n=0;n<s.length-1;n++){const r=t[s[n]],o=t[s[n+1]],a=e;for(let t=0;t<r.length&&e<2;t++)for(let a=0;a<o.length&&e<2;a++){const i=Number(s[n+1])-Number(s[n]);(r[t]+1===o[a]||i>1&&r[t]+i===o[a])&&e++}let i=n>0?Object.entries(t)[n-1]:Object.entries(t)[n];if(a===e)e=0,i=Object.entries(t)[n];else if(e>=2)return{pageIndex:Number(i[0]),pageNum:i[1][0]}}},d=(t,e)=>{const s={items:[...t.items]},n=h(1,6,s.items.length),r=h(5,6,s.items.length);return s.items=s.items.filter((t,o)=>{const a=o>0&&o<n,i=o>r&&o<s.items.length;return a||i?Number(t.str)!==Number(e):t}),s},f=class t{type;annotation;parsedElements;constructor(e){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.type=e.type??null,this.annotation=e.annotation??null,this.parsedElements=e.parsedElements??null}},g=class extends f{x;y;width;height;text;font;lineFormat;unopenedFormat;unclosedFormat;constructor(t){super(t),this.x=t.x,this.y=t.y,this.width=t.width,this.height=t.height,this.text=t.text,this.font=t.font,this.lineFormat=t.lineFormat??null,this.unopenedFormat=t.unopenedFormat??null,this.unclosedFormat=t.unclosedFormat??null}},y=class{index;items;constructor(t){this.index=t.index,this.items=t.items||[]}};async function w(t,e={}){const s=e.timeout?1e3*e.timeout:1e4,n=new AbortController,r=setTimeout(()=>n.abort(),s);try{const s=await fetch(t,{signal:n.signal});if(clearTimeout(r),!s.ok)throw new Error(`HTTP ${s.status}`);return"arraybuffer"===e.responseType?await s.arrayBuffer():await s.text()}catch(o){throw clearTimeout(r),o}}var x=class t{name;itemType;constructor(e,s){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");if(this.transform===t.prototype.transform)throw new TypeError("Please implement abstract method 'transform()'.");this.name=e,this.itemType=s}transform(t){throw new TypeError("Do not call abstract method foo from child.")}completeTransform(t){return t.messages=[],t}},v=class{category;color;constructor(t){this.category=t.category,this.color=t.color}},E=new v({category:"Added",color:"green"}),T=new v({category:"Removed",color:"red"}),b=new v({category:"Unchanged",color:"brown"}),O=new v({category:"Detected",color:"green"}),S=new v({category:"Modified",color:"green"}),L=class t extends x{constructor(e){if(super(e,g.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},I=class{pages;globals;messages;constructor(t){this.pages=t.pages||[],this.globals=t.globals||{},this.messages=t.messages||[]}},C=class{string;type;format;constructor(t){this.string=t.string,this.type=t.type??null,this.format=t.format??null}},k=class extends f{x;y;width;height;words;constructor(t){super(t),this.x=t.x??0,this.y=t.y??0,this.width=t.width??0,this.height=t.height??0,this.words=t.words||[],t.text&&!t.words&&(this.words=t.text.split(" ").filter(t=>t.trim().length>0).map(t=>new C({string:t})))}text(){return this.wordStrings().join(" ")}wordStrings(){return this.words.map(t=>t.string)}},N=class t{results;stash;constructor(){if(this.constructor===t)throw new TypeError("Can not construct abstract class.");this.results=[],this.stash=[]}consumeAll(t){t.forEach(t=>this.consume(t))}consume(t){this.shouldStash(t)?(this.matchesStash(t)||this.flushStash(),this.pushOnStash(t)):(this.stash.length>0&&this.flushStash(),this.results.push(t))}pushOnStash(t){this.onPushOnStash(t),this.stash.push(t)}complete(){return this.stash.length>0&&this.flushStash(),this.results}matchesStash(t){if(0===this.stash.length)return!0;const e=this.stash[this.stash.length-1];return this.doMatchesStash(e,t)}flushStash(){this.stash.length>0&&(this.doFlushStash(this.stash,this.results),this.stash=[])}onPushOnStash(t){}shouldStash(t){throw new TypeError(" Do not call abstract method foo from child."+t)}doMatchesStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}doFlushStash(t,e){throw new TypeError(" Do not call abstract method foo from child."+t+e)}},F=class{footnoteLinks;footnotes;containLinks;formattedWords;constructor(t){this.footnoteLinks=t.footnoteLinks||[],this.footnotes=t.footnotes||[],this.containLinks=t.containLinks??!1,this.formattedWords=t.formattedWords??0}add(t){this.footnoteLinks=this.footnoteLinks.concat(t.footnoteLinks),this.footnotes=this.footnotes.concat(t.footnotes),this.containLinks=this.containLinks||t.containLinks,this.formattedWords+=t.formattedWords}};function _(t){t.sort((t,e)=>t.x-e.x)}var $={BOLD:{name:"BOLD",startSymbol:"<strong>",endSymbol:"</strong>"},OBLIQUE:{name:"OBLIQUE",startSymbol:"<em>",endSymbol:"</em>"},BOLD_OBLIQUE:{name:"BOLD_OBLIQUE",startSymbol:"<strong><em>",endSymbol:"</em></strong>"}},B={LINK:{name:"LINK",toText:t=>`<a href="${t}">${t}</a>`},FOOTNOTE_LINK:{name:"FOOTNOTE_LINK",attachWithoutWhitespace:!0,plainTextFormat:!0,toText:t=>`<sup><a href="#${t}">${t}</a></sup>`},FOOTNOTE:{name:"FOOTNOTE",toText:t=>`<p id="${t}">^${t}</p>`}},M=class{fontToFormats;constructor(t){this.fontToFormats=t}compact(t){_(t);const e=new D(this.fontToFormats);e.consumeAll(t.map(t=>new g({...t})));const s=e.complete();var n=0,r=0;return t.forEach(t=>{n=Math.max(n,t.height),r+=t.width}),new k({x:t[0].x,y:t[0].y,height:n,width:r,words:s,parsedElements:new F({footnoteLinks:e.footnoteLinks,footnotes:e.footnotes,containLinks:e.containLinks,formattedWords:e.formattedWords})})}},D=class extends N{fontToFormats;footnoteLinks;footnotes;formattedWords;containLinks;stashedNumber;firstY;currentItem;constructor(t){super(),this.fontToFormats=t,this.footnoteLinks=[],this.footnotes=[],this.formattedWords=0,this.containLinks=!1,this.stashedNumber=!1,this.currentItem=null}shouldStash(t){return this.firstY||(this.firstY=t.y),this.currentItem=t,!0}onPushOnStash(t){this.stashedNumber=n(t.text.trim())}doMatchesStash(t,e){if(this.fontToFormats.get(t.font)!==this.fontToFormats.get(e.font))return!1;const s=n(e.text.trim());return this.stashedNumber===s}doFlushStash(t,e){if(this.stashedNumber){const s=t.map(t=>t.text).join("").trim();t[0].y>this.firstY?(e.push(new C({string:`${s}`,type:B.FOOTNOTE_LINK})),this.footnoteLinks.push(parseInt(s))):this.currentItem&&this.currentItem.y<t[0].y?(e.push(new C({string:`${s}`,type:B.FOOTNOTE})),this.footnotes.push(s)):this.copyStashItemsAsText(t,e)}else this.copyStashItemsAsText(t,e)}copyStashItemsAsText(t,e){const s=this.fontToFormats.get(t[0].font);e.push(...this.itemsToWords(t,s))}itemsToWords(t,e){const s=(n=t,r="",o=null,n.forEach(t=>{var e=t.text;r.endsWith(" ")||e.startsWith(" ")||(o?t.x-o.x-o.width>5&&(r+=" "):a(t.text)&&(e+=" ")),r+=e,o=t}),r).split(" ");var n,r,o;const i=e?$[e]:null;return s.filter(t=>t.trim().length>0).map(t=>{var e=null;return t.startsWith("http:")?(this.containLinks=!0,e=B.LINK):t.startsWith("www.")&&(this.containLinks=!0,t=`http://${t}`,e=B.LINK),i&&this.formattedWords++,new C({string:t,type:e,format:i})})}};var U=class extends L{fontMap;constructor(t){super("Calculate Global Stats"),this.fontMap=t}transform(t){const e={},s={};var n,r=0;t.pages.forEach(t=>{t.items.forEach(t=>{t.height&&(e[t.height]=e[t.height]?e[t.height]+1:1,s[t.font]=s[t.font]?s[t.font]+1:1,t.height>r&&(r=t.height,n=t.font))})});const o=parseInt(H(e)),a=H(s),i={};t.pages.forEach(t=>{var e;t.items.forEach(t=>{if(t.height===o&&t.text.trim().length>0){if(e&&t.y!==e.y){const s=e.y-t.y;s>0&&(i[s]=i[s]?i[s]+1:1)}e=t}else e=null})});const c=parseInt(H(i)),l=[],h=/* @__PURE__ */new Map;this.fontMap?.forEach(function(t,e){l.push(e+" = "+t.name);const s=t.name.toLowerCase();var r=null;e===a?r=null:s.includes("bold")&&(s.includes("oblique")||s.includes("italic"))?r=$.BOLD_OBLIQUE:s.includes("bold")?r=$.BOLD:s.includes("oblique")||s.includes("italic")?r=$.OBLIQUE:s===n&&(r=$.BOLD),r&&h.set(e,r.name)}),l.sort();const m=t.pages.map(t=>({...t,items:t.items.map(t=>({...t}))}));return new I({...t,pages:m,globals:{mostUsedHeight:o,mostUsedFont:a,mostUsedDistance:c,maxHeight:r,maxHeightFont:n,fontToFormats:h},messages:["Items per height: "+JSON.stringify(e),"Items per font: "+JSON.stringify(s),"Items per distance: "+JSON.stringify(i),"Fonts:"+JSON.stringify(l)]})}};function H(t){var e=0,s="";return Object.keys(t).map(n=>{(!s||t[n]>e)&&(e=t[n],s=n)}),s}var A=class t extends x{constructor(e){if(super(e,k.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},P=class{mostUsedDistance;constructor(t){this.mostUsedDistance=t.mostUsedDistance||12}group(t){const e=[];var s=[];return t.forEach(t=>{s.length>0&&Math.abs(s[0].y-t.y)>=this.mostUsedDistance/2&&(e.push(s),s=[]),s.push(t)}),e.push(s),e.forEach(t=>{_(t)}),e}},j={H1:{name:"H1",headline:!0,headlineLevel:1,toText:t=>"<h1>"+W(t.items,!0)+"</h1>"},H2:{name:"H2",headline:!0,headlineLevel:2,toText:t=>"<h2>"+W(t.items,!0)+"</h2>"},H3:{name:"H3",headline:!0,headlineLevel:3,toText:t=>"<h3>"+W(t.items,!0)+"</h3>"},H4:{name:"H4",headline:!0,headlineLevel:4,toText:t=>"<h4>"+W(t.items,!0)+"</h4>"},H5:{name:"H5",headline:!0,headlineLevel:5,toText:t=>"<h5>"+W(t.items,!0)+"</h5>"},H6:{name:"H6",headline:!0,headlineLevel:6,toText:t=>"<h6>"+W(t.items,!0)+"</h6>"},TOC:{name:"TOC",mergeToBlock:!0,toText:t=>W(t.items,!0)},FOOTNOTES:{name:"FOOTNOTES",mergeToBlock:!0,mergeFollowingNonTypedItems:!0,toText:t=>"<p>"+W(t.items,!1)+"</p>"},CODE:{name:"CODE",mergeToBlock:!0,toText:t=>"<code>"+W(t.items,!0)+"</code>"},LIST:{name:"LIST",mergeToBlock:!1,mergeFollowingNonTypedItemsWithSmallDistance:!0,toText:t=>"<ul>\n"+W(t.items,!1)+"</ul>"},PARAGRAPH:{name:"PARAGRAPH",toText:t=>"<p>"+W(t.items,!1)+"</p>"}};function W(t,e){var s,n="";const r=()=>{n+=s.endSymbol,s=null};return t.forEach((o,a)=>{var i;o&&(o.words.forEach((t,o)=>{const a=t.type,i=t.format;var c;!s||i&&i===s||r(),o>0&&(!a||!a.attachWithoutWhitespace)&&(1!==(c=t.string).length||"."!==c[0]&&"!"!==c[0]&&"?"!==c[0])&&(n+=" "),!i||s||e||(n+=(s=i).startSymbol),!a||e&&!a.plainTextFormat?n+=t.string:n+=a.toText(t.string)}),!s||a!==t.length-1&&(0===(i=t[a+1]).words.length?null:i.words[0].format)===s||r(),n+="\n")}),n}j.enumValueOf=function(t){return j[t]},j.isHeadline=function(t){return t&&2===t.name.length&&"H"===t.name[0]},j.blockToText=function(t){return t.type?t.type.toText(t):W(t.items,!1)},j.headlineByLevel=function(t){return 1===t?j.H1:2===t?j.H2:3===t?j.H3:4===t?j.H4:5===t?j.H5:(t>6&&console.warn("Unsupported headline level: "+t+" (supported are 1-6), defaulting to level 6"),j.H6)};var q=class extends A{constructor(){super("Compact To Lines")}transform(t){const{mostUsedDistance:e,fontToFormats:s}=t.globals,n=[],r=[];var o=0,a=0;const i=new P({mostUsedDistance:e}),c=new M(s);return t.pages.forEach(t=>{if(t.items.length>0){const e=[];i.group(t.items).forEach(s=>{const i=c.compact(s);if(s.length>1&&(i.annotation=E,s.forEach(t=>{t.annotation=T,e.push(new k({...t}))})),0===i.words.length&&(i.annotation=T),e.push(i),i.parsedElements.formattedWords&&(a+=i.parsedElements.formattedWords),i.parsedElements.containLinks&&o++,i.parsedElements.footnoteLinks.length>0){const e=i.parsedElements.footnoteLinks.map(e=>({footnoteLink:e,page:t.index+1}));r.push.apply(r,e)}if(i.parsedElements.footnotes.length>0){i.type=j.FOOTNOTES;const e=i.parsedElements.footnotes.map(e=>({footnote:e,page:t.index+1}));n.push.apply(n,e)}}),t.items=e}}),new I({...t,messages:["Detected "+a+" formatted words","Found "+o+" links","Detected "+r.length+" footnotes links","Detected "+n.length+" footnotes"]})}};function R(t){var e=0;if(0===t.trim().length)return e;for(var n=0;n<t.length;n++){const r=t.charCodeAt(n);s(r)||32===r||160===r||(e=(e<<5)-e+r,e|=0)}return e}var Y=class extends A{constructor(){super("Remove Repetitive Elements")}transform(t){const e=[],s={},n={};t.pages.forEach(t=>{const r=t.items.reduce((t,e)=>(e.y<t.minY?(t.minElements=[e],t.minY=e.y):e.y===t.minY&&t.minElements.push(e),e.y>t.maxY?(t.maxElements=[e],t.maxY=e.y):e.y===t.maxY&&t.maxElements.push(e),t),{minY:999,maxY:0,minElements:[],maxElements:[]}),o=R(r.minElements.reduce((t,e)=>t+e.text().toUpperCase(),"")),a=R(r.maxElements.reduce((t,e)=>t+e.text().toUpperCase(),""));e.push({minElements:r.minElements,maxElements:r.maxElements,minLineHash:o,maxLineHash:a}),s[o]=s[o]?s[o]+1:1,n[a]=n[a]?n[a]+1:1});var r=0,o=0;return t.pages.forEach((a,i)=>{s[e[i].minLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].minElements.forEach(t=>{t.annotation=T}),o++),n[e[i].maxLineHash]>=Math.max(3,2*t.pages.length/3)&&(e[i].maxElements.forEach(t=>{t.annotation=T}),r++)}),new I({...t,messages:["Removed Header: "+r,"Removed Footers: "+o]})}},V=class extends A{constructor(){super("Vertical to Horizontal Text")}transform(t){var e=0;return t.pages.forEach(t=>{const s=new z;s.consumeAll(t.items),t.items=s.complete(),e+=s.foundVerticals}),new I({...t,messages:["Converted "+e+" verticals"]})}},z=class extends N{foundVerticals;constructor(){super(),this.foundVerticals=0}shouldStash(t){return 1===t.words.length&&1===t.words[0].string.length}doMatchesStash(t,e){return t.y-e.y>5&&t.words[0].type===e.words[0].type}doFlushStash(t,e){if(t.length>5){var s=[],n=999,r=0,o=0,a=0;t.forEach(t=>{t.annotation=T,e.push(t),s.push(t.words[0]),n=Math.min(n,t.x),r=Math.max(r,t.y),o+=t.width,a=Math.max(a,t.height)}),e.push(new k({...t[0],x:n,y:r,width:o,height:a,words:s,annotation:E})),this.foundVerticals++}else e.push(...t)}},J=class{headlineCharCodes;stackedLineItems;stackedChars;constructor(t){this.headlineCharCodes=o(t.headline),this.stackedLineItems=[],this.stackedChars=0}consume(t){const e=o(t.text());if(this.matchAll(e)){if(this.stackedLineItems.push(t),this.stackedChars+=e.length,this.stackedChars===this.headlineCharCodes.length)return this.stackedLineItems}else this.stackedChars>0&&(this.stackedChars=0,this.stackedLineItems=[],this.consume(t));return null}matchAll(t){for(var e=0;e<t.length;e++){const s=this.headlineCharCodes[this.stackedChars+e];if(t[e]!==s)return!1}return!0}},K=class extends A{constructor(){super("Detect TOC")}transform(t){const e=[],o=Math.min(20,t.pages.length),a=new G;var i,c,h=[];t.pages.slice(0,o).forEach(t=>{var o=0;const l=/* @__PURE__ */new Set,m=[];var u,p;if(t.items.filter(t=>t.words.includes(t=>r(t.string,"."))).forEach(t=>{var e=t.words.filter(t=>!r(t.string,"."));const a=[];for(;e.length>0&&n(e[e.length-1].string);){const t=e.pop();a.unshift(t.string)}if(0===a.length&&e.length>0){const t=e[e.length-1];for(;s(t.string.charCodeAt(t.string.length-1));)a.unshift(t.string.charAt(t.string.length-1)),t.string=t.string.substring(0,t.string.length-1)}var i=a.length>0;i?(i=!0,u&&(e.push(...u),u=null),m.push(new Z({pageNumber:parseInt(a.join("")),lineItem:new k({...t,words:e})})),o++):c?(u&&l.add(p),u=e,p=t):c=t}),100*o/t.items.length>75){e.push(t.index+1),i=t,a.levelPageItems(m),h.push(...m);const s=[];t.items.forEach(t=>{l.has(t)||(t.annotation=T),s.push(t),t===c&&s.push(new k({...t,type:j.H2,annotation:E}))}),t.items=s}else c=null});var m=h.length;const u=[],p=[],d={};if(e.length>0){h.forEach(t=>{i.items.push(new k({words:[new C({string:" ".repeat(3*t.level)+"-"})].concat(t.lineItem.words),type:j.TOC,annotation:E}))});const e=function(t,e){for(var s of e){const e=Q(t,s.lineItem.text());if(e)return e.index-s.pageNumber}return null}(t.pages.filter(t=>t.index>i.index),h);h.forEach(s=>{var n,r=t.pages[s.pageNumber+e];r&&((n=X(r,s.lineItem.text()))||(r=t.pages[s.pageNumber+e+1])&&(n=X(r,s.lineItem.text()))),n?function(t,e,s,n){s.headlineItems.forEach(t=>t.annotation=T);const r=j.headlineByLevel(e.level+2),o=s.headlineItems.reduce((t,e)=>Math.max(t,e.height),0);t.items.splice(s.lineIndex+1,0,new k({...s.headlineItems[0],words:e.lineItem.words,height:o,type:r,annotation:E}));var a=n[r.name];a?(a.min=Math.min(a.min,o),a.max=Math.max(a.max,o)):(a={min:o,max:o},n[r.name]=a)}(r,s,n,d):u.push(s)});var f=i.index+2,g=[];const s=e=>{g.length>0&&(g.forEach(s=>{const n=j.headlineByLevel(s.level+2),r=d[n.name];if(r){const[o,a]=function(t,e,s,n,r){const o=e.lineItem.text().toUpperCase();for(var a=n;a<=r;a++){const e=t[a-1];if(e){const t=e.items.findIndex(t=>!t.type&&!t.annotation&&t.height>=s.min&&t.height<=s.max&&l(o,t.text())>=.5);if(t>-1)return[a-1,t]}}return[-1,-1]}(t.pages,s,r,f,e);if(a>-1){const e=t.pages[o];e.items[a].annotation=T,e.items.splice(a+1,0,new k({...s.lineItem,type:n,annotation:E})),p.push(s)}}}),g=[])};u.length>0&&(h.forEach(t=>{u.includes(t)?g.push(t):(s(t.pageNumber),f=t.pageNumber)}),g.length>0&&s(t.pages.length))}const y=[];return y.push("Detected "+e.length+" table of content pages"),e.length>0&&(y.push("TOC headline heights: "+JSON.stringify(d)),y.push("Found TOC headlines: "+(m-u.length+p.length)+"/"+m)),u.length>0&&(y.push("Found TOC headlines (by size): "+p.map(t=>t.lineItem.text())),y.push("Missing TOC headlines: "+u.filter(t=>!p.includes(t)).map(t=>t.lineItem.text()+"=>"+t.pageNumber))),new I({...t,globals:{...t.globals,tocPages:e,headlineTypeToHeightRange:d},messages:y})}};function Q(t,e){for(var s of t)if(X(s,e))return s;return null}function X(t,e){const s=new J({headline:e});var n=0;for(var r of t.items){const t=s.consume(r);if(t)return{lineIndex:n,headlineItems:t};n++}return null}var G=class{levelByMethod;uniqueFonts;constructor(){this.levelByMethod=null,this.uniqueFonts=[]}levelPageItems(t){if(!this.levelByMethod)if(this.calculateUniqueX(t).length>1)this.levelByMethod=this.levelByXDiff;else{const e=this.calculateUniqueFonts(t);e.length>1?(this.uniqueFonts=e,this.levelByMethod=this.levelByFont):this.levelByMethod=this.levelToZero}this.levelByMethod(t)}levelByXDiff(t){const e=this.calculateUniqueX(t);t.forEach(t=>{t.level=e.indexOf(t.lineItem.x)})}levelByFont(t){t.forEach(t=>{t.level=this.uniqueFonts.indexOf(t.lineItem.font)})}levelToZero(t){t.forEach(t=>{t.level=0})}calculateUniqueX(t){var e=t.reduce(function(t,e){return t.indexOf(e.lineItem.x)<0&&t.push(e.lineItem.x),t},[]);return e.sort((t,e)=>t-e),e}calculateUniqueFonts(t){return t.reduce(function(t,e){return t.indexOf(e.lineItem.font)<0&&t.push(e.lineItem.font),t},[])}},Z=class{lineItem;pageNumber;level;constructor(t){this.lineItem=t.lineItem,this.pageNumber=t.pageNumber,this.level=0}},tt=class extends A{constructor(){super("Detect List Items")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{const n=[];t.items.forEach(t=>{if(n.push(t),!t.type){var r=t.text();if(a(t.words[0].string))if(e++,"-"===t.words[0].string)t.annotation=O,t.type=j.LIST;else{t.annotation=T;const e=t.words.map(t=>new C({...t}));e[0].string="-",n.push(new k({...t,words:e,annotation:E,type:j.LIST}))}else/^[\s]*[\d]*[.][\s].*$/g.test(r)&&(s++,t.annotation=O,t.type=j.LIST)}}),t.items=n}),new I({...t,messages:["Detected "+e+" plain list items.","Detected "+s+" numbered list items."]})}},et=class extends A{constructor(){super("Detect Headers")}transform(t){const{tocPages:e,headlineTypeToHeightRange:s,mostUsedHeight:n,mostUsedDistance:r,mostUsedFont:o,maxHeight:a}=t.globals,c=e.length>0;var l=0;const h=function(t,e){const s=/* @__PURE__ */new Set;return t.forEach(t=>{t.items.forEach(n=>{n.type||n.height!==e||s.add(t)})}),s}(t.pages,a),m=n+(a-n)/4;if(h.forEach(t=>{t.items.forEach(t=>{const e=t.height;!t.type&&e>m&&(t.type=e===a?j.H1:j.H2,t.annotation=O,l++)})}),c)Object.keys(s).forEach(e=>{var r=s[e];r.max>n&&t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==r.max||(t.annotation=O,t.type=j.enumValueOf(e),l++)})})});else{const e=[];t.pages.forEach(t=>{t.items.forEach(t=>{!t.type&&t.height>n&&!i(t.text())&&(e.includes(t.height)||e.push(t.height))})}),e.sort((t,e)=>e-t),e.forEach((e,s)=>{if(s+2<=6){const n=j.headlineByLevel(2+s);t.pages.forEach(t=>{t.items.forEach(t=>{t.type||t.height!==e||i(t.text())||(l++,t.annotation=O,t.type=n)})})}})}var u=1;if(t.pages.forEach(t=>{t.items.forEach(t=>{t.type&&t.type.headline&&(u=Math.max(u,t.type.headlineLevel))})}),u<6){const e=j.headlineByLevel(u+1);t.pages.forEach(t=>{var s;t.items.forEach(t=>{!t.type&&t.height===n&&t.font!==o&&(!s||s.y<t.y||s.type&&s.type.headline||s.y-t.y>2*r)&&t.text()===t.text().toUpperCase()&&(l++,t.annotation=O,t.type=e),s=t})})}return new I({...t,messages:["Detected "+l+" headlines."]})}};var st=class extends f{items;constructor(t){super(t),this.items=[],t.items&&t.items.forEach(t=>this.addItem(t))}addItem(t){if(this.type&&t.type&&this.type!==t.type)throw new Error(`Adding item of type ${t.type.name} to block of type ${this.type.name}`);this.type||(this.type=t.type),t.parsedElements&&(this.parsedElements?this.parsedElements.add(t.parsedElements):this.parsedElements=t.parsedElements);const e=new k({...t});e.type=null,this.items.push(e)}},nt=class t extends x{constructor(e){if(super(e,st.name),this.constructor===t)throw new TypeError("Can not construct abstract class.")}completeTransform(t){return t.messages=[],t.pages.forEach(t=>{t.items=t.items.filter(t=>!t.annotation||t.annotation!==T),t.items.forEach(t=>t.annotation=null)}),t}},rt=class extends nt{constructor(){super("Gather Blocks")}transform(t){const{mostUsedDistance:e}=t.globals;var s=0,n=0;return t.pages.map(t=>{n+=t.items.length;const r=[];var o=new st({});const a=()=>{o.items.length>1&&(o.annotation=O),r.push(o),o=new st({}),s++};var i=function(t){var e=999;return t.forEach(t=>{e=Math.min(e,t.x)}),999===e?null:e}(t.items);t.items.forEach(t=>{o.items.length>0&&function(t,e,s,n){if(t.type&&t.type.mergeFollowingNonTypedItems&&!e.type)return!1;const r=t.items[t.items.length-1],o=function(t,e,s,n){const r=t.y-e.y;if(r<0-n/2)return!0;var o=n+1;t.x>s&&e.x>s&&(o=n+n/2);return r>o}(r,e,s,n);if(t.type&&t.type.mergeFollowingNonTypedItemsWithSmallDistance&&!e.type&&!o)return!1;if(e.type!==t.type)return!0;return e.type?!e.type.mergeToBlock:o}(o,t,i,e)&&a(),o.addItem(t)}),o.items.length>0&&a(),t.items=r}),new I({...t,messages:["Gathered "+s+" blocks out of "+n+" line items"]})}};var ot=class extends nt{constructor(){super("$1")}transform(t){const e=t.globals.mostUsedHeight??0;var s=0;return t.pages.forEach(t=>{var n=function(t){var e=999;return t.forEach(t=>{t.items.forEach(t=>{e=Math.min(e,t.x)})}),999===e?null:e}(t.items);t.items.forEach(t=>{!t.type&&function(t,e,s){if(0===e.length)return!1;if(1===e.length)return e[0].x>t&&e[0].height<=s+1;for(var n of e)if(n.x===t)return!1;return!0}(n,t.items,e)&&(t.annotation=O,t.type=j.CODE,s++)})}),new I({...t,messages:["Detected "+s+" code/quote items."]})}};var at,it=class extends nt{constructor(){super("Level Lists")}transform(t){var e=0,s=0;return t.pages.forEach(t=>{t.items.filter(t=>t.type===j.LIST).forEach(t=>{var n,r=0;const o={};var a=!1;t.items.forEach(t=>{n?t.x>n?(r++,o[t.x]=r):t.x<n&&(r=o[t.x]):o[t.x]=0,r>0&&(t.words=[new C({string:" ".repeat(3*r)})].concat(t.words),a=!0),n=t.x}),e++,a?(s++,t.annotation=S):t.annotation=b})}),new I({...t,messages:["Modified "+s+" / "+e+" list blocks."]})}},ct=class extends x{constructor(){super("To Text Blocks","TextBlock")}transform(t){return t.pages.forEach(t=>{const e=[];t.items.forEach(t=>{const s=t.type?t.type.name:"Unknown";e.push({category:s,text:j.blockToText(t)})}),t.items=e}),new I({...t})}},lt=class extends x{constructor(){super("To HTML","String")}transform(t){return t.pages.forEach(t=>{var e="";t.items.forEach(t=>{let s;s="TOC"===t.category?t.text:t.text.replace(/(\r\n|\n|\r)/gm,"\n"),"LIST"!==t.category&&(s=s.split("- ").join("")),"CODE"===t.category&&(s=s.split("<code>").join("").split("</code>").join("")),e+=`<p>${s}</p>\n\n`}),t.items=[e]}),new I({...t})}};async function ht(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{({LiteParse:a}=await(import("@llamaindex/liteparse")))}catch(p){return{error:`method: 'liteparse' requires the optional @llamaindex/liteparse dependency (Node.js only): ${p.message}`}}const i=new a({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(p){return{error:p.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,u;return n&&(u=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:u,title:m,html:h,format:"pdf"}}async function mt(t,e={}){const{addPageNumbers:s=!1,addCitation:n=!0,liteParseOptions:r={}}=e,o="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let a;try{a=await(import("@llamaindex/liteparse-wasm")),await async function(t){at||(at=t.default()),await at}(a)}catch(p){return{error:`method: 'liteparse-wasm' requires the optional @llamaindex/liteparse-wasm dependency: ${p.message}`}}const i=new a.LiteParse({quiet:!0,ocrEnabled:!1,ocrFailureFatal:!1,...r});let l;try{l=await i.parse(new Uint8Array(o))}catch(p){return{error:p.message}}const h=l.pages.reduce((t,e)=>{const n=e.text.split(/\n{2,}/).filter(t=>t.trim().length>0).map(t=>c(t).replace(/\n/g,"<br/>")).join("</p><p>");return t+`<p id="page-${e.pageNum}">${s?` [${e.pageNum}] `:""}${n}</p>`},"");let m,u;return n&&(u=l.creator||l.producer,m=l.pages[0]?.text.split("\n").find(t=>t.trim().length>0)),{author:u,title:m,html:h,format:"pdf"}}var ut=null;async function pt(){return ut||(ut=import("https://cdn.jsdelivr.net/npm/pdfjs-serverless@1/+esm").catch(()=>import("pdfjs-serverless")).then(({resolvePDFJS:t})=>t())),ut}var dt=/\b(?:fig(?:ure)?s?|table|chart|graph|diagram|infographic|exhibit|illustration|plate|scheme)\s*\.?\s*(?:\d+(?:\.\d+)*|[ivxlcdm]+)\b/gi,ft=/^\s*\S[^\t\n]{0,60}?(?:\t+| {2,})\S[^\t\n]{0,60}?(?:\t+| {2,})\S/,gt=/(?:[-+]?[\d][\d.,]*\s*[%$€£]?\s+){3,}[-+]?[\d]/,yt=/[\uFFFD\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function wt(t,e={}){const{sparseTextThreshold:s=200,minTableRows:n=3,minNumericRows:r=2}=e,o=t.map((t,e)=>{const o=[],a=(t||"").replace(/\s+/g," ").trim();0===a.length?o.push("no-text"):a.length<s&&o.push("sparse-text");const i=(t||"").match(yt)?.length||0;a.length>0&&i/a.length>.05&&o.push("garbled-text");const c=[...(t||"").matchAll(dt)].map(t=>t[0].replace(/\s+/g," ").trim()),l=[...(t||"").matchAll(new RegExp(`^\\s*${dt.source}`,"gim"))].map(t=>t[0].trim());l.some(t=>/^table/i.test(t))&&o.push("table-caption"),l.some(t=>!/^table/i.test(t))&&o.push("figure-caption");const h=(t||"").split(/\r?\n/),m=h.filter(t=>ft.test(t)).length,u=h.filter(t=>gt.test(t)).length;return m>=n&&o.push("table-rows"),u>=r&&o.push("numeric-grid"),{page:e+1,needsOcr:o.length>0,reasons:o,captions:c}}),a=o.filter(t=>t.needsOcr).map(t=>t.page);return{needsOcr:a.length>0,pagesNeedingOcr:a,pages:o}}var xt="onnx-community/granite-docling-258M-ONNX",vt="Convert this page to docling.",Et=null;async function Tt(t,e={}){const{processorUrl:s,prompt:n=vt,maxTokens:r=4096}=e;if(s){const e=await fetch(`${s.replace(/\/$/,"")}/api/v1/convert-base64`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({imageBase64:t,mimeType:"image/png",prompt:n,maxTokens:r})}),o=await e.json();if(!e.ok||!o?.success)throw new Error(o?.error||`Processor error ${e.status}`);return o.result}const{model:o,processor:a,lib:i}=await async function(){return Et||(Et=(async()=>{const t=await(import("@huggingface/transformers")),e=await t.AutoProcessor.from_pretrained(xt);return{model:await t.AutoModelForVision2Seq.from_pretrained(xt,{dtype:"fp32"}),processor:e,lib:t}})()),Et}(),c=await i.load_image(`data:image/png;base64,${t}`),l=[{role:"user",content:[{type:"image"},{type:"text",text:n}]}],h=await a(a.apply_chat_template(l,{add_generation_prompt:!0}),[c],{do_image_splitting:!0}),m=await o.generate({...h,max_new_tokens:r}),[u]=a.batch_decode(m.slice(null,[h.input_ids.dims.at(-1),null]),{skip_special_tokens:!0});return u}async function bt(t,e=2){const s=t.getViewport({scale:e}),n=await async function(t,e){if("undefined"!=typeof document){const s=document.createElement("canvas");return s.width=t,s.height=e,s}try{return(await(import("@napi-rs/canvas"))).createCanvas(t,e)}catch{}if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(t,e);throw new Error("No canvas available to rasterize PDF pages — install @napi-rs/canvas in Node.js, or run in a browser/Worker with OffscreenCanvas")}(Math.ceil(s.width),Math.ceil(s.height)),r=n.getContext("2d");return await t.render({canvasContext:r,viewport:s}).promise,async function(t){if("function"==typeof t.toBuffer)return t.toBuffer("image/png").toString("base64");if("function"==typeof t.convertToBlob){const e=await t.convertToBlob({type:"image/png"}),s=new Uint8Array(await e.arrayBuffer());let n="";for(const t of s)n+=String.fromCharCode(t);return btoa(n)}return t.toDataURL("image/png").split(",")[1]}(n)}async function Ot(t,e,s={}){const n=/* @__PURE__ */new Map;for(const o of e)try{const e=await Tt(await bt(await t.getPage(o),s.scale),s);n.set(o,St(e))}catch(r){console.error(`Docling OCR failed for page ${o}:`,r)}return n}function St(t){let e=(t||"").replace(/<\/?doctag>/g,"").replace(/<loc_\d+>/g,"").replace(/<page_break>/g,"").replace(/<end_of_utterance>/g,"").replace(/<page_(?:header|footer)>[\s\S]*?<\/page_(?:header|footer)>/g,"");e=e.replace(/<otsl>([\s\S]*?)<\/otsl>/g,(t,e)=>function(t){return`<table>${t.split("<nl>").map(t=>t.trim()).filter(Boolean).map(t=>{const e=[...t.matchAll(/<(fcel|ched|rhed|ecel|lcel|ucel|xcel)>([^<]*)/g)];return e.length?`<tr>${e.map(([,t,e])=>{const s="ched"===t||"rhed"===t?"th":"td";return`<${s}>${e.trim()}</${s}>`}).join("")}</tr>`:""}).filter(Boolean).join("")}</table>`}(e));for(const[s,n,r]of[[/<title>([\s\S]*?)<\/title>/g,"<h1>","</h1>"],[/<section_header_level_1>([\s\S]*?)<\/section_header_level_1>/g,"<h2>","</h2>"],[/<section_header_level_2>([\s\S]*?)<\/section_header_level_2>/g,"<h3>","</h3>"],[/<section_header_level_3>([\s\S]*?)<\/section_header_level_3>/g,"<h4>","</h4>"],[/<section_header_level_[4-9]>([\s\S]*?)<\/section_header_level_[4-9]>/g,"<h5>","</h5>"],[/<(?:text|paragraph)>([\s\S]*?)<\/(?:text|paragraph)>/g,"<p>","</p>"],[/<caption>([\s\S]*?)<\/caption>/g,"<figcaption>","</figcaption>"],[/<(?:picture|chart)>([\s\S]*?)<\/(?:picture|chart)>/g,"<figure>","</figure>"],[/<code>([\s\S]*?)<\/code>/g,"<pre><code>","</code></pre>"],[/<formula>([\s\S]*?)<\/formula>/g,'<code class="formula">',"</code>"],[/<footnote>([\s\S]*?)<\/footnote>/g,'<p class="footnote">',"</p>"],[/<list_item>([\s\S]*?)<\/list_item>/g,"<li>","</li>"],[/<unordered_list>([\s\S]*?)<\/unordered_list>/g,"<ul>","</ul>"],[/<ordered_list>([\s\S]*?)<\/ordered_list>/g,"<ol>","</ol>"]])e=e.replace(s,(t,e)=>`${n}${e.trim()}${r}`);return e=e.replace(/<\/?(?:[a-z][a-z0-9]*_[a-z0-9_]+|otsl|smiles)>/g,""),e.trim()}async function Lt(t,e={}){const s="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t,{LiteParse:n}=await(import("@llamaindex/liteparse")),r=await new n({quiet:!0,...e.liteParseOptions}).isComplex(new Uint8Array(s));return{needsOcr:r.some(t=>t.needsOcr),pages:r,reasons:[...new Set(r.flatMap(t=>t.reasons))]}}async function It(t,e={}){if("liteparse"===e.method){const{method:s,...n}=e;return ht(t,n)}if("liteparse-wasm"===e.method){const{method:s,...n}=e;return mt(t,n)}var{addPageNumbers:s=!1,addCitation:n=!0}=e;const r=e.processor??"frontend",o=/^https?:\/\//i.test(r),a=o?"docling":r,i={...e.doclingOptions,processorUrl:o?r:e.processorUrl};var c="string"==typeof t?await w(t,{responseType:"arraybuffer",timeout:10}):t;let l;try{const{getDocument:t}=await pt();l=await t({data:new Uint8Array(c),useSystemFonts:!0,verbosity:0}).promise}catch(k){return{error:k.message}}const h=[...Array(l.numPages).keys()].map(t=>new y({index:t}));let m,f={};for(let d=1;d<=l.numPages;d++){const t=await l.getPage(d),e=await t.getTextContent();if(!(Object.keys(f).length<10)){m=p(f);break}f=u(f,t.pageNumber-1,e.items)}let x=m?m.pageNum:0;for(let u=1;u<=l.numPages;u++){const t=await l.getPage(u);await t.getOperatorList();const e=t.getViewport({scale:1});let s=await t.getTextContent();m&&t.pageIndex>=m.pageIndex&&(s=d(s,x),x++);const n=s.items.map(t=>{const s=[1,0,0,1,0,0];for(let o=0;o<6;o++)s[o]+=t.transform[o]*e.transform[o%2?3:0],o%2&&(s[o+1]+=t.transform[o]*e.transform[1]);const n=Math.sqrt(s[2]*s[2]+s[3]*s[3]),r=t.height/n;return new g({x:Math.round(t.transform[4]),y:Math.round(t.transform[5]),width:Math.round(t.width),height:Math.round(r<=1?t.height:r),text:t.str,font:t.fontName})});h[t.pageNumber-1].items=n}const v=h.map(t=>Object.values(t.items.reduce((t,e)=>((t[e.y]=t[e.y]||[]).push(e.text),t),{})).map(t=>t.join(" ")).join("\n"));var E=new I({pages:h});let T;[new U,new q,new Y,new V,new K,new et,new tt,new rt,new ot,new it,new ct,new lt].forEach(t=>{T&&(E=T.completeTransform(E)),E=t.transform(E),T=t});var b=E.pages.map((t,e)=>`<p id="page-${e+1}">${s?` [${e+1}] `:""}${t.items.join('</p><p id="page-'+e+'">')}</p>`);const O=wt(v,e.ocrScanOptions);if("docling"===a||"hybrid"===a){const t="docling"===a?b.map((t,e)=>e+1):O.pagesNeedingOcr,e=await Ot(l,t,i);for(const[n,r]of e)b[n-1]=`<section class="ocr-page" id="page-${n}">${s?` [${n}] `:""}${r}</section>`}var S=b.join("");if(n){var{Author:L,Title:C}=(await l.getMetadata()).info;C=S.slice(0,400).match(/<h[0-9]>(.*?)<\/h[0-9]>/)?.[1]||C}return{author:L,title:C,html:S,format:"pdf",processor:a,ocrScan:O}}export{It as convertPDFToHTML,ht as convertPDFToHTMLWithLiteParse,mt as convertPDFToHTMLWithLiteParseWasm,Lt as detectPdfNeedsOcr,St as doctagsToHtml,pt as loadPdfJs,Tt as ocrImageWithDocling,Ot as ocrPdfPagesWithDocling,bt as renderPdfPageToPngBase64,wt as scanPagesForOCR};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extract-pdf",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.269",
|
|
4
4
|
"description": "Convert a PDF (URL or ArrayBuffer) into clean HTML with structural tagging — headings, lists, footnotes, code blocks, bold/italic. Slim by default (PDF.js loads at runtime from the pdfjs-serverless CDN build), with optional Granite Docling OCR for pages with infographics/tables — in-process, hybrid, or via a remote processor URL. Works in Node.js, Cloudflare Workers, and browser environments.",
|
|
5
5
|
"author": "vtempest <grokthiscontact@gmail.com>",
|
|
6
6
|
"license": "rights.institute/PROSPER",
|
|
@@ -32,22 +32,22 @@
|
|
|
32
32
|
"grab-url": "^1.6.22"
|
|
33
33
|
},
|
|
34
34
|
"optionalDependencies": {
|
|
35
|
-
"@hono/node-server": "^1.
|
|
36
|
-
"@hono/swagger-ui": "^0.
|
|
37
|
-
"@hono/zod-openapi": "^
|
|
38
|
-
"@huggingface/transformers": "^
|
|
39
|
-
"@llamaindex/liteparse": "^2.
|
|
40
|
-
"@llamaindex/liteparse-wasm": "^2.
|
|
41
|
-
"@napi-rs/canvas": "^0.
|
|
42
|
-
"hono": "^4.
|
|
43
|
-
"pdfjs-serverless": "^1.1
|
|
44
|
-
"zod": "^
|
|
35
|
+
"@hono/node-server": "^2.1.1",
|
|
36
|
+
"@hono/swagger-ui": "^0.6.1",
|
|
37
|
+
"@hono/zod-openapi": "^1.6.3",
|
|
38
|
+
"@huggingface/transformers": "^4.2.0",
|
|
39
|
+
"@llamaindex/liteparse": "^2.14.4",
|
|
40
|
+
"@llamaindex/liteparse-wasm": "^2.14.4",
|
|
41
|
+
"@napi-rs/canvas": "^1.0.8",
|
|
42
|
+
"hono": "^4.13.7",
|
|
43
|
+
"pdfjs-serverless": "^1.3.1",
|
|
44
|
+
"zod": "^4.5.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"terser": "^5.
|
|
48
|
-
"typescript": "^
|
|
49
|
-
"vite": "^8.
|
|
50
|
-
"vite-plugin-dts": "^5.0
|
|
47
|
+
"terser": "^5.51.2",
|
|
48
|
+
"typescript": "^7.0.2",
|
|
49
|
+
"vite": "^8.2.2",
|
|
50
|
+
"vite-plugin-dts": "^5.1.0"
|
|
51
51
|
},
|
|
52
52
|
"keywords": [
|
|
53
53
|
"pdf",
|