mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-23 07:26:47 +00:00
文章预览优化
This commit is contained in:
@@ -2,12 +2,12 @@ import BLOG from '@/blog.config'
|
||||
import { NotionAPI } from 'notion-client'
|
||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
|
||||
export async function getPostBlocks (id, from) {
|
||||
export async function getPostBlocks (id, from, slice) {
|
||||
const cacheKey = 'page_block_' + id
|
||||
let pageBlock = await getDataFromCache(cacheKey)
|
||||
if (pageBlock) {
|
||||
console.log('[请求缓存]:', `from:${from}`, `id:${id}`)
|
||||
return pageBlock
|
||||
return filterPostBlocks(id, pageBlock, slice)
|
||||
}
|
||||
const authToken = BLOG.notionAccessToken || null
|
||||
const api = new NotionAPI({ authToken })
|
||||
@@ -20,24 +20,57 @@ export async function getPostBlocks (id, from) {
|
||||
return null
|
||||
}
|
||||
|
||||
// 去掉不用的字段
|
||||
for (const j in pageBlock?.block) {
|
||||
const b = pageBlock?.block[j]
|
||||
if (b) {
|
||||
delete b.role
|
||||
delete b.value?.version
|
||||
delete b.value?.created_time
|
||||
delete b.value?.last_edited_time
|
||||
delete b.value?.created_by_table
|
||||
delete b.value?.created_by_id
|
||||
delete b.value?.last_edited_by_table
|
||||
delete b.value?.last_edited_by_id
|
||||
delete b.value?.space_id
|
||||
}
|
||||
}
|
||||
|
||||
if (pageBlock) {
|
||||
await setDataToCache(cacheKey, pageBlock)
|
||||
return filterPostBlocks(id, pageBlock, slice)
|
||||
}
|
||||
return pageBlock
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} id 页面ID
|
||||
* @param {*} pageBlock 页面元素
|
||||
* @param {*} slice 截取数量
|
||||
* @returns
|
||||
*/
|
||||
function filterPostBlocks (id, pageBlock, slice) {
|
||||
const clonePageBlock = deepClone(pageBlock)
|
||||
let count = 0
|
||||
|
||||
for (const i in clonePageBlock?.block) {
|
||||
const b = clonePageBlock?.block[i]
|
||||
if (slice && slice > 0 && count > slice) {
|
||||
delete clonePageBlock?.block[i]
|
||||
continue
|
||||
}
|
||||
count++
|
||||
delete b?.role
|
||||
delete b?.value?.version
|
||||
delete b?.value?.created_time
|
||||
delete b?.value?.last_edited_time
|
||||
delete b?.value?.created_by_table
|
||||
delete b?.value?.created_by_id
|
||||
delete b?.value?.last_edited_by_table
|
||||
delete b?.value?.last_edited_by_id
|
||||
delete b?.value?.space_id
|
||||
}
|
||||
|
||||
// 去掉不用的字段
|
||||
if (id === BLOG.notionPageId) {
|
||||
return clonePageBlock
|
||||
}
|
||||
return clonePageBlock
|
||||
}
|
||||
|
||||
function deepClone (obj) {
|
||||
const newObj = Array.isArray(obj) ? [] : {}
|
||||
if (obj && typeof obj === 'object') {
|
||||
for (const key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
newObj[key] = (obj && typeof obj[key] === 'object') ? deepClone(obj[key]) : obj[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
return newObj
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user