mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-21 07:26:49 +00:00
Merge branch 'main' into feat/algolia/delete-post
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import BLOG from '@/blog.config'
|
||||
import { useGlobal } from './global'
|
||||
import { deepClone, isUrl } from './utils'
|
||||
import { deepClone, isUrlLikePath } from './utils'
|
||||
|
||||
/**
|
||||
* 读取配置顺序
|
||||
@@ -146,7 +146,7 @@ export const convertVal = val => {
|
||||
}
|
||||
|
||||
// 检测是否为 URL
|
||||
if (isUrl(val)) {
|
||||
if (isUrlLikePath(val)) {
|
||||
return val
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ export function GlobalContextProvider(props) {
|
||||
) // 默认语言
|
||||
const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || THEME) // 默认博客主题
|
||||
const [THEME_CONFIG, SET_THEME_CONFIG] = useState(null) // 主题配置
|
||||
const [isLiteMode,setLiteMode] = useState(false)
|
||||
|
||||
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
|
||||
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
|
||||
@@ -85,10 +86,17 @@ export function GlobalContextProvider(props) {
|
||||
// 添加路由变化时的语言处理
|
||||
useEffect(() => {
|
||||
initLocale(router.locale, changeLang, updateLocale)
|
||||
}, [router])
|
||||
// 处理极简模式
|
||||
if (router.query.lite && router.query.lite==='true') {
|
||||
setLiteMode(true)
|
||||
}
|
||||
}, [router])
|
||||
|
||||
|
||||
// 首次加载成功
|
||||
useEffect(() => {
|
||||
initDarkMode(updateDarkMode, defaultDarkMode)
|
||||
// 处理多语言自动重定向
|
||||
if (
|
||||
NOTION_CONFIG?.REDIRECT_LANG &&
|
||||
JSON.parse(NOTION_CONFIG?.REDIRECT_LANG)
|
||||
@@ -107,6 +115,7 @@ export function GlobalContextProvider(props) {
|
||||
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${themeStr}`
|
||||
router.push(newUrl)
|
||||
}
|
||||
|
||||
if (!onLoading) {
|
||||
setOnLoading(true)
|
||||
}
|
||||
@@ -134,6 +143,7 @@ export function GlobalContextProvider(props) {
|
||||
return (
|
||||
<GlobalContext.Provider
|
||||
value={{
|
||||
isLiteMode,
|
||||
isLoaded,
|
||||
isSignedIn,
|
||||
user,
|
||||
|
||||
@@ -4,11 +4,7 @@ import formatDate from '../utils/formatDate'
|
||||
// import { createHash } from 'crypto'
|
||||
import md5 from 'js-md5'
|
||||
import { siteConfig } from '../config'
|
||||
import {
|
||||
checkStartWithHttp,
|
||||
convertUrlStartWithOneSlash,
|
||||
getLastSegmentFromUrl
|
||||
} from '../utils'
|
||||
import { convertUrlStartWithOneSlash, getLastSegmentFromUrl, isHttpLink, isMailOrTelLink } from '../utils'
|
||||
import { extractLangPrefix } from '../utils/pageId'
|
||||
import { mapImgUrl } from './mapImage'
|
||||
import notionAPI from '@/lib/notion/getNotionAPI'
|
||||
@@ -85,8 +81,9 @@ export default async function getPageProperties(
|
||||
const fieldNames = BLOG.NOTION_PROPERTY_NAME
|
||||
if (fieldNames) {
|
||||
Object.keys(fieldNames).forEach(key => {
|
||||
if (fieldNames[key] && properties[fieldNames[key]])
|
||||
if (fieldNames[key] && properties[fieldNames[key]]) {
|
||||
properties[key] = properties[fieldNames[key]]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -190,9 +187,12 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
|
||||
}
|
||||
|
||||
// http or https 开头的视为外链
|
||||
if (checkStartWithHttp(properties?.href)) {
|
||||
if (isHttpLink(properties?.href)) {
|
||||
properties.href = properties?.slug
|
||||
properties.target = '_blank'
|
||||
} else if (isMailOrTelLink(properties?.href)) {
|
||||
properties.href = properties?.slug
|
||||
properties.target = '_self'
|
||||
} else {
|
||||
properties.target = '_self'
|
||||
// 伪静态路径右侧拼接.html
|
||||
@@ -238,7 +238,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
|
||||
*/
|
||||
function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
|
||||
// 外链不处理
|
||||
if (checkStartWithHttp(postProperties.slug)) {
|
||||
if (isHttpLink(postProperties.slug)) {
|
||||
return postProperties.slug
|
||||
}
|
||||
let fullPrefix = ''
|
||||
|
||||
@@ -119,10 +119,19 @@ const compressImage = (image, width, quality = 50, fmt = 'webp') => {
|
||||
width = siteConfig('IMAGE_COMPRESS_WIDTH')
|
||||
}
|
||||
|
||||
// 将URL解析为一个对象
|
||||
const urlObj = new URL(image)
|
||||
// 获取URL参数
|
||||
const params = new URLSearchParams(urlObj.search)
|
||||
|
||||
let urlObj
|
||||
let params
|
||||
try {
|
||||
// 将URL解析为一个对象
|
||||
urlObj = new URL(image)
|
||||
// 获取URL参数
|
||||
params = new URLSearchParams(urlObj.search)
|
||||
} catch (err) {
|
||||
// 捕获异常并打印错误的url
|
||||
console.error('compressImage: Invalid URL:', image, err)
|
||||
return image
|
||||
}
|
||||
|
||||
// Notion图床
|
||||
if (
|
||||
|
||||
@@ -80,28 +80,31 @@ export function convertUrlStartWithOneSlash(str) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是一个相对或绝对路径的ur类
|
||||
* @param {*} str
|
||||
* @returns
|
||||
* 判断是否是一个“URL 样式”的路径(内部或外部)
|
||||
* 内部如 /about,外部如 http://xxx.com
|
||||
* @param str
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isUrl(str) {
|
||||
if (!str) {
|
||||
return false
|
||||
}
|
||||
|
||||
return str?.indexOf('/') === 0 || checkStartWithHttp(str)
|
||||
export function isUrlLikePath(str) {
|
||||
return typeof str === 'string' && (str.startsWith('/') || isHttpLink(str))
|
||||
}
|
||||
|
||||
// 检查是否外链
|
||||
export function checkStartWithHttp(str) {
|
||||
// 检查字符串是否包含http
|
||||
if (str?.indexOf('http:') === 0 || str?.indexOf('https:') === 0) {
|
||||
// 如果包含,找到http的位置
|
||||
return true
|
||||
} else {
|
||||
// 不包含
|
||||
return false
|
||||
}
|
||||
/**
|
||||
* 判断是否是 http(s) 开头的链接(外部网页)
|
||||
* @param str
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isHttpLink(str) {
|
||||
return typeof str === 'string' && /^https?:\/\//i.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否是邮件或电话链接
|
||||
* @param href
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isMailOrTelLink(href) {
|
||||
return /^(mailto:|tel:)/i.test(href)
|
||||
}
|
||||
|
||||
// 检查一个字符串是否UUID https://ihateregex.io/expr/uuid/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* 文章相关工具
|
||||
*/
|
||||
import { checkStartWithHttp } from '.'
|
||||
import { isHttpLink } from '.'
|
||||
import { getPostBlocks } from '@/lib/db/getSiteData'
|
||||
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
@@ -59,7 +59,7 @@ export function checkSlugHasNoSlash(row) {
|
||||
}
|
||||
return (
|
||||
(slug.match(/\//g) || []).length === 0 &&
|
||||
!checkStartWithHttp(slug) &&
|
||||
!isHttpLink(slug) &&
|
||||
row.type.indexOf('Menu') < 0
|
||||
)
|
||||
}
|
||||
@@ -76,7 +76,7 @@ export function checkSlugHasOneSlash(row) {
|
||||
}
|
||||
return (
|
||||
(slug.match(/\//g) || []).length === 1 &&
|
||||
!checkStartWithHttp(slug) &&
|
||||
!isHttpLink(slug) &&
|
||||
row.type.indexOf('Menu') < 0
|
||||
)
|
||||
}
|
||||
@@ -94,7 +94,7 @@ export function checkSlugHasMorThanTwoSlash(row) {
|
||||
return (
|
||||
(slug.match(/\//g) || []).length >= 2 &&
|
||||
row.type.indexOf('Menu') < 0 &&
|
||||
!checkStartWithHttp(slug)
|
||||
!isHttpLink(slug)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user