mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-22 07:26:48 +00:00
Merge branch 'main' into pr/Olimiya/1723
This commit is contained in:
@@ -16,7 +16,6 @@ export async function getNotion(pageId) {
|
||||
}
|
||||
|
||||
const postInfo = blockMap?.block?.[idToUuid(pageId)].value
|
||||
|
||||
return {
|
||||
id: pageId,
|
||||
type: postInfo,
|
||||
@@ -26,7 +25,7 @@ export async function getNotion(pageId) {
|
||||
status: 'Published',
|
||||
createdTime: formatDate(new Date(postInfo.created_time).toString(), BLOG.LANG),
|
||||
lastEditedDay: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG),
|
||||
fullWidth: false,
|
||||
fullWidth: postInfo?.fullWidth,
|
||||
page_cover: getPageCover(postInfo),
|
||||
date: { start_date: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG) },
|
||||
blockMap
|
||||
|
||||
@@ -100,11 +100,11 @@ export default async function getPageProperties(id, value, schema, authToken, ta
|
||||
delete properties.content
|
||||
|
||||
// 处理URL
|
||||
if (properties.type === BLOG.NOTION_PROPERTY_NAME.type_post) {
|
||||
if (properties.type === 'Post') {
|
||||
properties.slug = (BLOG.POST_URL_PREFIX) ? generateCustomizeUrl(properties) : (properties.slug ?? properties.id)
|
||||
} else if (properties.type === BLOG.NOTION_PROPERTY_NAME.type_page) {
|
||||
} else if (properties.type === 'Page') {
|
||||
properties.slug = properties.slug ?? properties.id
|
||||
} else if (properties.type === BLOG.NOTION_PROPERTY_NAME.type_menu || properties.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu) {
|
||||
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
|
||||
// 菜单路径为空、作为可展开菜单使用
|
||||
properties.to = properties.slug ?? '#'
|
||||
properties.name = properties.title ?? ''
|
||||
|
||||
@@ -81,8 +81,10 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取到的blockMap删除不需要的字段
|
||||
* 并且对于页面内容进行特殊处理,比如文件url格式化
|
||||
* 获取到的页面BLOCK特殊处理
|
||||
* 1.删除冗余字段
|
||||
* 2.比如文件、视频、音频、url格式化
|
||||
* 3.代码块等元素兼容
|
||||
* @param {*} id 页面ID
|
||||
* @param {*} pageBlock 页面元素
|
||||
* @param {*} slice 截取数量
|
||||
@@ -121,7 +123,10 @@ function filterPostBlocks(id, pageBlock, slice) {
|
||||
}
|
||||
|
||||
// 如果是文件,或嵌入式PDF,需要重新加密签名
|
||||
if ((b?.value?.type === 'file' || b?.value?.type === 'pdf' || b?.value?.type === 'video') && b?.value?.properties?.source?.[0][0]) {
|
||||
if ((b?.value?.type === 'file' || b?.value?.type === 'pdf' || b?.value?.type === 'video' || b?.value?.type === 'audio') &&
|
||||
b?.value?.properties?.source?.[0][0] &&
|
||||
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
|
||||
) {
|
||||
const oldUrl = b?.value?.properties?.source?.[0][0]
|
||||
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`
|
||||
b.value.properties.source[0][0] = newUrl
|
||||
|
||||
@@ -1,45 +1,5 @@
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
/**
|
||||
* 压缩图片
|
||||
* 1. Notion图床可以通过指定url-query参数来压缩裁剪图片 例如 ?xx=xx&width=400
|
||||
* 2. UnPlash 图片可以通过api q=50 控制压缩质量 width=400 控制图片尺寸
|
||||
* @param {*} image
|
||||
*/
|
||||
const compressImage = (image, width = 800, quality = 50, fmt = 'webp') => {
|
||||
if (!image) {
|
||||
return null
|
||||
}
|
||||
if (image.indexOf(BLOG.NOTION_HOST) === 0 && image.indexOf('amazonaws.com') > 0) {
|
||||
return `${image}&width=${width}`
|
||||
}
|
||||
// 压缩unsplash图片
|
||||
if (image.indexOf('https://images.unsplash.com/') === 0) {
|
||||
// 将URL解析为一个对象
|
||||
const urlObj = new URL(image)
|
||||
// 获取URL参数
|
||||
const params = new URLSearchParams(urlObj.search)
|
||||
// 将q参数的值替换
|
||||
params.set('q', quality)
|
||||
// 尺寸
|
||||
params.set('width', width)
|
||||
// 格式
|
||||
params.set('fmt', fmt)
|
||||
params.set('fm', fmt)
|
||||
// 生成新的URL
|
||||
urlObj.search = params.toString()
|
||||
return urlObj.toString()
|
||||
}
|
||||
|
||||
// 此处还可以添加您的自定义图传的封面图压缩参数。
|
||||
// .e.g
|
||||
if (image.indexOf('https://your_picture_bed') === 0) {
|
||||
return 'do_somethin_here'
|
||||
}
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片映射
|
||||
* 1. 如果是 /xx.xx 相对路径格式,则转化为 完整notion域名图片
|
||||
@@ -61,9 +21,9 @@ const mapImgUrl = (img, block, type = 'block', from) => {
|
||||
}
|
||||
|
||||
// Notion 图床转换为永久地址
|
||||
const isNotionImg = ret.indexOf('secure.notion-static.com') > 0 || ret.indexOf('prod-files-secure') > 0
|
||||
const isNotionSignImg = ret.indexOf('https://www.notion.so/image') !== 0 && (ret.indexOf('secure.notion-static.com') > 0 || ret.indexOf('prod-files-secure') > 0)
|
||||
const isImgBlock = BLOG.IMG_URL_TYPE === 'Notion' || type !== 'block'
|
||||
if (isNotionImg && isImgBlock) {
|
||||
if (isNotionSignImg && isImgBlock) {
|
||||
ret = BLOG.NOTION_HOST + '/image/' + encodeURIComponent(ret) + '?table=' + type + '&id=' + block.id
|
||||
}
|
||||
|
||||
@@ -96,9 +56,11 @@ const mapImgUrl = (img, block, type = 'block', from) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 文章封面
|
||||
if (from === 'pageCoverThumbnail') {
|
||||
ret = compressImage(ret)
|
||||
// 文章封面压缩
|
||||
if (from === 'pageCoverThumbnail' || block.type === 'image') {
|
||||
// 统一压缩图片
|
||||
const width = block?.format?.block_width
|
||||
ret = compressImage(ret, width)
|
||||
}
|
||||
|
||||
return ret
|
||||
@@ -114,4 +76,44 @@ function isEmoji(str) {
|
||||
return emojiRegex.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩图片
|
||||
* 1. Notion图床可以通过指定url-query参数来压缩裁剪图片 例如 ?xx=xx&width=400
|
||||
* 2. UnPlash 图片可以通过api q=50 控制压缩质量 width=400 控制图片尺寸
|
||||
* @param {*} image
|
||||
*/
|
||||
const compressImage = (image, width = 800, quality = 50, fmt = 'webp') => {
|
||||
if (!image) {
|
||||
return null
|
||||
}
|
||||
if (image.indexOf(BLOG.NOTION_HOST) === 0 && image.indexOf('amazonaws.com') > 0) {
|
||||
return `${image}&width=${width}&cache=v2`
|
||||
}
|
||||
// 压缩unsplash图片
|
||||
if (image.indexOf('https://images.unsplash.com/') === 0) {
|
||||
// 将URL解析为一个对象
|
||||
const urlObj = new URL(image)
|
||||
// 获取URL参数
|
||||
const params = new URLSearchParams(urlObj.search)
|
||||
// 将q参数的值替换
|
||||
params.set('q', quality)
|
||||
// 尺寸
|
||||
params.set('width', width)
|
||||
// 格式
|
||||
params.set('fmt', fmt)
|
||||
params.set('fm', fmt)
|
||||
// 生成新的URL
|
||||
urlObj.search = params.toString()
|
||||
return urlObj.toString()
|
||||
}
|
||||
|
||||
// 此处还可以添加您的自定义图传的封面图压缩参数。
|
||||
// .e.g
|
||||
if (image.indexOf('https://your_picture_bed') === 0) {
|
||||
return 'do_somethin_here'
|
||||
}
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
export { mapImgUrl, compressImage }
|
||||
|
||||
Reference in New Issue
Block a user