flatulentfowl_docdrop/main.js

3 lines
11 KiB
JavaScript
Raw Normal View History

2026-05-12 21:21:22 +00:00
var g=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var I=(a,t)=>{for(var e in t)g(a,e,{get:t[e],enumerable:!0})},b=(a,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of k(t))!D.call(a,o)&&o!==e&&g(a,o,{get:()=>t[o],enumerable:!(s=P(t,o))||s.enumerable});return a};var x=a=>b(g({},"__esModule",{value:!0}),a);var T={};I(T,{default:()=>p});module.exports=x(T);var i=require("obsidian"),f=require("child_process"),v=new Set(["pdf","docx","doc","pptx","ppt","xlsx","xls","jpg","jpeg","png","gif","webp","bmp","tiff","html","htm","csv","json","xml","epub","zip","mp3","wav"]),E={executablePath:"markitdown",outputMode:"same",customOutputFolder:"",keepDataUris:!1,usePlugins:!1,openAiApiKey:"",openAiBaseUrl:"",openAiModel:"gpt-4o",useDocIntel:!1,docIntelEndpoint:"",docIntelApiKey:"",charset:"",mimeType:""},h=class extends i.Modal{constructor(t,e,s,o){super(t),this.fileName=e,this.onConfirm=s,this.onCancel=o}onOpen(){let{contentEl:t}=this;t.empty(),new i.Setting(t).setName("File already exists").setHeading(),t.createEl("p",{text:`"${this.fileName}" already exists. Overwrite it?`});let e=t.createDiv({cls:"modal-button-container"});e.createEl("button",{text:"Overwrite",cls:"mod-warning"}).addEventListener("click",()=>{this.close(),this.onConfirm()}),e.createEl("button",{text:"Cancel"}).addEventListener("click",()=>{this.close(),this.onCancel()})}onClose(){this.contentEl.empty()}},m=class extends i.PluginSettingTab{constructor(t,e){super(t,e),this.plugin=e}display(){let{containerEl:t}=this;t.empty(),new i.Setting(t).setName("Executable path").setDesc('Full path to the markitdown binary, or just "markitdown" if it is on your PATH.').addText(e=>e.setPlaceholder("markitdown").setValue(this.plugin.settings.executablePath).onChange(s=>{this.plugin.settings.executablePath=s.trim()||"markitdown",this.plugin.saveSettings()})),new i.Setting(t).setName("Output location").setDesc("Where to save converted Markdown files.").addDropdown(e=>e.addOption("same","Same folder as PDF").addOption("custom","Custom folder").setValue(this.plugin.settings.outputMode).onChange(s=>{this.plugin.settings.outputMode=s,this.plugin.saveSettings(),this.display()})),this.plugin.settings.outputMode==="custom"&&new i.Setting(t).setName("Custom output folder").setDesc('Vault-relative path, e.g. "Converted". The folder must already exist.').addText(e=>e.setPlaceholder("Converted").setValue(this.plugin.settings.customOutputFolder).onChange(s=>{this.plugin.settings.customOutputFolder=s.trim(),this.plugin.saveSettings()})),new i.Setting(t).setName("Conversion").setHeading(),new i.Setting(t).setName("Keep images").setDesc("Preserve images embedded in the PDF as base64 data inside the Markdown file. Turning this on makes the output file much larger but keeps all visuals inline. Leave off if you only need the text.").addToggle(e=>e.setValue(this.plugin.settings.keepDataUris).onChange(s=>{this.plugin.settings.keepDataUris=s,this.plugin.saveSettings()})),new i.Setting(t).setName("MIME type hint").setDesc('Tells markitdown what kind of file it is receiving, e.g. "application/pdf". Normally not needed \u2014 markitdown detects the type automatically. Only set this if conversion produces wrong results and you suspect a detection failure.').addText(e=>e.setPlaceholder("application/pdf").setValue(this.plugin.settings.mimeType).onChange(async s=>{this.plugin.settings.mimeType=s.trim(),await this.plugin.saveSettings()})),new i.Setting(t).setName("Character encoding hint").setDesc('Tells markitdown what text encoding the file uses, e.g. "UTF-8" or "ISO-8859-1". Leave blank unless converted text contains garbled characters \u2014 markitdown detects encoding automatically.').addText(e=>e.setPlaceholder("UTF-8").setValue(this.plugin.settings.charset).onChange(s=>{this.plugin.settings.charset=s.trim(),this.plugin.saveSettings()})),new i.Setting(t).setName("markitdown-ocr plugin (optional)").setHeading(),t.createEl("p",{text:"markitdown-ocr is a free, separately-installed plugin that uses an AI
${r}`,8e3),console.error("[DocDrop] conversion error",n)}}invokeMarkItDown(t){var r,c;let e=["/usr/local/bin","/opt/homebrew/bin","/opt/homebrew/sbin","/Library/Frameworks/Python.framework/Versions/3.12/bin","/Library/Frameworks/Python.framework/Versions/3.11/bin","/Library/Frameworks/Python.framework/Versions/3.10/bin","/usr/bin","/bin"],s=[...(c=(r=process.env.PATH)==null?void 0:r.split(":"))!=null?c:[],...e].join(":"),o=[t];this.settings.keepDataUris&&o.push("--keep-data-uris"),this.settings.usePlugins&&o.push("--use-plugins"),this.settings.charset&&o.push("--charset",this.settings.charset),this.settings.useDocIntel&&(o.push("--use-docintel"),this.settings.docIntelEndpoint&&o.push("--endpoint",this.settings.docIntelEndpoint));let n={PATH:s};return this.settings.useDocIntel&&this.settings.docIntelApiKey&&(n.AZURE_API_KEY=this.settings.docIntelApiKey),this.settings.usePlugins&&this.settings.openAiApiKey&&(n.OPENAI_API_KEY=this.settings.openAiApiKey),this.settings.usePlugins&&this.settings.openAiModel&&(n.OPENAI_MODEL=this.settings.openAiModel),this.settings.usePlugins&&this.settings.openAiBaseUrl&&(n.OPENAI_BASE_URL=this.settings.openAiBaseUrl),new Promise((u,w)=>{(0,f.execFile)(this.settings.executablePath,o,{encoding:"utf8",maxBuffer:50*1024*1024,env:{...process.env,...n}},(d,A,l)=>{if(d){let y=(l==null?void 0:l.trim())||d.message;w(new Error(`Exit code ${d.code}: ${y}`));return}l!=null&&l.trim()&&console.warn("[DocDrop] stderr:",l.trim()),u(A)})})}};