| 1 | import{_ as c}from"./index-DFMl_77X.js";async function y(o){var g;const i=o.name.split(".").pop().toLowerCase();let s=[],l=[];if(["txt","md"].includes(i))s=[{page:1,text:(await o.text()).replace(/^\uFEFF/,"")}];else if(i==="docx"){const e=await c(()=>import("./mammoth.browser-D35GFhkM.js").then(m=>m.m),[],import.meta.url),r=await(e.default||e).extractRawText({arrayBuffer:await o.arrayBuffer()});s=[{page:1,text:r.value}],r.messages.length&&l.push("文档含复杂格式,请核对提取文字是否完整。")}else if(i==="pdf"){const e=await c(()=>import("./pdf-CcZYcL52.js"),[],import.meta.url),{default:r}=await c(async()=>{const{default:t}=await import("./pdf.worker.min-DgRcL-GR.js");return{default:t}},[],import.meta.url);e.GlobalWorkerOptions.workerSrc=r;const n=await e.getDocument({data:new Uint8Array(await o.arrayBuffer()),isEvalSupported:!1,useSystemFonts:!0}).promise;let p=0;try{for(let t=1;t<=Math.min(n.numPages,600);t++){const a=await n.getPage(t),f=await a.getTextContent();let u,w="";for(const d of f.items){if(!("str"in d))continue;const h=(g=d.transform)==null?void 0:g[5];w+=(u!==void 0&&Math.abs(h-u)>3?` |
| 2 | `:" ")+d.str,u=h}w.trim().length<10&&p++,s.push({page:t,text:w.trim()}),a.cleanup()}n.numPages>600&&l.push("仅导入前 600 页,请把剩余页分册上传。"),p&&l.push(`${p} 页未提取到足够文字,扫描件需先 OCR。`)}finally{await n.destroy()}}else if(i==="pptx"){const{unzipSync:e,strFromU8:r}=await c(async()=>{const{unzipSync:t,strFromU8:a}=await import("./browser-B-cVdNk9.js");return{unzipSync:t,strFromU8:a}},[],import.meta.url);let m=0;const n=e(new Uint8Array(await o.arrayBuffer()),{filter:t=>{if(!/^ppt\/slides\/slide\d+\.xml$/.test(t.name))return!1;if(m+=t.originalSize,m>30*1024*1024)throw new Error("幻灯片文字解压后过大,请分册上传。");return!0}});s=Object.keys(n).sort((t,a)=>Number(t.match(/slide(\d+)/)[1])-Number(a.match(/slide(\d+)/)[1])).map((t,a)=>{const f=new DOMParser().parseFromString(r(n[t]),"application/xml");return{page:a+1,text:Array.from(f.getElementsByTagName("a:t")).map(u=>u.textContent).join(` |
| 3 | `)}})}else throw new Error("支持 PDF、DOCX、PPTX、TXT、Markdown。");const _=s.reduce((e,r)=>e+r.text.length,0);if(_<30)throw new Error("未提取到有效文字,扫描版教材请先 OCR。");if(_>1e6)throw new Error("文本超过 100 万字符,请分册上传。");return{name:o.name,format:i,pages:s,warnings:l}}export{y as parseMaterial}; |