mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
Merge branch 'main' into pr/Olimiya/1723
This commit is contained in:
4
lib/cache/cache_manager.js
vendored
4
lib/cache/cache_manager.js
vendored
@@ -9,7 +9,7 @@ import BLOG from '@/blog.config'
|
||||
* @returns
|
||||
*/
|
||||
export async function getDataFromCache(key, force) {
|
||||
if (BLOG.ENABLE_CACHE || force) {
|
||||
if (JSON.parse(BLOG.ENABLE_CACHE) || force) {
|
||||
const dataFromCache = await getApi().getCache(key)
|
||||
if (JSON.stringify(dataFromCache) === '[]') {
|
||||
return null
|
||||
@@ -28,7 +28,7 @@ export async function setDataToCache(key, data) {
|
||||
}
|
||||
|
||||
export async function delCacheData(key) {
|
||||
if (!BLOG.ENABLE_CACHE) {
|
||||
if (!JSON.parse(BLOG.ENABLE_CACHE)) {
|
||||
return
|
||||
}
|
||||
await getApi().delCache(key)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { generateLocaleDict, initLocale } from './lang'
|
||||
import { generateLocaleDict, initLocale, saveLangToCookies } from './lang'
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { THEMES, initDarkMode, saveDarkModeToCookies } from '@/themes/theme'
|
||||
@@ -12,7 +12,7 @@ const GlobalContext = createContext()
|
||||
* @constructor
|
||||
*/
|
||||
export function GlobalContextProvider(props) {
|
||||
const { children, siteInfo, categoryOptions, tagOptions, NOTION_CONFIG } = props
|
||||
const { post, children, siteInfo, categoryOptions, tagOptions, NOTION_CONFIG } = props
|
||||
const router = useRouter()
|
||||
const [lang, updateLang] = useState(NOTION_CONFIG?.LANG || LANG) // 默认语言
|
||||
const [locale, updateLocale] = useState(generateLocaleDict(NOTION_CONFIG?.LANG || LANG)) // 默认语言
|
||||
@@ -20,6 +20,9 @@ export function GlobalContextProvider(props) {
|
||||
const [isDarkMode, updateDarkMode] = useState(NOTION_CONFIG?.APPEARANCE || APPEARANCE === 'dark') // 默认深色模式
|
||||
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
|
||||
|
||||
// 是否全屏
|
||||
const fullWidth = post?.fullWidth ?? false
|
||||
|
||||
// 切换主题
|
||||
function switchTheme() {
|
||||
const currentIndex = THEMES.indexOf(theme)
|
||||
@@ -46,6 +49,7 @@ export function GlobalContextProvider(props) {
|
||||
*/
|
||||
function changeLang(lang) {
|
||||
if (lang) {
|
||||
saveLangToCookies(lang)
|
||||
updateLang(lang)
|
||||
updateLocale(generateLocaleDict(lang))
|
||||
}
|
||||
@@ -82,6 +86,7 @@ export function GlobalContextProvider(props) {
|
||||
|
||||
return (
|
||||
<GlobalContext.Provider value={{
|
||||
fullWidth,
|
||||
NOTION_CONFIG,
|
||||
toggleDarkMode,
|
||||
onLoading,
|
||||
|
||||
@@ -89,9 +89,9 @@ export const loadLangFromCookies = () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存语言
|
||||
* @param newTheme
|
||||
*/
|
||||
* 保存语言
|
||||
* @param newTheme
|
||||
*/
|
||||
export const saveLangToCookies = (lang) => {
|
||||
cookie.save('lang', lang, { path: '/' })
|
||||
}
|
||||
|
||||
@@ -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