UNPKG

yuque-dl

Version:
2 lines (1 loc) 5.54 kB
import{_ as e,l as o,m as n}from"./index-ConFUJXT.js";import{existsSync as t,readFileSync as i}from"node:fs";import{cac as r}from"cac";import s from"semver";import"rand-user-agent";import{mkdir as d,copyFile as c,readFile as l,writeFile as m,access as p,readdir as a,stat as u}from"node:fs/promises";import"cli-progress";import{resolve as f,join as h,sep as v,dirname as y}from"node:path";import{fileURLToPath as x}from"node:url";import{createServer as k}from"vitepress";import"log4js";import"node:process";import"axios";import"ora";import"pull-md-img";import"markdown-toc";import"pako";import"node:crypto";import"node:stream";import"node:util";import"mdast-util-from-markdown";import"mdast-util-to-markdown";let g;function w(o,n){return e(this,void 0,void 0,(function*(){const i=f(o);if(!(yield j(i)))throw new Error("server root not found");const r=h(i,"/.vitepress");t(r)&&!n.force||(yield function(o){return e(this,void 0,void 0,(function*(){const n=o.split(v).filter(Boolean).at(-1)||"yuque-dl",t=h(o,"/.vitepress");yield d(t,{recursive:!0});const i=y(x(import.meta.url)),r=h(i,"../../server-lib/bundle.js"),s=h(t,"bundle.mjs");yield c(r,s);const p=h(t,"config.mjs"),k=yield function(o){return e(this,void 0,void 0,(function*(){const e=[],n=[".vitepress","img","index.md","progress.json","attachments"];let t=yield a(o);t=t.filter((e=>!n.includes(e)));for(const n of t){const t=h(o,n);if((yield u(t)).isDirectory()){const i=yield j(h(t,"index.md")),r={text:n,collapsed:!0,items:yield M(o,n)};i&&(r.link=`/${n}/`),e.push(r)}else e.push({text:n.slice(0,n.lastIndexOf(".")),link:`/${n}`})}return e}))}(o);$((yield l(f(o,"index.md"),"utf8")).split("\n"),k),b(k);const g=`\n import {fixHtmlTags} from './bundle.mjs'\n export default {\n title: "${n}",\n themeConfig: {\n search: {\n provider: 'local'\n },\n sidebar: ${JSON.stringify(k)}\n },\n vite: {\n optimizeDeps: {\n include: []\n }\n },\n markdown: {\n html: true,\n breaks: true,\n config(md) {\n // 包装原始 render 方法,捕获解析异常\n const originalRender = md.render.bind(md);\n md.render = (src, env) => {\n try {\n let newMd = fixHtmlTags(originalRender(src, env))\n newMd = newMd.replace('<html><head></head><body>', '')\n newMd = newMd.replace('</body></html>', '')\n return newMd\n } catch (error) {\n console.error("Markdown/HTML parsing error:", error);\n return src\n }\n }\n }\n }\n }\n `;yield m(p,g)}))}(i));const s=()=>e(this,void 0,void 0,(function*(){const t=yield k(o,{host:n.host,port:n.port},(()=>e(this,void 0,void 0,(function*(){return g||(g=(()=>e(this,void 0,void 0,(function*(){yield t.close(),yield s()})))().finally((()=>{g=void 0}))),g}))));yield t.listen(),t.printUrls()}));yield s().catch((e=>{console.error(e),process.exit(1)}))}))}function j(o){return e(this,void 0,void 0,(function*(){try{return yield p(o),!0}catch(e){if("ENOENT"===e.code)return!1;throw e}}))}function $(e,o){o.map((o=>{"items"in o&&$(e,o.items);const n=e.findIndex((e=>e.includes(`[${o.text}]`)||e.includes(`# ${o.text}`)));o.index=n}))}function b(e){e.length<=1||(e.forEach((e=>{"items"in e&&b(e.items)})),e.sort(((e,o)=>e.index&&o.index?e.index>o.index?1:-1:0)))}function M(o,...n){return e(this,void 0,void 0,(function*(){const e=yield a(h(o,...n)),t=[];for(const i of e){const e=h(o,...n,i);if((yield u(e)).isDirectory()){const r=yield M(h(o),...n,i);if(r.length>0){const o={text:i,items:r,collapsed:false};(yield j(h(e,"index.md")))&&(o.link="/"+[...n,i].map(decodeURIComponent).join("/")+"/"),t.push(o)}}else{if("index.md"===i||/^-.*\.(md|MD)$/.test(i)||!i.endsWith(".md"))continue;const e=i.replace(/\.md$/,""),o={text:e,link:"/"+[...n.map(decodeURIComponent),`${encodeURI(e)}.html`].join("/")};t.push(o)}}return t}))}const R=r("yuque-dl"),{version:I,engines:U}=JSON.parse(i(new URL("../../package.json",import.meta.url)).toString());!function(){const e=U.node;s.satisfies(process.version,e)||(o.error(`✕ nodejs 版本需 ${e}, 当前版本为 ${process.version}`),process.exit(1))}(),R.command("<url>","语雀知识库url").option("-d, --dist-dir <dir>","下载的目录 eg: -d download",{default:"download"}).option("-i, --ignore-img","忽略图片不下载",{default:!1}).option("-k, --key <key>",'语雀的cookie key, 默认是 "_yuque_session", 在某些企业版本中 key 不一样').option("-t, --token <token>","语雀的cookie key 对应的值").option("--toc","是否输出文档toc目录",{default:!1}).option("--incremental","开启增量下载(初次下载加不加该参数没区别)",{default:!1}).option("--convertMarkdownVideoLinks","转化markdown视频链接为video标签",{default:!1}).option("--hideFooter","是否禁用页脚显示(更新时间、原文地址...)",{default:!1}).action(((t,i)=>e(void 0,void 0,void 0,(function*(){try{yield n(t,i),process.exit(0)}catch(e){o.error(e.message||"unknown exception"),process.exit(1)}})))),R.command("server <serverPath>","启动web服务").option("-p, --port <port>"," --port 1234",{default:5173}).option("--host [host]"," --host 0.0.0.0 或 --host",{default:"localhost"}).option("--force","强制重新生成.vitepress",{default:!1}).action(((n,t)=>e(void 0,void 0,void 0,(function*(){try{yield w(n,t)}catch(e){o.error(e.message||"unknown exception"),process.exit(1)}})))),R.help(),R.version(I);try{R.parse()}catch(e){o.error(e.message||"unknown exception"),process.exit(1)}