UNPKG

yuque-dl

Version:
2 lines (1 loc) 27 kB
import e,{writeFile as t,mkdir as o}from"node:fs/promises";import n from"node:path";import r from"rand-user-agent";import i from"log4js";import s from"cli-progress";import{env as a,stdout as l}from"node:process";import d from"axios";import{fromMarkdown as c}from"mdast-util-from-markdown";import{toMarkdown as u}from"mdast-util-to-markdown";import h from"ora";import f from"pull-md-img";import p from"markdown-toc";import m from"pako";import v from"node:crypto";import{createWriteStream as g,mkdirSync as y}from"node:fs";import*as k from"node:stream";import{promisify as w}from"node:util";function $(e,t,o,n){return new(o||(o=Promise))((function(r,i){function s(e){try{l(n.next(e))}catch(e){i(e)}}function a(e){try{l(n.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?r(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(s,a)}l((n=n.apply(e,t||[])).next())}))}var I,b;"function"==typeof SuppressedError&&SuppressedError,function(e){e.TITLE="title",e.LINK="link",e.DOC="doc"}(I||(I={})),function(e){e.BOARD="board",e.TABLE="table",e.SHEET="sheet",e.DOC="doc"}(b||(b={}));const D=new Map([[b.BOARD,"画板类型"],[b.TABLE,"数据表类型"],[b.SHEET,"表格类型"],[b.DOC,"文档类型"]]),E="_yuque_session",N="https://www.yuque.com",P="UXO91eVnUveQn8suOJaYMvBcWs9KptS8N5HoP8ezSeU4vqApZpy1CkPaTpkpQEx2W2mlhxL8zwS8UePwBgksUM0CTtAODbTTTDFD",S=(i.configure({appenders:{cheese:{type:"console",layout:{type:"pattern",pattern:"%[%c [%p]:%] %m%n"}}},categories:{default:{appenders:["cheese"],level:"trace"}}}),i.getLogger("yuque-dl"));class U{constructor(e,t,o=!1,n=!1){this.bookPath="",this.progressFilePath="",this.progressInfo=[],this.curr=0,this.total=0,this.isDownloadInterrupted=!1,this.bar=null,this.completePromise=null,this.bookPath=e,this.progressFilePath=`${e}/progress.json`,this.total=t,this.incremental=o,this.disableProgressJSON=n}init(){return $(this,void 0,void 0,(function*(){this.progressInfo=this.disableProgressJSON?[]:yield this.getProgress(),this.curr=this.incremental?0:this.progressInfo.length,this.curr!==this.total&&(this.curr>0&&this.curr!==this.total&&!this.incremental&&(this.isDownloadInterrupted=!0,S.info("根据上次数据继续断点下载")),this.bar=new s.SingleBar({format:"Download [{bar}] {percentage}% | {value}/{total}"},s.Presets.legacy),this.bar.start(this.total,this.curr))}))}getProgress(){return $(this,void 0,void 0,(function*(){let t=[];try{const o=yield e.readFile(this.progressFilePath,{encoding:"utf8"});t=JSON.parse(o)}catch(o){o&&"ENOENT"===o.code&&(yield e.writeFile(this.progressFilePath,JSON.stringify(t),{encoding:"utf8"}))}return t}))}updateProgress(t,o){return $(this,void 0,void 0,(function*(){if(this.curr!==this.total){if(this.curr=this.curr+1,o){const o=t.toc.uuid,n=this.progressInfo.find((e=>e.toc.uuid===o));n?Object.assign(n,t):this.progressInfo.push(t),this.disableProgressJSON||(yield e.writeFile(this.progressFilePath,JSON.stringify(this.progressInfo),{encoding:"utf8"}))}this.bar&&(this.bar.update(this.curr),this.curr>=this.total&&(this.bar.stop(),console.log("")))}}))}pause(){this.bar&&this.bar.stop()}continue(){var e;this.clearLine(2),null===(e=this.bar)||void 0===e||e.start(this.total,this.curr)}clearLine(e){var t;if(!(e<=0)&&"function"==typeof(null===(t=null===process||void 0===process?void 0:process.stderr)||void 0===t?void 0:t.cursorTo)){process.stderr.cursorTo(0);for(let t=0;t<e;t++)process.stderr.moveCursor(0,-1),process.stderr.clearLine(1)}}}function L({browser:e="chrome",os:t="mac os",device:o="desktop"}){o=o.toLowerCase(),e=e.toLowerCase(),t=t.toLowerCase();let n=r(o,e,t);if("chrome"===e)for(;n.includes("Chrome-Lighthouse")||n.includes("Gener8")||n.includes("HeadlessChrome")||n.includes("SMTBot");)n=r(o,e,t);if("safari"===e)for(;n.includes("Applebot");)n=r(o,e,t);return n}function T(e){if("function"==typeof URL.canParse)return URL.canParse(e);try{return new URL(e),!0}catch(e){return!1}}function O(e){return e.toString().padStart(2,"0")}function x(e){const t=new Date(e);if(isNaN(t.getTime()))return"";const o=t.getFullYear(),n=t.getMonth()+1,r=t.getDate(),i=t.getHours(),s=t.getMinutes(),a=t.getSeconds();return`${o}-${O(n)}-${O(r)} ${O(i)}:${O(s)}:${O(a)}`}function C(e){return e instanceof Date&&!isNaN(e.getTime())}class _{constructor(e){this.summaryInfo=e}genFile(){return $(this,void 0,void 0,(function*(){const{bookName:t,bookDesc:o,bookPath:n,uuidMap:r}=this.summaryInfo;let i=`# ${t}\n\n`;o&&(i+=`> ${o}\n\n`);let s=i;const a=[];r.forEach((e=>{const t=e.toc,o=t.parent_uuid,n=this.findTree(a,o),r={text:t.title.replace(/[\\/:*?"<>|\n\r]/g,"_").replace(/\s/,""),id:t.uuid,level:1,type:"link"},i=t.type.toLocaleLowerCase();i===I.TITLE||""!==t.child_uuid?(r.type="title","boolean"!=typeof n?(Array.isArray(n.children)||(n.children=[]),r.level=n.level+1,n.children.push(r)):(r.level=1,a.push(r)),i===I.DOC&&(r.link=e.path)):(r.type="link",r.link=i===I.LINK?e.toc.url:e.path,"boolean"!=typeof n?(Array.isArray(n.children)||(n.children=[]),r.level=n.level+1,n.children.push(r)):(r.level=1,a.push(r)))}));s+=this.genSummaryContent(a,"");try{yield e.writeFile(`${n}/index.md`,s)}catch(e){S.error("Generate Summary Error")}return s}))}genSummaryContent(e,t){for(const o of e){if(o.type===I.TITLE)if(o.link){const e=o.link?o.link.replace(/\s/g,"%20"):o.link;t+=`\n${"".padStart(o.level+1,"#")} [${o.text}](${e})\n\n`}else t+=`\n${"".padStart(o.level+1,"#")} ${o.text}\n\n`;else if(o.type===I.LINK){const e=o.link?o.link.replace(/\s/g,"%20"):o.link;t+=`${1===o.level?"\n##":"-"} [${o.text}](${e})\n`}Array.isArray(o.children)&&(t+=this.genSummaryContent(o.children,""))}return t}findIdItem(e,t){if(e.id===t)return e;if(e.children){const o=this.findTree(e.children,t);if(o)return o}return!1}findTree(e,t){if(!t)return!1;for(const o of e){const e=this.findIdItem(o,t);if(e)return e}return!1}}function A(e){const{key:t=E,token:o}=e,n={"user-agent":L({browser:"chrome",device:"desktop"})};return o&&(n.cookie=`${t}=${o};`),n}function F(e){const t={headers:A(e),beforeRedirect:t=>{t.headers=Object.assign(Object.assign({},(null==t?void 0:t.headers)||{}),A(e))}};return"test"===a.NODE_ENV&&(t.proxy=!1),t}const R=(e,t=!0)=>{const{articleUrl:o,bookId:n,token:r,key:i,host:s=N}=e;let a=`${s}/api/docs/${o}`;const l={book_id:String(n),merge_dynamic_data:String(!1)};t&&(l.mode="markdown");const c=new URLSearchParams(l).toString();return a=`${a}?${c}`,d.get(a,F({token:r,key:i})).then((({data:e,status:t})=>({apiUrl:a,httpStatus:t,response:e})))},j=(e,t)=>{const o=/decodeURIComponent\("(.+)"\)\);/m;return d.get(e,F(t)).then((({data:e="",status:t})=>200===t?e:"")).then((e=>{var t,n,r,i;const s=null!==(t=o.exec(e))&&void 0!==t?t:"";if(!s[1])return{};const a=JSON.parse(decodeURIComponent(s[1]));if(!a.doc)return{};return{docId:a.doc.id,docSlug:a.doc.slug,docTitle:a.doc.title||"",bookId:a.doc.book_id,bookSlug:(null===(n=a.book)||void 0===n?void 0:n.slug)||"",bookName:(null===(r=a.book)||void 0===r?void 0:r.name)||"",host:(null===(i=a.space)||void 0===i?void 0:i.host)||N,imageServiceDomains:a.imageServiceDomains||[]}})).catch((e=>{var t;const o=null!==(t=null==e?void 0:e.message)&&void 0!==t?t:"";if(!o)throw new Error("unknown error");if(["getaddrinfo ENOTFOUND","read ECONNRESET","Client network socket disconnected before secure TLS connection was established"].some((e=>o.startsWith(e))))throw new Error("请检查网络(是否正常联网/是否开启了代理软件)");throw new Error(o)}))};function M(e){return c(e)}function B(e){return u(e)}function J(e,t,o=[]){t(e,o),Array.isArray(e.children)&&(o.push("children"),e.children.forEach(((e,n)=>{J(e,t,[...o,String(n)])})))}function q(e){if(!e)return"";return e.replace(/[\\/:*?"<>|\n\r]/g,"_").replace(/\s/g,"").replace(/[\ud800-\udbff][\udc00-\udfff]/g,"")}function V(e,t){let o=e;const n=M(e),r=function(e){const t=[];return J(e,(function(e,o){"inlineCode"===e.type&&t.push({node:e,keyChain:o})})),t}(n);return 0===r.length?e:(r.forEach((e=>{const o=e.node,n=(r=o.value,/<([a-z][\s\S]*?)>/i.test(r));var r;const i=function(e){return/(~~|\*\*|_)/g.test(e)}(o.value);if(!n&&!i)return;const s=o.value.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");t.includes(s)||(o.type="html",o.value=`<code>${o.value}</code>`)})),o=B(n),o)}const H=e=>{if(!e)return"";const t=m.inflate(e,{to:"string"}),o=JSON.parse(t);let n="";return o.forEach((e=>{const t=`## ${e.name}\n`,o=function(e){var t,o,n;let r=Object.keys(e);r=r.filter((t=>Object.keys(e[t]).some((o=>{var n,r;return null===(r=null===(n=null==e?void 0:e[t])||void 0===n?void 0:n[o])||void 0===r?void 0:r.v}))));let i=[];r.forEach((t=>{const o=e[t];o&&(i=i.concat(Object.keys(o)))}));const s=Math.max(...r.map((e=>Number(e)))),a=Math.max(...i.map((e=>Number(e))));if(s<0||a<0)return"";let l="";const d="ABCDEFGHIJKLMNOPQRSTUVWXYZ";let c=Array(a+1).fill(" ").map(((e,t)=>{const o=t%d.length;return d[o]})).join(" | ");c=`| |${c}|`;let u=Array(a+2).fill("---").join(" |");u=`|${u}|`,l=`${c}\n${u}\n`;for(let r=0;r<s+1;r++){const i=[];for(let s=0;s<a+1;s++){const a=(null===(o=null===(t=null==e?void 0:e[r])||void 0===t?void 0:t[s])||void 0===o?void 0:o.v)||null;a&&"string"==typeof a?i.push(a):a&&"object"==typeof a?"image"===(null==a?void 0:a.class)&&(null==a?void 0:a.src)?i.push(`![${null==a?void 0:a.name}'](${null==a?void 0:a.src})`):"checkbox"===(null==a?void 0:a.class)?i.push((null==a?void 0:a.value)?"[x] ":"[ ] "):"link"===(null==a?void 0:a.class)?i.push(`[${null==a?void 0:a.text}](${null==a?void 0:a.url})`):"select"===(null==a?void 0:a.class)&&i.push(null===(n=null==a?void 0:a.value)||void 0===n?void 0:n.join(",")):i.push(null)}l=`${l}${`| ${r+1} | ${i.join(" | ")}|`}\n`}return l}(e.data);n=`${n}\n${t}\n${o}`})),n};function K(e,t=[]){return function(e,t){try{const{host:o,pathname:n}=new URL(e);return!t.includes(o)&&Boolean(n)}catch(e){return!1}}(e,t)?`https://www.yuque.com/api/filetransfer/images?url=${encodeURIComponent(e)}&sign=${function(e){const t=v.createHash("sha256");return t.update(`${P}${e}`),t.digest("hex")}(e)}`:e}const W=w(k.finished);function z(e){return $(this,void 0,void 0,(function*(){const{fileUrl:t,savePath:o,token:n,key:r,fileName:i}=e;return d.get(t,Object.assign(Object.assign({},F({token:n,key:r})),{responseType:"stream"})).then((e=>$(this,void 0,void 0,(function*(){var n,r,s;if(null===(r=null===(n=e.request)||void 0===n?void 0:n.path)||void 0===r?void 0:r.startsWith("/login"))throw new Error(`"${i}" need token`);if(200===e.status){const n=g(o);return null===(s=e.data)||void 0===s||s.pipe(n),W(n).then((()=>({fileUrl:t,savePath:o})))}throw new Error(`response status ${e.status}`)}))))}))}const G=/\[(.*?)\]\((.*?)\)/g,Q=/\[(.*?)\]\((.*?\.yuque\.com\/attachments.*?)\)/;function Y(e){return $(this,void 0,void 0,(function*(){const{mdData:t,savePath:o,attachmentsDir:r,articleTitle:i,token:s,key:d,ignoreAttachments:c}=e,u=(t.match(G)||[]).filter((e=>Q.test(e)));if(0===u.length)return{mdData:t};const f=n.resolve(o,r);let p=u.map((e=>function(e,t){const[,o,r]=Q.exec(e)||[];if(!r)return!1;const i=o||r.split("/").at(-1);if(!i)return!1;const s=n.join(t,i);return{fileName:i,url:r,rawMd:e,currentFilePath:s}}(e,f))).filter((e=>!1!==e));if("string"==typeof c){const e=c.split(",");p=p.filter((t=>{const o=t.url.lastIndexOf(".");if(-1===o)return!0;const n=t.url.slice(o+1);return!e.find((e=>e===n))}))}if(0===p.length)return{mdData:t};const m=h({text:`下载 "${i}" 的附件中...`,stream:l});"test"!==a.NODE_ENV&&m.start(),y(f,{recursive:!0});const v=p.map((e=>z({fileUrl:e.url,savePath:e.currentFilePath,token:s,key:d,fileName:e.fileName}))),g=yield Promise.all(v).finally((function(){m&&m.stop()}));let k=t;return g.forEach((e=>{const t=p.find((t=>t.url===e.fileUrl));if(t){const e=`[附件: ${t.fileName}](${r}/${t.fileName})`;k=k.replace(t.rawMd,e)}})),{mdData:k}}))}function X(e){return $(this,void 0,void 0,(function*(){const{mdData:t,htmlData:o,savePath:r,attachmentsDir:i,articleTitle:s,token:d,key:c,ignoreAttachments:u}=e,f=M(t),p=function(e){const t=[];return J(e,(function(e,o){"link"===e.type&&t.push({node:e,keyChain:o})})),t}(f);let m=p.filter((e=>/_lake_card.*?videoId/.test(e.node.url))),v=ne(o,"audio"),g=ne(o,"video");if(0===m.length&&0===v.length&&0===g.length)return{mdData:t};if("string"==typeof u){const e=u.split(",");if(m=m.filter((t=>{const o=t.node.url,n=o.lastIndexOf(".");if(-1===n)return!0;const r=o.slice(n+1);return!e.find((e=>e===r))})),v=v.filter((t=>{const o=t.audioId.lastIndexOf(".");if(-1===o)return!0;const n=t.audioId.slice(o+1);return!e.find((e=>e===n))})),g=g.filter((t=>{const o=t.videoId.lastIndexOf(".");if(-1===o)return!0;const n=t.videoId.slice(o+1);return!e.find((e=>e===n))})),0===m.length&&0===v.length&&0===g.length)return{mdData:t}}const k=h({text:`下载 "${s}" 的音视频中...`,stream:l});"test"!==a.NODE_ENV&&k.start();const w=n.resolve(r,i);y(w,{recursive:!0});let I=t;try{if(m.length>0){const t=yield function(e,t,o){return $(this,void 0,void 0,(function*(){const{key:r,token:i}=t,s=e.map((e=>$(this,void 0,void 0,(function*(){var t,s;const a=function(e){var t;try{const o=null!==(t=new URL(e).searchParams.get("_lake_card"))&&void 0!==t?t:"",n=decodeURIComponent(o),r=JSON.parse(n);return{name:(null==r?void 0:r.name)||"",videoId:(null==r?void 0:r.videoId)||""}}catch(e){return!1}}(e.node.url);if(!a)return!1;const l=yield Z({videoId:a.videoId,key:r,token:i});if(!l)return!1;const d=null!==(s=null!==(t=a.name)&&void 0!==t?t:a.videoId.split("/").at(-1))&&void 0!==s?s:a.videoId;return{videoInfo:Object.assign(Object.assign({},a),l),astNode:e,fileName:d,currentFilePath:n.join(o,d)}}))));return(yield Promise.all(s)).filter(ee)}))}(m,e,w),o=t.map((e=>z({fileUrl:e.videoInfo.video,savePath:e.currentFilePath,token:d,key:c,fileName:e.videoInfo.name})));(yield Promise.all(o)).forEach((e=>{const o=t.find((t=>t.videoInfo.video===e.fileUrl));o&&(o.astNode.node.url=`${i}${n.sep}${o.fileName}`,o.astNode.node.children=[{type:"text",value:`音视频附件: ${o.videoInfo.name}`}])})),I=B(f)}if(v.length>0||g.length>0){const t=yield re(v,e,w,"audio"),o=yield re(g,e,w,"video"),r=[...t,...o].map((e=>$(this,void 0,void 0,(function*(){const t="audio"===e.type?e.videoInfo.fileName:e.videoInfo.name,o={fileUrl:"audio"===e.type?e.videoInfo.audio:e.videoInfo.video,savePath:e.currentFilePath,token:d,key:c,fileName:t};return Object.assign(Object.assign({},yield z(o)),{id:e.videoInfo.id,fileName:t})}))));(yield Promise.all(r)).forEach((e=>{const t=p.find((t=>new RegExp(`#${e.id}`,"gm").test(t.node.url)));t&&(t.node.url=`${i}${n.sep}${e.fileName}`,t.node.children=[{type:"text",value:`音视频附件: ${e.fileName}`}])})),I=B(f)}}finally{k&&k.stop()}return{mdData:I}}))}function Z(e){let t="https://www.yuque.com/api/video";const{videoId:o,token:n,key:r}=e,i=new URLSearchParams;return i.set("video_id",o),t=`${t}?${i.toString()}`,d.get(t,F({token:n,key:r})).then((({data:e,status:t})=>{const o=e.data;return 200===t&&"success"===o.status&&o.info})).catch((()=>!1))}function ee(e){return!!e}const te=/name="audio" value="data:(.*?audioId.*?)".*?><\/card>/gm,oe=/name="video" value="data:(.*?videoId.*?)".*?><\/card>/gm;function ne(e,t){const o="video"===t?oe:te,n=e.match(o)||[];try{return n.map((e=>e.replace(o,"$1"))).map((e=>JSON.parse(decodeURIComponent(e))))}catch(e){return[]}}function re(e,t,o,r){return $(this,void 0,void 0,(function*(){const{key:i,token:s}=t,a=e.map((e=>$(this,void 0,void 0,(function*(){var t;const a=yield Z({videoId:"audio"===r?e.audioId:e.videoId,key:i,token:s});if(!a)return!1;const l=null!==(t="audio"===r?null==e?void 0:e.fileName:e.name)&&void 0!==t?t:e.id;return{videoInfo:Object.assign(Object.assign({},e),a),type:r,fileName:l,currentFilePath:n.join(o,l)}}))));return(yield Promise.all(a)).filter(ee)}))}function ie(e){return $(this,void 0,void 0,(function*(){var o,n,r,i,s,d,c,u,p,m;const{articleInfo:v,progressBar:g,options:y,progressItem:k,oldProgressItem:w}=e,{token:$,key:I,convertMarkdownVideoLinks:E,hideFooter:N,ignoreImg:P,ignoreAttachments:S}=y,{bookId:U,itemUrl:L,savePath:T,saveFilePath:O,uuid:_,articleUrl:A,articleTitle:F,host:j,imageServiceDomains:M}=v,B={articleUrl:L,bookId:U,token:$,host:j,key:I},{httpStatus:J,apiUrl:W,response:z}=yield R(B);!function(){var e,t,o,n;const r=(null===(e=null==z?void 0:z.data)||void 0===e?void 0:e.created_at)||"",i=(null===(t=null==z?void 0:z.data)||void 0===t?void 0:t.content_updated_at)||"",s=(null===(o=null==z?void 0:z.data)||void 0===o?void 0:o.published_at)||"",a=(null===(n=null==z?void 0:z.data)||void 0===n?void 0:n.first_published_at)||"";k.createAt=r,k.contentUpdatedAt=i,k.publishedAt=s,k.firstPublishedAt=a}();const{needDownload:G,isUpdateDownload:Q}=function(e,t){const o={isFirstDownload:!0,isUpdateDownload:!1,needDownload:!0};if(!e.contentUpdatedAt||!t||!(null==t?void 0:t.contentUpdatedAt))return o;const n=new Date(e.contentUpdatedAt),r=new Date(t.contentUpdatedAt);if(!C(n)||!C(r))return o;if(n.getTime()>r.getTime())return{needDownload:!0,isUpdateDownload:!0,isFirstDownload:!1};if(n.getTime()===r.getTime())return{needDownload:!1,isUpdateDownload:!1,isFirstDownload:!1};return o}(k,w);if(!G)return{needDownload:G,isUpdateDownload:Q,isDownloadFinish:!0};const Z=null===(n=null===(o=null==z?void 0:z.data)||void 0===o?void 0:o.type)||void 0===n?void 0:n.toLocaleLowerCase();let ee="";if(Z===b.SHEET){const{response:e}=yield R(B,!1);try{const t=null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.content,o=t?JSON.parse(t):{},n=null==o?void 0:o.sheet;ee=n?H(n):""}catch(e){const t=D.get(Z);throw new Error(`download article Error: “${t}”解析错误 ${e}`)}}else{if([b.BOARD,b.TABLE].includes(Z)){const e=D.get(Z);throw new Error(`download article Error: 暂不支持“${e}”的文档`)}if("string"!=typeof(null===(i=null==z?void 0:z.data)||void 0===i?void 0:i.sourcecode))throw new Error(`download article Error: ${W}, http status ${J}`);ee=z.data.sourcecode,ee=function(e){const t=/!\[(.*?)\]\((http.*?latex.*?)\)/gm,o=e.match(t);let n=e;const r=e;try{null==o||o.forEach((e=>{var o,r;t.lastIndex=0;const i=null!==(r=null===(o=t.exec(e))||void 0===o?void 0:o[2])&&void 0!==r?r:"",{pathname:s,search:a}=new URL(i);if(!s.endsWith(".svg")&&a){const t=decodeURIComponent(a);n=n.replace(e,t.slice(1))}}))}catch(e){return r}return n}(ee)}const te=function(e){if(!e)return[];const t=/!\[(.*?)\]\((.*?)\)/gm;let o=Array.from(e.match(t)||[]);return o=o.map((e=>(e=e.replace(t,"$2"),/^http.*/g.test(e)?e:""))).filter((e=>Boolean(e))),o}(ee),oe=null!==(c=null===(d=null===(s=(yield R(B,!1)).response)||void 0===s?void 0:s.data)||void 0===d?void 0:d.content)&&void 0!==c?c:"";te.length&&!P&&(ee=function(e,t,o){if(!o)return t;const n=/<card.*?name="image".*?value="data:(.*?)">(.*?)<\/card>/gm,r=[];let i,s=!0;for(;s||Boolean(i);)if(s=!1,i=n.exec(o),null==i?void 0:i[1])try{const e=decodeURIComponent(i[1]),t=JSON.parse(e);r.push((null==t?void 0:t.src)||"")}catch(e){r.push("")}const a=new Map;return e.forEach((e=>{const{origin:o,pathname:n}=new URL(e),i=`${o}${n}`,s=r.find(((e,t)=>{const o=new RegExp(`${i}.*?`).test(e);return o&&r.splice(t,1),o}));if(s){const o=new RegExp(e,"g"),n=a.get(e)||0;let r=0;t=t.replace(o,(e=>{let t=e;return r===n&&(t=s),r+=1,t})),a.set(e,n+1)}})),t}(te,ee,oe));const ne={toc:y.toc,articleTitle:F,articleUrl:A,articleUpdateTime:x(null!==(p=null===(u=null==z?void 0:z.data)||void 0===u?void 0:u.content_updated_at)&&void 0!==p?p:""),convertMarkdownVideoLinks:E,hideFooter:N},re=[];if(!S||"string"==typeof S)try{g.pause(),console.log("");ee=(yield Y({mdData:ee,savePath:T,attachmentsDir:`./attachments/${q(_)}`,articleTitle:F,token:$,key:I,ignoreAttachments:S})).mdData}catch(e){re.push(`附件下载失败: ${e.message||"unknown error"}`)}finally{g.continue()}if(!S||"string"==typeof S)try{g.pause(),console.log("");ee=(yield X({mdData:ee,htmlData:oe,savePath:T,attachmentsDir:`./attachments/${q(_)}`,articleTitle:F,token:$,key:I,ignoreAttachments:S})).mdData}catch(e){re.push(`音视频下载失败: ${e.message||"unknown error"}`)}finally{g.continue()}if(te.length&&!P){let e;g.pause(),console.log(""),"test"!==a.NODE_ENV&&(e=h({text:`下载 "${F}" 的图片中...`,stream:l}),e.start());let o=[],n=ee;try{const e=yield f.run(ee,{dist:T,imgDir:`./img/${_}`,isIgnoreConsole:!0,errorStillReturn:!0,referer:A||"",transform:e=>K(e=e.replace("x-oss-process=image%2Fwatermark%2C",""),M),timeout:18e4});o=e.errorInfo,n=e.data}catch(e){o=[e]}ee=n;const r=()=>{e&&e.stop(),g.continue()};if(o.length>0){const e=o[0];let n="图片下载失败(失败的以远程链接保存): ";throw n=e.url?`${n}${null===(m=e.error)||void 0===m?void 0:m.message} ${e.url.slice(0,20)}...`:`${n}${e.message}`,yield t(O,se(ee,ne)),r(),new Error(`${n}`)}r()}try{if(ee=V(ee,oe),yield t(O,se(ee,ne)),re.length>0)throw new Error(re[0]);return{needDownload:G,isUpdateDownload:Q,isDownloadFinish:!0}}catch(e){throw new Error(`${e.message}`)}}))}function se(e,t){const{articleTitle:o,articleUrl:n,toc:r,convertMarkdownVideoLinks:i,hideFooter:s}=t;let a=e;a=a.replace(/<a\b[^>]*?>(\s*?)<\/a>/gm,"");const l=o?`# ${o}\n\n`:"";let d=r?p(a).content:"";d&&(d=`${d}\n\n---\n\n`);let c="";return s||(c="\n\n",t.articleUpdateTime&&(c+=`> 更新: ${t.articleUpdateTime} \n`),n&&(c+=`> 原文: <${n}>`)),a=`${l}${d}${a}${c}`,i&&(a=function(e){return e.replace(/\[(.*?)\]\((.*?)\.(mp4|mp3)\)/gm,((e,t,o,n)=>{const r="mp3"===n?"audio":"video";return`<${r} controls width="800" alt="${t}" src="${o+"."+n}"></${r}>`}))}(a)),a}function ae(e,t){return $(this,void 0,void 0,(function*(){if(!T(e))throw new Error("Please enter a valid URL");const{bookId:r,tocList:i,bookName:s,bookDesc:a,bookSlug:l,host:c,imageServiceDomains:u}=yield((e,t)=>{const o=/decodeURIComponent\("(.+)"\)\);/m;return d.get(e,F(t)).then((({data:e="",status:t})=>200===t?e:"")).then((e=>{var t,n;const r=null!==(t=o.exec(e))&&void 0!==t?t:"";if(!r[1])return{};const i=JSON.parse(decodeURIComponent(r[1]));return i.book?{bookId:i.book.id,bookSlug:i.book.slug,tocList:i.book.toc||[],bookName:i.book.name||"",bookDesc:i.book.description||"",host:(null===(n=i.space)||void 0===n?void 0:n.host)||N,imageServiceDomains:i.imageServiceDomains||[]}:{}})).catch((e=>{var t;const o=null!==(t=null==e?void 0:e.message)&&void 0!==t?t:"";if(!o)throw new Error("unknown error");if(["getaddrinfo ENOTFOUND","read ECONNRESET","Client network socket disconnected before secure TLS connection was established"].some((e=>o.startsWith(e))))throw new Error("请检查网络(是否正常联网/是否开启了代理软件)");throw new Error(o)}))})(e,{token:t.token,key:t.key});if(!r)throw new Error("No found book id");if(!i||0===i.length)throw new Error("No found toc list");const h=n.resolve(t.distDir,s?q(s):String(r));yield o(h,{recursive:!0});const f=i.length,p=new U(h,f,t.incremental);if(yield p.init(),!t.incremental&&p.curr==f)return p.bar&&p.bar.stop(),void S.info(`√ 已完成: ${h}`);const m=new Map;(p.isDownloadInterrupted||t.incremental)&&p.progressInfo.forEach((e=>{m.set(e.toc.uuid,e)}));const v=e.replace(new RegExp(`(.*?/${l}).*`),"$1");yield function(e){return $(this,void 0,void 0,(function*(){const{articleUrlPrefix:t,total:r,uuidMap:i,tocList:s,bookPath:a,bookId:l,progressBar:d,host:c,options:u,imageServiceDomains:h=[]}=e;let f=0,p=0,m=0;const v=[],g=[],y=[];for(let e=0;e<r;e++){const t=s[e];if("string"!=typeof t.type)continue;const n=t.type.toLocaleLowerCase();if(n===I.TITLE||""!==t.child_uuid||n===I.LINK){let e=t;const r=[],s=[];for(;e;)r.unshift(q(e.title)),s.unshift(e.uuid),e=i.get(e.parent_uuid)?i.get(e.parent_uuid).toc:void 0;const l={path:r.map(q).join("/"),pathTitleList:r,pathIdList:s,toc:t};n===I.LINK?(m+=1,g.push(l)):yield o(`${a}/${r.map(q).join("/")}`,{recursive:!0}),n===b.DOC?yield k(t):(i.set(t.uuid,l),yield d.updateProgress(l,n!==I.LINK))}else t.url&&(yield k(t))}function k(e){return $(this,void 0,void 0,(function*(){p+=1;let o={path:"",pathTitleList:[],pathIdList:[]};const r=e.type.toLocaleLowerCase();i.get(e.parent_uuid)&&(o=i.get(e.parent_uuid));const s=q(e.title),m=[...o.pathTitleList,s],g=[...o.pathIdList,e.uuid];let k=[...o.pathTitleList,`${s}.md`].map(q).join("/"),w=o.pathTitleList.map(q).join("/");r===b.DOC&&e.child_uuid&&(k=[...o.pathTitleList,s,"index.md"].map(q).join("/"),w=m.map(q).join("/"));const $={path:k,savePath:w,pathTitleList:m,pathIdList:g,toc:e};let I=!0;const D=`${t}/${e.url}`;try{const t={bookId:l,itemUrl:e.url,savePath:n.resolve(a,$.savePath),saveFilePath:n.resolve(a,$.path),uuid:e.uuid,articleUrl:D,articleTitle:e.title,host:c,imageServiceDomains:h},{isUpdateDownload:o}=yield ie({articleInfo:t,progressBar:d,options:u,progressItem:$,oldProgressItem:i.get(e.uuid)});o&&y.push({progressItem:$,articleInfo:t})}catch(e){I=!1,f+=1,v.push({articleUrl:D,errItem:$,errMsg:e.message,err:e})}i.set(e.uuid,$),yield d.updateProgress($,I)}))}if(m>0){S.warn("该知识库存在以下外链文章");for(const e of g)S.warn(`———— ✕ ${e.path} ${e.toc.url}`)}if(f>0){S.error(`本次执行总数${p}篇,✕ 失败${f}篇`);for(const e of v)S.error(`《${e.errItem.path}》: ${e.articleUrl}`),e.errMsg.split("\n").forEach((e=>{S.error(`———— ✕ ${e}`)}));S.error("o(╥﹏╥)o 由于网络波动或链接失效以上下载失败,可重新执行命令重试(PS:不会影响已下载成功的数据)")}y.length>0&&(S.info("以下文档有更新: "),y.forEach((e=>{S.info(`———— √ ${e.articleInfo.saveFilePath}`)})))}))}({articleUrlPrefix:v,total:f,uuidMap:m,tocList:i,bookPath:h,bookId:r,progressBar:p,host:c,options:t,imageServiceDomains:u});const g=new _({bookPath:h,bookName:s,bookDesc:a,uuidMap:m});yield g.genFile(),S.info(`√ 生成目录 ${n.resolve(h,"index.md")}`),p.curr===f&&S.info(`√ 已完成: ${h}`)}))}function le(e,t){return $(this,void 0,void 0,(function*(){const r=Array.isArray(e)?e:[e];if(!r||0===r.length)throw new Error("Please provide at least one document URL");for(const e of r)if(!T(e))throw new Error(`Invalid URL: ${e}`);const i=r.length,s=n.resolve(t.distDir);yield o(s,{recursive:!0});const a=new U(s,i,!1,!0);yield a.init();let l=0;const d=[],c=[];for(let e=0;e<r.length;e++){const o=r[e];let i;try{const e=yield j(o,{token:t.token,key:t.key}),{docId:r,docSlug:l,docTitle:d,bookId:u,bookSlug:h,host:f,imageServiceDomains:p=[]}=e;if(!r||!u||!l)throw new Error("Failed to get document info from URL");const m=q(d||l),v=s,g=n.resolve(s,`${m}.md`);i={path:`${m}.md`,pathTitleList:[m],pathIdList:[String(r)],toc:{type:"DOC",title:d||l,uuid:String(r),url:l,prev_uuid:"",sibling_uuid:"",child_uuid:"",parent_uuid:"",doc_id:r,level:0,id:r,open_window:0,visible:1}};const y=h?`${f}/${h}/${l}`:o,k={bookId:u,itemUrl:l,savePath:v,saveFilePath:g,uuid:String(r),articleUrl:y,articleTitle:d||l,host:f,imageServiceDomains:p};yield ie({articleInfo:k,progressBar:a,options:t,progressItem:i}),yield a.updateProgress(i,!0),c.push(g)}catch(e){i&&(yield a.updateProgress(i,!1)),l+=1;const t=e.message||"unknown error";d.push({url:o,error:t})}}a.bar&&a.bar.stop(),c.forEach((e=>{S.info(`√ 已完成: ${e}`)})),l>0&&d.forEach((({url:e,error:t})=>{S.error(`✕ 下载失败: ${e}`),S.error(`———— ${t}`)}))}))}export{$ as _,le as d,S as l,ae as m};