Skip to content

Commit

Permalink
Merge branch 'tangly1024:main' into Run
Browse files Browse the repository at this point in the history
  • Loading branch information
Chacat68 committed Jun 19, 2024
2 parents eede1a1 + 96d5e2e commit b6ad366
Show file tree
Hide file tree
Showing 59 changed files with 1,144 additions and 706 deletions.
2 changes: 1 addition & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables
NEXT_PUBLIC_VERSION=4.5.4
NEXT_PUBLIC_VERSION=4.6.0


# 可在此添加环境变量,去掉最左边的(# )注释即可
Expand Down
2 changes: 1 addition & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ const BLOG = {
process.env.ENABLE_CACHE ||
process.env.npm_lifecycle_event === 'build' ||
process.env.npm_lifecycle_event === 'export', // 在打包过程中默认开启缓存,开发或运行时开启此功能意义不大。
isProd: process.env.VERCEL_ENV === 'production', // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
isProd: process.env.VERCEL_ENV === 'production' || process.env.EXPORT, // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
BUNDLE_ANALYZER: process.env.ANALYZE === 'true' || false, // 是否展示编译依赖内容与大小
VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号
}
Expand Down
5 changes: 2 additions & 3 deletions components/ExternalPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import LA51 from './LA51'
import TianLiGPT from './TianliGPT'
import WebWhiz from './Webwhiz'

import { CUSTOM_EXTERNAL_CSS, CUSTOM_EXTERNAL_JS } from '@/blog.config'
import { convertInnerUrl } from '@/lib/notion/convertInnerUrl'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -44,8 +43,6 @@ const ExternalPlugin = props => {
const CHATBASE_ID = siteConfig('CHATBASE_ID')
const COMMENT_DAO_VOICE_ID = siteConfig('COMMENT_DAO_VOICE_ID')
const AD_WWADS_ID = siteConfig('AD_WWADS_ID')
// const COMMENT_TWIKOO_ENV_ID = siteConfig('COMMENT_TWIKOO_ENV_ID')
// const COMMENT_TWIKOO_CDN_URL = siteConfig('COMMENT_TWIKOO_CDN_URL')
const COMMENT_ARTALK_SERVER = siteConfig('COMMENT_ARTALK_SERVER')
const COMMENT_ARTALK_JS = siteConfig('COMMENT_ARTALK_JS')
const COMMENT_TIDIO_ID = siteConfig('COMMENT_TIDIO_ID')
Expand All @@ -64,6 +61,8 @@ const ExternalPlugin = props => {
const IMG_SHADOW = siteConfig('IMG_SHADOW')
const ANIMATE_CSS_URL = siteConfig('ANIMATE_CSS_URL')
const MOUSE_FOLLOW = siteConfig('MOUSE_FOLLOW')
const CUSTOM_EXTERNAL_CSS = siteConfig('CUSTOM_EXTERNAL_CSS')
const CUSTOM_EXTERNAL_JS = siteConfig('CUSTOM_EXTERNAL_JS')

// 自定义样式css和js引入
if (isBrowser) {
Expand Down
6 changes: 3 additions & 3 deletions components/NotionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const NotionPage = ({ post, className }) => {
// 页面首次打开时执行的勾子
useEffect(() => {
// 检测当前的url并自动滚动到对应目标
autoScrollToTarget()
autoScrollToHash()
}, [])

// 页面文章发生变化时会执行的勾子
Expand Down Expand Up @@ -144,9 +144,9 @@ const processGalleryImg = zoom => {
}

/**
* 根据url参数自动滚动到指定区域
* 根据url参数自动滚动到锚位置
*/
const autoScrollToTarget = () => {
const autoScrollToHash = () => {
setTimeout(() => {
// 跳转到指定标题
const needToJumpToTitle = window.location.hash
Expand Down
70 changes: 44 additions & 26 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import BLOG from '@/blog.config'
import { useGlobal } from './global'
import { deepClone } from './utils'
import { deepClone, isUrl } from './utils'

/**
* 读取配置顺序
Expand Down Expand Up @@ -32,6 +32,8 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
case 'POST_LIST_STYLE':
case 'POST_LIST_PREVIEW':
case 'POST_URL_PREFIX_MAPPING_CATEGORY':
case 'IS_TAG_COLOR_DISTINGUISHED':
case 'TAG_SORT_BY_COUNT':
return convertVal(extendConfig[key] || defaultVal || BLOG[key])
default:
}
Expand Down Expand Up @@ -89,46 +91,62 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
return defaultVal
}

// 从Notion_CONFIG读取的配置通常都是字符串,适当转义
return convertVal(val)
}

/**
* 配置默认都是string类型;
* 识别配置的值是否数字、布尔、[]数组,若是则转成对应类型
* 从环境变量和NotionConfig读取的配置都是string类型;
* 这里识别出配置的字符值若为否 数字、布尔、[]数组,{}对象,若是则转成对应类型
* 使用JSON和eval两个函数
* @param {*} val
* @returns
*/
export const convertVal = val => {
if (typeof val === 'string') {
// 解析布尔
if (val === 'true' || val === 'false') {
return JSON.parse(val)
}
// 如果传入参数本身就是obj、数组、boolean 就无需处理
if (typeof val !== 'string' || !val) {
return val
}

// 解析数字,parseInt将字符串转换为数字
if (/^\d+$/.test(val)) {
return parseInt(val)
}
// 转移 [] , {} 这种json串为json对象
try {
const parsedJson = JSON.parse(val)
// 检查解析后的结果是否是对象或数组
if (typeof parsedJson === 'object' && parsedJson !== null) {
return parsedJson
}
} catch (error) {
// JSON 解析失败,返回原始字符串值
return val
}
// 解析数字,parseInt将字符串转换为数字
if (/^\d+$/.test(val)) {
return parseInt(val)
}

try {
// 检测是否url
if (isUrl(val)) {
return val
}
// 检测是否url
if (val === 'true' || val === 'false') {
return JSON.parse(val)
}

// 配置值前可能有污染的空格
if (val.indexOf('[') < 0 && val.indexOf('{') < 0) {
return val
}

// 转换 [] , {} , true/false 这类字符串为对象
try {
// 尝试解析json
const parsedJson = JSON.parse(val)
if (parsedJson !== null) {
return parsedJson
}
} catch (error) {
// 如果值是一个字符串但不是有效的 JSON 格式,直接返回字符串
// try {
// // 尝试解析对象,对象解析能力不如上一步的json
// const evalObj = eval('(' + val + ')')
// if (evalObj !== null) {
// return evalObj
// }
// } catch (error) {
// // Ojbject 解析失败,返回原始字符串值
// return val
// }
return val
}
return val
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties, {
adjustPageProperties
adjustPageProperties
} from '@/lib/notion/getPageProperties'
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
Expand Down Expand Up @@ -512,6 +512,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
adjustPageProperties(element, NOTION_CONFIG)
})

// 站点基础信息
const siteInfo = getSiteInfo({ collection, block, pageId })

// 文章计数
Expand Down
3 changes: 2 additions & 1 deletion lib/lang/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default {
START_READING: 'Start Reading',
MINUTE: 'min',
WORD_COUNT: 'Words',
READ_TIME: 'Read Time'
READ_TIME: 'Read Time',
NEXT_POST: '下一篇'
},
PAGINATION: {
PREV: 'Prev',
Expand Down
3 changes: 2 additions & 1 deletion lib/lang/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default {
START_READING: '开始阅读',
MINUTE: '分钟',
WORD_COUNT: '字数',
READ_TIME: '阅读时长'
READ_TIME: '阅读时长',
NEXT_POST:'下一篇'
},
PAGINATION: {
PREV: '上页',
Expand Down
34 changes: 20 additions & 14 deletions lib/notion/convertInnerUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
*/
export const convertInnerUrl = allPages => {
if (isBrowser) {
const allAnchorTags = document
?.getElementById('notion-article')
?.getElementsByTagName('a')

if (!allAnchorTags) {
return
}
const currentURL = window.location.origin + window.location.pathname
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
// url替换成slug
for (const anchorTag of allAnchorTags) {
// 检查url
if (anchorTag?.href) {
Expand All @@ -24,20 +31,19 @@ export const convertInnerUrl = allPages => {
}
}
}
}

for (const anchorTag of allAnchorTags) {
if (anchorTag?.target === '_blank') {
const hrefWithoutQueryHash = anchorTag.href
.split('?')[0]
.split('#')[0]
const hrefWithRelativeHash =
currentURL.split('#')[0] || '' + anchorTag.href.split('#')[1] || ''
if (
currentURL === hrefWithoutQueryHash ||
currentURL === hrefWithRelativeHash
) {
anchorTag.target = '_self'
}
// 链接在当前页面打开
for (const anchorTag of allAnchorTags) {
if (anchorTag?.target === '_blank') {
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
const hrefWithRelativeHash =
currentURL.split('#')[0] || '' + anchorTag.href.split('#')[1] || ''
if (
currentURL === hrefWithoutQueryHash ||
currentURL === hrefWithRelativeHash
) {
anchorTag.target = '_self'
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import formatDate from '../utils/formatDate'
import md5 from 'js-md5'
import { siteConfig } from '../config'
import {
checkContainHttp,
checkStartWithHttp,
convertUrlStartWithOneSlash,
getLastSegmentFromUrl,
sliceUrlFromHttp
} from '../utils'
import { extractLangPrefix } from '../utils/pageId'
import { mapImgUrl } from './mapImage'

/**
Expand Down Expand Up @@ -190,14 +192,17 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
if (siteConfig('PSEUDO_STATIC', false, NOTION_CONFIG)) {
if (
!properties?.href?.endsWith('.html') &&
!properties?.href?.startsWith('http')
!properties?.href?.startsWith('http') &&
properties?.href !== '' &&
properties?.href !== '#' &&
properties?.href !== '/'
) {
properties.href += '.html'
}
}

// 检查处理外链
properties.href = checkContainHttp(properties?.href)
properties.href = checkStartWithHttp(properties?.href)
? sliceUrlFromHttp(properties?.href)
: convertUrlStartWithOneSlash(properties?.href)

Expand All @@ -208,6 +213,18 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
properties.target = '_self'
}

// 如果跳转链接是多语言,则在新窗口打开
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
for (let index = 0; index < siteIds.length; index++) {
const siteId = siteIds[index]
const prefix = extractLangPrefix(siteId)
if (getLastSegmentFromUrl(properties.href) === prefix) {
properties.target = '_blank'
}
}
}

// 密码字段md5
properties.password = properties.password
? md5(properties.slug + properties.password)
Expand Down
33 changes: 31 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,23 @@ export function convertUrlStartWithOneSlash(str) {
return str
}

/**
* 是否是一个相对或绝对路径的ur类
* @param {*} str
* @returns
*/
export function isUrl(str) {
if (!str) {
return false
}

return str?.indexOf('/') === 0 || checkStartWithHttp(str)
}

// 检查是否外链
export function checkContainHttp(str) {
export function checkStartWithHttp(str) {
// 检查字符串是否包含http
if (str?.includes('http:') || str?.includes('https:')) {
if (str?.indexOf('http:') === 0 || str?.indexOf('https:') === 0) {
// 如果包含,找到http的位置
return str?.indexOf('http') > -1
} else {
Expand Down Expand Up @@ -354,3 +367,19 @@ export const scanAndConvertToLinks = node => {
}
}
}

/**
* 获取url最后一个斜杆后面的内容
* @param {*} url
* @returns
*/
export function getLastSegmentFromUrl(url) {
if (!url) {
return ''
}
// 去掉 URL 中的查询参数部分
const trimmedUrl = url.split('?')[0]
// 获取最后一个斜杠后面的内容
const segments = trimmedUrl.split('/')
return segments[segments.length - 1]
}
8 changes: 4 additions & 4 deletions lib/utils/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 文章相关工具
*/
import { checkContainHttp } from '.'
import { checkStartWithHttp } from '.'

/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
Expand Down Expand Up @@ -50,7 +50,7 @@ export function checkSlugHasNoSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 0 &&
!checkContainHttp(slug) &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
Expand All @@ -67,7 +67,7 @@ export function checkSlugHasOneSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 1 &&
!checkContainHttp(slug) &&
!checkStartWithHttp(slug) &&
row.type.indexOf('Menu') < 0
)
}
Expand All @@ -85,6 +85,6 @@ export function checkSlugHasMorThanTwoSlash(row) {
return (
(slug.match(/\//g) || []).length >= 2 &&
row.type.indexOf('Menu') < 0 &&
!checkContainHttp(slug)
!checkStartWithHttp(slug)
)
}
Loading

0 comments on commit b6ad366

Please sign in to comment.