Skip to content

Commit

Permalink
Merge pull request #65 from lifeafter619/origin
Browse files Browse the repository at this point in the history
Origin
  • Loading branch information
lifeafter619 authored Mar 27, 2024
2 parents 14de4ea + 6da1441 commit f98dc4b
Show file tree
Hide file tree
Showing 126 changed files with 6,019 additions and 483 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.4.0
NEXT_PUBLIC_VERSION=4.4.2


# 可在此添加环境变量,去掉最左边的(# )注释即可
Expand Down
18 changes: 9 additions & 9 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"singleQuote": true,
"semi": false,
"trailingComma": "none",
"arrowParens": "avoid",
"printWidth": 120,
"bracketSpacing": true,
"jsxSingleQuote": true,
"jsxBracketSameLine": true
}
"singleQuote": true,
"semi": false,
"trailingComma": "none",
"arrowParens": "avoid",
"printWidth": 80,
"bracketSpacing": true,
"jsxSingleQuote": true,
"jsxBracketSameLine": true
}
174 changes: 86 additions & 88 deletions blog.config.js

Large diffs are not rendered by default.

45 changes: 27 additions & 18 deletions components/Draggable.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useRef, useEffect, useState } from 'react'
import { useEffect, useRef, useState } from 'react'

/**
* 可拖拽组件
*/

export const Draggable = (props) => {
const { children } = props
export const Draggable = props => {
const { children, stick } = props
const draggableRef = useRef(null)
const rafRef = useRef(null)
const [moving, setMoving] = useState(false)
Expand All @@ -14,8 +14,10 @@ export const Draggable = (props) => {
const draggableElements = document.getElementsByClassName('draggable')

// 标准化鼠标事件对象
function e(event) { // 定义事件对象标准化函数
if (!event) { // 兼容IE浏览器
function e(event) {
// 定义事件对象标准化函数
if (!event) {
// 兼容IE浏览器
event = window.event
event.target = event.srcElement
event.layerX = event.offsetX
Expand All @@ -40,9 +42,10 @@ export const Draggable = (props) => {
document.onmousedown = start
document.ontouchstart = start

function start (event) { // 按下鼠标时,初始化处理
function start(event) {
// 按下鼠标时,初始化处理
if (!draggableElements) return
event = e(event)// 获取标准事件对象
event = e(event) // 获取标准事件对象

for (const drag of draggableElements) {
// 判断鼠标点击的区域是否是拖拽框内
Expand All @@ -60,27 +63,28 @@ export const Draggable = (props) => {
offsetX = event.mx - currentObj.offsetLeft
offsetY = event.my - currentObj.offsetTop

document.onmousemove = move// 注册鼠标移动事件处理函数
document.onmousemove = move // 注册鼠标移动事件处理函数
document.ontouchmove = move
document.onmouseup = stop// 注册松开鼠标事件处理函数
document.onmouseup = stop // 注册松开鼠标事件处理函数
document.ontouchend = stop
}
}

function move(event) { // 鼠标移动处理函数
function move(event) {
// 鼠标移动处理函数
event = e(event)
rafRef.current = requestAnimationFrame(() => updatePosition(event))
}

const stop = (event) => {
const stop = event => {
event = e(event)
document.documentElement.style.overflow = 'auto' // 恢复默认的滚动行为
cancelAnimationFrame(rafRef.current)
setMoving(false)
currentObj = document.ontouchmove = document.ontouchend = document.onmousemove = document.onmouseup = null
}

const updatePosition = (event) => {
const updatePosition = event => {
if (currentObj) {
const left = event.mx - offsetX
const top = event.my - offsetY
Expand Down Expand Up @@ -120,15 +124,18 @@ export const Draggable = (props) => {
if (offsetTop < 0) {
drag.firstElementChild.style.top = 0
}
if (offsetTop > (clientHeight - offsetHeight)) {
if (offsetTop > clientHeight - offsetHeight) {
drag.firstElementChild.style.top = clientHeight - offsetHeight + 'px'
}
if (offsetLeft < 0) {
drag.firstElementChild.style.left = 0
}
if (offsetLeft > (clientWidth - offsetWidth)) {
if (offsetLeft > clientWidth - offsetWidth) {
drag.firstElementChild.style.left = clientWidth - offsetWidth + 'px'
}
if (stick === 'left') {
drag.firstElementChild.style.left = 0 + 'px'
}
}
}

Expand All @@ -142,9 +149,11 @@ export const Draggable = (props) => {
}
}, [])

return <div className={`draggable ${moving ? 'cursor-grabbing' : 'cursor-grab'} select-none`} ref={draggableRef}>
{children}
</div>
return (
<div className={`draggable ${moving ? 'cursor-grabbing' : 'cursor-grab'} select-none`} ref={draggableRef}>
{children}
</div>
)
}

Draggable.defaultProps = { left: 0, top: 0 }
4 changes: 2 additions & 2 deletions components/GlobalHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ const getSEOMeta = (props, router, global) => {
}
case '/category/[category]':
return {
title: `${category} | ${locale.COMMON.CATEGORY} | ${siteInfo?.title} || ''}`,
title: `${category} | ${locale.COMMON.CATEGORY} | ${siteInfo?.title}`,
description: `${siteInfo?.description}`,
slug: 'category/' + category,
image: `${siteInfo?.pageCover}`,
type: 'website'
}
case '/category/[category]/page/[page]':
return {
title: `${category} | ${locale.COMMON.CATEGORY} | ${siteInfo?.title} || ''}`,
title: `${category} | ${locale.COMMON.CATEGORY} | ${siteInfo?.title}`,
description: `${siteInfo?.description}`,
slug: 'category/' + category,
image: `${siteInfo?.pageCover}`,
Expand Down
14 changes: 9 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { deepClone } from './utils'
export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
let global = null
try {
const isClient = typeof window !== 'undefined';
const isClient = typeof window !== 'undefined'
// eslint-disable-next-line react-hooks/rules-of-hooks
global = isClient ? useGlobal() : {}
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down Expand Up @@ -67,15 +67,19 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
} else {
if (typeof val === 'string') {
if (val === 'true' || val === 'false') {
return JSON.parse(val);
return JSON.parse(val)
}
return val;
if (/^\d+$/.test(val)) {
// 如果是数字,使用parseFloat或者parseInt将字符串转换为数字
return parseInt(val)
}
return val
} else {
try {
return JSON.parse(val);
return JSON.parse(val)
} catch (error) {
// 如果值是一个字符串但不是有效的 JSON 格式,直接返回字符串
return val;
return val
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import BLOG from '@/blog.config'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
import { idToUuid } from 'notion-utils'
import { deepClone } from '@/lib/utils'
import { getAllCategories } from '@/lib/notion/getAllCategories'
import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties from '@/lib/notion/getPageProperties'
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import { deepClone } from '@/lib/utils'
import { idToUuid } from 'notion-utils'

export { getAllTags } from '../notion/getAllTags'
export { getPostBlocks } from '../notion/getPostBlocks'
export { getPost } from '../notion/getNotionPost'
export { getPostBlocks } from '../notion/getPostBlocks'

/**
* 获取博客数据; 基于Notion实现
Expand Down Expand Up @@ -275,7 +275,8 @@ export function getNavPages({ allPages }) {
slug: item.slug,
pageIcon: item.pageIcon || '',
lastEditedDate: item.lastEditedDate,
publishDate: item.publishDate
publishDate: item.publishDate,
ext: item.ext || {}
}))
}

Expand Down
40 changes: 29 additions & 11 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getTextContent, getDateValue } from 'notion-utils'
import { NotionAPI } from 'notion-client'
import BLOG from '@/blog.config'
import { NotionAPI } from 'notion-client'
import { getDateValue, getTextContent } from 'notion-utils'
import formatDate from '../utils/formatDate'
// import { createHash } from 'crypto'
import md5 from 'js-md5'
Expand Down Expand Up @@ -49,8 +49,7 @@ export default async function getPageProperties(id, value, schema, authToken, ta
if (rawUsers[i][0][1]) {
const userId = rawUsers[i][0]
const res = await api.getUsers(userId)
const resValue =
res?.recordMapWithRoles?.notion_user?.[userId[1]]?.value
const resValue = res?.recordMapWithRoles?.notion_user?.[userId[1]]?.value
const user = {
id: resValue?.id,
first_name: resValue?.given_name,
Expand Down Expand Up @@ -93,16 +92,17 @@ export default async function getPageProperties(id, value, schema, authToken, ta
properties.pageIcon = mapImgUrl(value?.format?.page_icon, value) ?? ''
properties.pageCover = mapImgUrl(value?.format?.page_cover, value) ?? ''
properties.pageCoverThumbnail = mapImgUrl(value?.format?.page_cover, value, 'block', 'pageCoverThumbnail') ?? ''

properties.ext = converToJSON(properties?.ext)
properties.content = value.content ?? []
properties.tagItems = properties?.tags?.map(tag => {
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
}) || []
properties.tagItems =
properties?.tags?.map(tag => {
return { name: tag, color: tagOptions?.find(t => t.value === tag)?.color || 'gray' }
}) || []
delete properties.content

// 处理URL
if (properties.type === 'Post') {
properties.slug = (BLOG.POST_URL_PREFIX) ? generateCustomizeUrl(properties) : (properties.slug ?? properties.id)
properties.slug = BLOG.POST_URL_PREFIX ? generateCustomizeUrl(properties) : properties.slug ?? properties.id
} else if (properties.type === 'Page') {
properties.slug = properties.slug ?? properties.id
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
Expand All @@ -122,6 +122,24 @@ export default async function getPageProperties(id, value, schema, authToken, ta
return properties
}

/**
* 字符串转json
* @param {*} str
* @returns
*/
function converToJSON(str) {
if (!str) {
return {}
}
// 使用正则表达式去除空格和换行符
try {
return JSON.parse(str.replace(/\s/g, ''))
} catch (error) {
console.warn('无效JSON', str)
return {}
}
}

/**
* 映射用户自定义表头
*/
Expand Down Expand Up @@ -164,7 +182,7 @@ function generateCustomizeUrl(postProperties) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)
fullPrefix += String(formatPostCreatedDate.getUTCDate()).padStart(2, 0)
} else if (pattern === '%slug%') {
fullPrefix += (postProperties.slug ?? postProperties.id)
fullPrefix += postProperties.slug ?? postProperties.id
} else if (!pattern.includes('%')) {
fullPrefix += pattern
} else {
Expand All @@ -180,5 +198,5 @@ function generateCustomizeUrl(postProperties) {
if (fullPrefix.endsWith('/')) {
fullPrefix = fullPrefix.substring(0, fullPrefix.length - 1) // 去掉尾部部的"/"
}
return `${fullPrefix}/${(postProperties.slug ?? postProperties.id)}`
return `${fullPrefix}/${postProperties.slug ?? postProperties.id}`
}
18 changes: 17 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
// 封装异步加载资源的方法
import { memo } from 'react'
import { memo } from 'react';

/**
* 判断是否客户端
* @returns {boolean}
*/
export const isBrowser = typeof window !== 'undefined'

/**
* 打乱数组
* @param {*} array
* @returns
*/
export const shuffleArray = (array) => {
if (!array) {
return []
}
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}

/**
* google机器人
* @returns
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notion-next",
"version": "4.4.0",
"version": "4.4.2",
"homepage": "https://github.com/tangly1024/NotionNext.git",
"license": "MIT",
"repository": {
Expand Down
Loading

0 comments on commit f98dc4b

Please sign in to comment.