"use strict";var ie=Object.create;var z=Object.defineProperty;var oe=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var le=Object.getPrototypeOf,ce=Object.prototype.hasOwnProperty;var M=(e,t)=>{for(var r in t)z(e,r,{get:t[r],enumerable:!0})},O=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of ne(t))!ce.call(e,i)&&i!==r&&z(e,i,{get:()=>t[i],enumerable:!(a=oe(t,i))||a.enumerable});return e};var E=(e,t,r)=>(r=e!=null?ie(le(e)):{},O(t||!e||!e.__esModule?z(r,"default",{value:e,enumerable:!0}):r,e)),se=e=>O(z({},"__esModule",{value:!0}),e);var _t={};M(_t,{crawler:()=>re,dbCrawler:()=>D,pageToString:()=>ht,serializer:()=>S,waitAllResults:()=>bt});module.exports=se(_t);var X=require("md-utils-ts");var b=require("@notionhq/client");var f=(e,t)=>t in e,I=e=>new Promise(t=>setTimeout(t,e));var de=e=>(0,b.isNotionClientError)(e)&&e.code===b.APIErrorCode.RateLimited,F=async(e,t=5,r=1e3)=>{try{return await e()}catch(a){if(de(a)){if(t===0)throw a;return console.log(`Rate limited. Retries left: ${t}. Waiting ${r}ms before retrying...`),await I(r),F(e,t-1,r*2)}else throw a}},_=e=>async t=>F(()=>(0,b.collectPaginatedAPI)(e.blocks.children.list,{block_id:t})).catch(()=>[]),L=e=>t=>F(()=>e.pages.retrieve({page_id:t})),j=e=>t=>F(()=>e.databases.query({database_id:t}).then(({results:r})=>r)).catch(()=>[]);var o=E(require("md-utils-ts"),1);var C={};M(C,{annotate:()=>q,fromDate:()=>P,fromLink:()=>h,fromRichText:()=>c,fromUser:()=>k});var m=E(require("md-utils-ts"),1);var q=(e,t)=>(t.code&&(e=m.inlineCode(e)),t.bold&&(e=m.bold(e)),t.italic&&(e=m.italic(e)),t.strikethrough&&(e=m.del(e)),t.underline&&(e=m.underline(e)),e),c=(e,t=!1)=>e.map(({plain_text:r,annotations:a,href:i})=>{if(r.match(/^\s*$/))return r;let l=r.match(/^(\s*)/),n=r.match(/(\s*)$/),p=l?l[0]:"",x=n?n[0]:"",s=r.trim();if(s==="")return p+x;let d=q(s,a),u=i?m.anchor(d,t||i):d;return p+u+x}).join(""),h=e=>{let t=c(e.caption),r=e.type==="external"?e.external.url:e.file.url,a=r.match(/[^\/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))/);return{title:t.trim()?t:a?a[0]:"link",href:r}},k=e=>{var r;if(!f(e,"type"))return"";let t=(r=e.name)!=null?r:"";return e.type==="person"?`${t}`:`${t}[bot]`},P=e=>e?e.end?`(start)${e.start}, (end): ${e.end}`:e.start:"";var pe=({urlMask:e})=>t=>{let{title:r,href:a}=h(t.audio);return o.anchor(r,e||a)},me=({urlMask:e})=>t=>o.anchor(c(t.bookmark.caption),e||t.bookmark.url),ue=()=>()=>!1,ye=({urlMask:e})=>t=>o.bullet(c(t.bulleted_list_item.rich_text,e)),ge=({urlMask:e})=>t=>o.quote(c(t.callout.rich_text,e)),fe=()=>e=>`[${e.child_page.title}]`,xe=()=>e=>`[${e.child_database.title}]`,he=({urlMask:e})=>t=>o.codeBlock(t.code.language)(c(t.code.rich_text,e)),be=()=>()=>!1,_e=()=>()=>!1,ke=()=>()=>o.hr(),Te=({urlMask:e})=>t=>{let r=c(t.embed.caption,e);return o.anchor(r,e||t.embed.url)},Se=()=>e=>o.equationBlock(e.equation.expression),ze=({urlMask:e})=>t=>{let{title:r,href:a}=h(t.file);return o.anchor(r,e||a)},Fe=({urlMask:e})=>t=>o.h1(c(t.heading_1.rich_text,e)),Pe=({urlMask:e})=>t=>o.h2(c(t.heading_2.rich_text,e)),we=({urlMask:e})=>t=>o.h3(c(t.heading_3.rich_text,e)),Ce=({urlMask:e})=>t=>{let{title:r,href:a}=h(t.image);return o.image(r,e||a)},$e=({urlMask:e})=>t=>o.anchor(t.type,e||t.link_preview.url),Ne=({urlMask:e})=>t=>{let r=t.link_to_page.type==="page_id"?t.link_to_page.page_id:"";return o.anchor(t.type,e||r)},Be=({urlMask:e})=>t=>o.bullet(c(t.numbered_list_item.rich_text,e),1),Re=({urlMask:e})=>t=>c(t.paragraph.rich_text,e),De=({urlMask:e})=>t=>{let{title:r,href:a}=h(t.pdf);return o.anchor(r,e||a)},Me=({urlMask:e})=>t=>o.quote(c(t.quote.rich_text,e)),Oe=()=>()=>!1,Ee=()=>()=>!1,Ie=()=>()=>!1,Le=({urlMask:e})=>t=>`| ${t.table_row.cells.flatMap(r=>r.map(a=>c([a],e))).join(" | ")} |`,je=({urlMask:e})=>t=>c(t.template.rich_text,e),qe=({urlMask:e})=>t=>o.todo(c(t.to_do.rich_text,e),t.to_do.checked),ve=({urlMask:e})=>t=>c(t.toggle.rich_text,e),Ue=()=>()=>!1,Ae=({urlMask:e})=>t=>{let{title:r,href:a}=h(t.video);return o.anchor(r,e||a)},$=e=>({audio:pe(e),bookmark:me(e),breadcrumb:ue(e),bulleted_list_item:ye(e),callout:ge(e),child_database:xe(e),child_page:fe(e),code:he(e),column:be(e),column_list:_e(e),divider:ke(e),embed:Te(e),equation:Se(e),file:ze(e),heading_1:Fe(e),heading_2:Pe(e),heading_3:we(e),image:Ce(e),link_preview:$e(e),link_to_page:Ne(e),numbered_list_item:Be(e),paragraph:Re(e),pdf:De(e),quote:Me(e),synced_block:Oe(e),table:Ee(e),table_of_contents:Ie(e),table_row:Le(e),template:je(e),to_do:qe(e),toggle:ve(e),unsupported:Ue(e),video:Ae(e)}),v=$({urlMask:!1});var U=$;var A={defaults:v,strategy:U};var H=require("md-utils-ts");var T=", ",g="",He=({urlMask:e})=>(t,r)=>`[${t}] ${r.checkbox}`,Ge=({urlMask:e})=>(t,r)=>`[${t}] ${k(r.created_by)}`,Ke=({urlMask:e})=>(t,r)=>`[${t}] ${r.created_time}`,G=({urlMask:e})=>(t,r)=>`[${t}] ${P(r.date)}`,Ve=({urlMask:e})=>(t,r)=>{var a;return`[${t}] ${(a=r.email)!=null?a:g}`},Qe=({urlMask:e})=>(t,r)=>`[${t}] `+r.files.map(a=>{let i=f(a,"external")?a.external.url:a.file.url;return(0,H.anchor)(a.name,i)}).join(T),We=({urlMask:e})=>(t,r)=>{var a,i;switch(r.formula.type){case"string":return`[${t}] ${(a=r.formula.string)!=null?a:g}`;case"boolean":return`[${t}] ${(i=r.formula.boolean)!=null?i:g}`;case"date":return`[${t}] ${P(r.formula.date)}`;case"number":return`[${t}] ${r.formula.number}`}},Ye=({urlMask:e})=>(t,r)=>`[${t}] ${k(r.last_edited_by)}`,Je=({urlMask:e})=>(t,r)=>`[${t}] ${r.last_edited_time}`,Xe=({urlMask:e})=>(t,r)=>`[${t}] `+r.multi_select.map(a=>a.name).join(T),K=({urlMask:e})=>(t,r)=>{var a;return`[${t}] ${(a=r.number)!=null?a:g}`},Ze=({urlMask:e})=>(t,r)=>`[${t}] `+r.people.map(a=>k(a)).join(T),et=({urlMask:e})=>(t,r)=>{var a;return`[${t}] ${(a=r.phone_number)!=null?a:g}`},tt=({urlMask:e})=>(t,r)=>`[${t}] `+r.relation.map(a=>`${a.id}`).join(T),rt=({urlMask:e})=>(t,r)=>`[${t}] ${c(r.rich_text)}`,at=({urlMask:e})=>(t,r)=>{var a,i;return`[${t}] ${(i=(a=r.select)==null?void 0:a.name)!=null?i:g}`},it=({urlMask:e})=>(t,r)=>{var a,i;return`[${t}] ${(i=(a=r.status)==null?void 0:a.name)!=null?i:g}`},ot=({urlMask:e})=>(t,r)=>`[${t}] ${c(r.title)}`,nt=({urlMask:e})=>(t,r)=>{var n,p;let a=(n=r.unique_id.prefix)!=null?n:"",i=(p=r.unique_id.number)!=null?p:"",l=a+i;return`[${t}] ${l||g}`},lt=({urlMask:e})=>(t,r)=>{var a;return`[${t}] ${(a=r.url)!=null?a:g}`},ct=({urlMask:e})=>()=>!1,V=e=>({checkbox:He(e),created_by:Ge(e),created_time:Ke(e),date:G(e),email:Ve(e),files:Qe(e),formula:We(e),last_edited_by:Ye(e),last_edited_time:Je(e),multi_select:Xe(e),number:K(e),people:Ze(e),phone_number:et(e),relation:tt(e),rich_text:rt(e),select:at(e),status:it(e),title:ot(e),unique_id:nt(e),url:lt(e),verification:ct(e)}),st=e=>(t,r)=>{switch(r.rollup.type){case"number":return K(e)(t,r.rollup);case"date":return G(e)(t,r.rollup);case"array":let a=V(e);return Promise.all(r.rollup.array.map(i=>a[i.type](t,i))).then(i=>`[${t}] `+i.map(l=>l).map(l=>l.replace(`[${t}] `,"")).join(T))}},N=e=>({...V(e),rollup:st(e)}),Q=N({urlMask:!1});var W=N;var Y={defaults:Q,strategy:W},J=e=>t=>Promise.all(Object.entries(t).map(([r,a])=>e[a.type](r,a))).then(r=>r.filter(a=>a!==!1));var S={block:A,property:Y,utils:C};var w=(e,t)=>t.includes(e.type),R=(e,t)=>t&&t.includes(e),Z=e=>async(t,r,a,i)=>{let l={id:t.id,title:r,createdTime:t.created_time,lastEditedTime:t.last_edited_time,parentId:a==null?void 0:a.metadata.id},n=e?await e({page:t,title:r,properties:i,parent:a}):{};return{metadata:{...l,...n},properties:i||[],lines:[]}},dt=["table","table_row","column_list","column"],pt=(0,X.indent)(),mt=(e,{urlMask:t=!1,serializers:r})=>({...S.block.strategy({urlMask:t}),...r==null?void 0:r.block})[e],ut=e=>w(e,["child_page","child_database"]),ee=e=>({id:e.metadata.id,success:!0,page:e}),yt=(e,t)=>({id:e.metadata.id,success:!1,failure:{parentId:e.metadata.parentId,reason:t instanceof Error?`${t.name}: ${t.message} ${t.stack}`:`${t}`}}),B=e=>async(t,r=0)=>{var l;let a=[],i=[];for(let n of t){if(!f(n,"type"))continue;let{type:p}=n,s=await mt(p,e)(n);if(s!==!1){let d=pt(s,r);a.push(d)}if(ut(n)){i.push(n);continue}if(w(n,["synced_block"])){let d=((l=n.synced_block.synced_from)==null?void 0:l.block_id)||n.id,u=await _(e.client)(d),y=await B(e)(u,r);a=[...a,...y.lines],i=[...i,...y.pages];continue}if(n.has_children){let d=await _(e.client)(n.id),u=dt.includes(p)?r:r+1,y=await B(e)(d,u);a=[...a,...y.lines],i=[...i,...y.pages]}}return{lines:a,pages:i}},te=e=>async function*(t,r,a=0){try{let{client:i,metadataBuilder:l}=e,n=Z(l),{lines:p,pages:x}=await B(e)(r,a);yield ee({...t,lines:p});for(let s of x)if(!R(s.id,e.skipPageIds)){if(w(s,["child_page"])){let{title:d}=s.child_page,u=await n(s,d,t),y=await _(i)(s.id);yield*te(e)(u,y,0);continue}if(w(s,["child_database"])){let{title:d}=s.child_database,u=await n(s,d,t),y={...e,parent:u};yield*D(y)(s.id)}}}catch(i){yield yt(t,i)}},gt=(e,t)=>{let{urlMask:r=!1,serializers:a}=t,i={...S.property.strategy({urlMask:r}),...a==null?void 0:a.property};return J(i)(e)},ft=e=>{if(!f(e,"properties"))return"";let t="";for(let r of Object.values(e.properties)){if(r.type!=="title")continue;t=S.property.defaults.title("",r).replace("[] ","")}return t},re=e=>async function*(t){let{client:r,parent:a,metadataBuilder:i,skipPageIds:l}=e;if(!R(t,l))try{let n=await L(r)(t);if(!f(n,"parent")){let y="Unintended Notion Page object.";return yield{id:t,success:!1,failure:{parentId:a==null?void 0:a.metadata.id,reason:y}}}let p=await gt(n.properties,e),x=await _(r)(n.id),s=ft(n),u=await Z(i)(n,s,a,p);yield*te(e)(u,x)}catch{yield*D(e)(t)}},D=e=>async function*(t){let{skipPageIds:r}=e;if(R(t,r))return;let a=re(e),i=await j(e.client)(t),{parent:l}=e;l&&(yield ee(l));for(let n of i)yield*a(n.id)};var ae=require("md-utils-ts"),xt=e=>e.match(/^#+\s/)?"#"+e:e,ht=({metadata:e,properties:t,lines:r})=>{let a=(0,ae.h1)(e.title),i=["---",t.join(` `),"---"].join(` `),l=r.map(xt);return[a,i,...l].join(` `)},bt=async e=>{let t=[];for await(let r of e)t.push(r);return t};0&&(module.exports={crawler,dbCrawler,pageToString,serializer,waitAllResults}); //# sourceMappingURL=index.cjs.map