import{t as e}from"./config-BYRrjYC5.mjs";import{t}from"./logger-B0fjfmRH.mjs";import{t as n}from"./cache-DUthuMSu.mjs";import{t as r}from"./parse-date-BdYTmLpo.mjs";import{t as i}from"./got-BBDj7u1e.mjs";import{t as a}from"./invalid-parameter-DLv2wPKB.mjs";import{t as o}from"./config-not-found-B9AOp828.mjs";import{load as s}from"cheerio";import c from"markdown-it";import{APIErrorCode as l,Client as u,isNotionClientError as d}from"@notionhq/client";import{NotionToMarkdown as f}from"notion-to-md";const p=c({html:!0,linkify:!0}),m={path:`/database/:databaseId`,categories:[`other`],example:`/notion/database/a7cc133b68454f138011f1530a13531e`,parameters:{databaseId:`Database ID`},features:{requireConfig:[{name:`NOTION_TOKEN`,description:``}],requirePuppeteer:!1,antiCrawler:!1,supportBT:!1,supportPodcast:!1,supportScihub:!1},radar:[{source:[`notion.so/:id`],target:`/database/:id`}],name:`Database`,maintainers:[`curly210102`],handler:h,description:'There is an optional query parameter called `properties=` that can be used to customize field mapping. There are three built-in fields: author, pubTime and link, which can be used to add additional information.\n\n  For example, if you have set up three properties in your database - "Publish Time", "Author", and "Original Article Link" - then execute the following JavaScript code to get the result for the properties parameter.\n\n  ```js\n  encodeURIComponent(JSON.stringify({"pubTime": "Publish Time", "author": "Author", "link": "Original Article Link"}))\n  ```\n\n  There is an optional query parameter called `query=` that can be used to customize the search rules for your database, such as custom sorting and filtering rules.\n\n  please refer to the [Notion API documentation](https://developers.notion.com/reference/post-database-query) and execute `encodeURIComponent(JSON.stringify(custom rules))` to provide the query parameter.'};async function h(c){if(!e.notion.key)throw new o(`Notion RSS is disabled due to the lack of NOTION_TOKEN(<a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>)`);let m=c.req.param(`databaseId`),h=e.notion.key,v=new u({auth:h});try{let e=await v.databases.retrieve({database_id:m}),t=e.title[0]?.plain_text,a=e.description[0]?.plain_text,o=e.url,l=e.cover?.external.url??e.icon?.emoji,u=g(c.req.query(`query`)),{results:d}=await v.databases.query({database_id:m,...u}),h=g(c.req.query(`properties`))??{},y={author:h.author??`Author`,link:h.link??`URL`,pubTime:h.pubTime??`Created time`},b=new f({notionClient:v}),x=d.filter(e=>Object.values(e.properties).find(e=>e.id===`title`)?.title[0]?.plain_text),S=await Promise.all(x.map(async e=>{let t=Object.values(e.properties).find(e=>e.id===`title`),a=t.title[0].plain_text,o=e.url,c=e.last_edited_time,l=y.link&&_(e.properties[y.link])||t.title[0].href||``,u=_(e.properties[y.author]),d=_(e.properties[y.pubTime]),f=await n.tryGet(`${o}-${c}`,async()=>{let t=await b.pageToMarkdown(e.id);return b.toMarkdownString(t).parent}),m=u,h=d||c;if(l&&!u){let{articleAuthor:e,articlePubTime:t}=await n.tryGet(`${o}-${l}`,async()=>{try{let e=s((await i({method:`get`,url:l})).body);return{articleAuthor:e(`meta[name="author"]`).attr(`content`),articlePubTime:e(`meta[name="publish_date"], meta[name="date"]`).attr(`content`)}}catch{return{}}});e&&(m=e),t&&(h=t)}return{title:a,author:m,pubDate:r(h),description:p.render(f??``),link:l||o}}));return{title:`Notion - ${t}`,link:o,description:a,image:l,item:S,allowEmpty:!0}}catch(e){if(t.error(e),d(e)){if(e.statusCode===l.ObjectNotFound)throw new a(`The database is not exist`);if(e.statusCode===l.Unauthorized)throw new o(`Please check the config of NOTION_TOKEN`);c.throw(e.statusCode,`Notion API Error`)}else c.throw(e)}}function g(e){try{if(e)return JSON.parse(decodeURIComponent(e))}catch{t.error(`Query Parse Error`)}}function _(e){return e?e.type===`rich_text`?e.rich_text?.map(e=>e.plain_text).join(``)??``:e.type===`select`?e.select.name:e.type===`url`?e.url:``:``}export{m as route};