Skip to content

Commit

Permalink
lastEditTime 字段名调整;修改algolia逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jul 29, 2023
1 parent c13eb5e commit 7841b55
Show file tree
Hide file tree
Showing 39 changed files with 148 additions and 80 deletions.
2 changes: 1 addition & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const BLOG = {
ALGOLIA_ADMIN_APP_KEY: process.env.ALGOLIA_ADMIN_APP_KEY || null, // 管理后台的KEY,不要暴露在代码中,在这里查看 https://dashboard.algolia.com/account/api-keys/
ALGOLIA_SEARCH_ONLY_APP_KEY: process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_ONLY_APP_KEY || null, // 客户端搜索用的KEY
ALGOLIA_INDEX: process.env.NEXT_PUBLIC_ALGOLIA_INDEX || null, // 在Algolia中创建一个index用作数据库
ALGOLIA_RECREATE_DATA: process.env.ALGOLIA_RECREATE_DATA || process.env.npm_lifecycle_event === 'build', // 为true时重新构建索引数据; 默认在build时会构建
// ALGOLIA_RECREATE_DATA: process.env.ALGOLIA_RECREATE_DATA || process.env.npm_lifecycle_event === 'build', // 为true时重新构建索引数据; 默认在build时会构建

PREVIEW_CATEGORY_COUNT: 16, // 首页最多展示的分类数量,0为不限制
PREVIEW_TAG_COUNT: 16, // 首页最多展示的标签数量,0为不限制
Expand Down
4 changes: 2 additions & 2 deletions components/AlgoliaSearchModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ function TagGroups(props) {
return <Link passHref
key={index}
href={`/tag/${encodeURIComponent(tag.name)}`}
className={'cursor-pointer inline-block whitespace-nowrap'}>
<div className={' flex items-center hover:bg-blue-600 dark:hover:bg-yellow-600 hover:scale-110 hover:text-white rounded-lg px-2 py-0.5 duration-150 transition-all'}>
className={'cursor-pointer inline-block whitespace-nowrap'}>
<div className={' flex items-center text-black dark:text-gray-300 hover:bg-blue-600 dark:hover:bg-yellow-600 hover:scale-110 hover:text-white rounded-lg px-2 py-0.5 duration-150 transition-all'}>
<div className='text-lg'>{tag.name} </div>{tag.count ? <sup className='relative ml-1'>{tag.count}</sup> : <></>}
</div>

Expand Down
2 changes: 1 addition & 1 deletion components/CommonHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const CommonHead = ({ meta, children }) => {
<>
<meta
property="article:published_time"
content={meta.publishTime}
content={meta.publishDay}
/>
<meta property="article:author" content={BLOG.AUTHOR} />
<meta property="article:section" content={category} />
Expand Down
88 changes: 75 additions & 13 deletions lib/algolia.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,88 @@ const generateAlgoliaSearch = async({ allPages, force = false }) => {

/**
* 上传数据
* 根据上次修改文章日期和上次更新索引数据判断是否需要更新algolia索引
*/
const uploadDataToAlgolia = (post) => {
const uploadDataToAlgolia = async(post) => {
// Connect and authenticate with your Algolia app
const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY)

// Create a new index and add a record
const index = client.initIndex(BLOG.ALGOLIA_INDEX)
const record = {
objectID: post.id,
title: post.title,
category: post.category,
tags: post.tags,
pageCover: post.pageCover,
slug: post.slug,
summary: post.summary,
content: getPageContentText(post, post.blockMap)

if (!post) {
return
}
index.saveObject(record).wait().then(r => {
console.log('Algolia索引', r, record)
})

// 检查是否有索引
let existed
let needUpdateIndex = false
try {
existed = await index.getObject(post.id)
} catch (error) {
// 通常是不存在索引
}

if (!existed || !existed?.lastEditedDate || !existed?.lastIndexDate) {
needUpdateIndex = true
} else {
const lastEditedDate = new Date(existed.lastEditedDate)
const lastIndexDate = new Date(existed.lastIndexDate)
if (lastEditedDate.getTime() > lastIndexDate.getTime()) {
needUpdateIndex = true
}
}

// 如果需要更新搜索
if (needUpdateIndex) {
const record = {
objectID: post.id,
title: post.title,
category: post.category,
tags: post.tags,
pageCover: post.pageCover,
slug: post.slug,
summary: post.summary,
lastEditedDate: post.lastEditedDate, // 更新文章时间
lastIndexDate: new Date(), // 更新索引时间
content: truncate(getPageContentText(post, post.blockMap), 9000) // 索引9000个字节,因为api限制总请求内容上限1万个字节
}
console.log('更新Algolia索引', record)
index.saveObject(record).wait().then(r => {
console.log('更新成功', r)
}).catch(err => {
console.log('algolia', err)
})
}
}

/**
* 限制内容字节数
* @param {*} str
* @param {*} maxBytes
* @returns
*/
function truncate(str, maxBytes) {
let count = 0
let result = ''
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i)
if (code <= 0x7f) {
count += 1
} else if (code <= 0x7ff) {
count += 2
} else if (code <= 0xffff) {
count += 3
} else {
count += 4
}
if (count <= maxBytes) {
result += str[i]
} else {
break
}
}
return result
}

export { uploadDataToAlgolia, generateAlgoliaSearch }
2 changes: 1 addition & 1 deletion lib/notion/getNotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getNotion(pageId) {
title: postInfo?.properties?.title?.[0],
status: 'Published',
createdTime: formatDate(new Date(postInfo.created_time).toString(), BLOG.LANG),
lastEditedTime: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG),
lastEditedDay: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG),
fullWidth: false,
page_cover: getPageCover(postInfo),
date: { start_date: formatDate(new Date(postInfo?.last_edited_time).toString(), BLOG.LANG) },
Expand Down
9 changes: 5 additions & 4 deletions lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export async function getGlobalData({
from
}) {
// 从notion获取
const db = deepClone(await getNotionPageData({ pageId, from }))
const data = await getNotionPageData({ pageId, from })
const db = deepClone(data)
// 不返回的敏感数据
delete db.block
delete db.schema
Expand All @@ -48,8 +49,8 @@ function getLatestPosts({ allPages, from, latestPostCount }) {
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')

const latestPosts = Object.create(allPosts).sort((a, b) => {
const dateA = new Date(a?.lastEditedTime || a?.publishDate)
const dateB = new Date(b?.lastEditedTime || b?.publishDate)
const dateA = new Date(a?.lastEditedDay || a?.publishDate)
const dateB = new Date(b?.lastEditedDay || b?.publishDate)
return dateB - dateA
})
return latestPosts.slice(0, latestPostCount)
Expand Down Expand Up @@ -217,7 +218,7 @@ const EmptyData = (pageId) => {
const empty = {
notice: null,
siteInfo: getSiteInfo({}),
allPages: [{ id: 1, title: `无法获取Notion数据,请检查Notion_ID: \n 当前 ${pageId}`, summary: '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next', status: 'Published', type: 'Post', slug: '13a171332816461db29d50e9f575b00d', date: { start_date: '2023-04-24', lastEditedTime: '2023-04-24', tagItems: [] } }],
allPages: [{ id: 1, title: `无法获取Notion数据,请检查Notion_ID: \n 当前 ${pageId}`, summary: '访问文档获取帮助→ https://tangly1024.com/article/vercel-deploy-notion-next', status: 'Published', type: 'Post', slug: '13a171332816461db29d50e9f575b00d', date: { start_date: '2023-04-24', lastEditedDay: '2023-04-24', tagItems: [] } }],
allNavPages: [],
collection: [],
collectionQuery: {},
Expand Down
17 changes: 9 additions & 8 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ export default async function getPageProperties(id, block, schema, authToken, ta
mapProperties(properties)

properties.publishDate = new Date(properties?.date?.start_date || value.created_time).getTime()
properties.publishTime = formatDate(properties.publishDate, BLOG.LANG)
properties.lastEditedTime = formatDate(new Date(value?.last_edited_time), BLOG.LANG)
properties.publishDay = formatDate(properties.publishDate, BLOG.LANG)
properties.lastEditedDate = new Date(value?.last_edited_time)
properties.lastEditedDay = formatDate(new Date(value?.last_edited_time), BLOG.LANG)
properties.fullWidth = value.format?.page_full_width ?? false
properties.pageIcon = mapImgUrl(block[id].value?.format?.page_icon, block[id].value) ?? ''
properties.pageCover = mapImgUrl(block[id].value?.format?.page_cover, block[id].value) ?? ''
Expand Down Expand Up @@ -141,14 +142,14 @@ function generateCustomizeUrl(postProperties) {
let fullPrefix = ''
const allSlugPatterns = BLOG.POST_URL_PREFIX.split('/')
allSlugPatterns.forEach((pattern, idx) => {
if (pattern === '%year%' && postProperties?.publishTime) {
const formatPostCreatedDate = new Date(postProperties?.publishTime)
if (pattern === '%year%' && postProperties?.publishDay) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)
fullPrefix += formatPostCreatedDate.getUTCFullYear()
} else if (pattern === '%month%' && postProperties?.publishTime) {
const formatPostCreatedDate = new Date(postProperties?.publishTime)
} else if (pattern === '%month%' && postProperties?.publishDay) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)
fullPrefix += String(formatPostCreatedDate.getUTCMonth() + 1).padStart(2, 0)
} else if (pattern === '%day%' && postProperties?.publishTime) {
const formatPostCreatedDate = new Date(postProperties?.publishTime)
} else if (pattern === '%day%' && postProperties?.publishDay) {
const formatPostCreatedDate = new Date(postProperties?.publishDay)
fullPrefix += String(formatPostCreatedDate.getUTCDate()).padStart(2, 0)
} else if (pattern === '%slug%') {
fullPrefix += (postProperties.slug ?? postProperties.id)
Expand Down
2 changes: 1 addition & 1 deletion lib/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function generateRss(posts) {
link: `${BLOG.LINK}/${post.slug}`,
description: post.summary,
content: await createFeedContent(post),
date: new Date(post?.publishTime)
date: new Date(post?.publishDay)
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/sitemap.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function generateSitemapXml({ allPages }) {
const slugWithoutLeadingSlash = post?.slug?.startsWith('/') ? post?.slug?.slice(1) : post.slug
urls.push({
loc: `${BLOG.LINK}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishTime).toISOString().split('T')[0],
lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
changefreq: 'daily'
})
})
Expand Down
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ export function deepClone(obj) {
if (obj && typeof obj === 'object') {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
newObj[key] = (obj && typeof obj[key] === 'object') ? deepClone(obj[key]) : obj[key]
if (obj[key] instanceof Date) { // 判断属性值是否为 Date 对象
newObj[key] = new Date(obj[key].getTime()).toISOString() // 直接拷贝引用
} else if (obj[key] && typeof obj[key] === 'object') {
newObj[key] = deepClone(obj[key])
} else {
newObj[key] = obj[key]
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pages/[prefix]/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export async function getStaticProps({ params: { prefix, slug } }) {
if (!props?.posts?.blockMap) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}
// 生成全文索引
if (BLOG.ALGOLIA_APP_ID && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA) && process.env.npm_lifecycle_event === 'build') {
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
if (BLOG.ALGOLIA_APP_ID) {
uploadDataToAlgolia(props?.post)
}

Expand Down
12 changes: 7 additions & 5 deletions pages/[prefix]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ export async function getStaticPaths() {
}
}

export async function getStaticProps({ params: { prefix } }) {
let fullSlug = prefix
export async function getStaticProps(p) {
const { params } = p
console.log('getStaticProps', p)
let fullSlug = params.prefix
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
if (!fullSlug.endsWith('.html')) {
fullSlug += '.html'
Expand All @@ -111,7 +113,7 @@ export async function getStaticProps({ params: { prefix } }) {

// 处理非列表内文章的内信息
if (!props?.post) {
const pageId = prefix.slice(-1)[0]
const pageId = params.PSEUDO_STATICprefix.slice(-1)[0]
if (pageId.length >= 32) {
const post = await getNotion(pageId)
props.post = post
Expand All @@ -129,8 +131,8 @@ export async function getStaticProps({ params: { prefix } }) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}

// 生成全文索引
if (BLOG.ALGOLIA_APP_ID && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA) && process.env.npm_lifecycle_event === 'build') {
// 生成全文索引 && process.env.npm_lifecycle_event === 'build' && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
if (BLOG.ALGOLIA_APP_ID) {
uploadDataToAlgolia(props?.post)
}

Expand Down
4 changes: 0 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { generateRobotsTxt } from '@/lib/robots.txt'

import { useRouter } from 'next/router'
import { getLayoutByTheme } from '@/themes/theme'
import { generateAlgoliaSearch } from '@/lib/algolia'

/**
* 首页布局
Expand Down Expand Up @@ -64,9 +63,6 @@ export async function getStaticProps() {
}

// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
if (BLOG.ALGOLIA_APP_ID && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)) {
generateAlgoliaSearch({ allPages: props.allPages })
}

delete props.allPages

Expand Down
2 changes: 1 addition & 1 deletion pages/sitemap.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getServerSideProps = async (ctx) => {
const slugWithoutLeadingSlash = post?.slug.startsWith('/') ? post?.slug?.slice(1) : post.slug
return {
loc: `${BLOG.LINK}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishTime).toISOString().split('T')[0],
lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
changefreq: 'daily',
priority: '0.7'
}
Expand Down
4 changes: 2 additions & 2 deletions themes/example/components/ArticleInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const ArticleInfo = (props) => {
passHref
className="pl-1 mr-2 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 border-b dark:border-gray-500 border-dashed">

{post?.publishTime}
{post?.publishDay}

</Link>
<span className='mr-2'>|</span>
<span className='mx-2 text-gray-400 dark:text-gray-500'>
{locale.COMMON.LAST_EDITED_TIME}: {post?.lastEditedTime}
{locale.COMMON.LAST_EDITED_TIME}: {post?.lastEditedDay}
</span>
<span className='mr-2'>|</span>
<span className="hidden busuanzi_container_page_pv font-light mr-2">
Expand Down
4 changes: 2 additions & 2 deletions themes/example/components/BlogListGroupByDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default function BlogListGroupByDate({ archiveTitle, archivePosts }) {
key={post.id}
className="border-l-2 p-1 text-xs md:text-base items-center hover:scale-x-105 hover:border-gray-500 dark:hover:border-gray-300 dark:border-gray-400 transform duration-500"
>
<div id={post?.publishTime}>
<div id={post?.publishDay}>
<span className="text-gray-400">
{post?.publishTime}
{post?.publishDay}
</span>{' '}
&nbsp;
<Link href={`${BLOG.SUB_PATH}/${post.slug}`} className="dark:text-gray-400 dark:hover:text-gray-300 overflow-x-hidden hover:underline cursor-pointer text-gray-600">
Expand Down
4 changes: 2 additions & 2 deletions themes/fukasawa/components/ArticleDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export default function ArticleDetail(props) {
passHref
className="pl-1 mr-2 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 border-b dark:border-gray-500 border-dashed">

{post?.publishTime}
{post?.publishDay}

</Link>
<span className='mr-2'>|</span>
<span className='mx-2 text-gray-400 dark:text-gray-500'>
{locale.COMMON.LAST_EDITED_TIME}: {post.lastEditedTime}
{locale.COMMON.LAST_EDITED_TIME}: {post.lastEditedDay}
</span>
</>)}

Expand Down
2 changes: 1 addition & 1 deletion themes/fukasawa/components/BlogPostArchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const BlogArchiveItem = ({ posts = [], archiveTitle }) => {
key={post.id}
className="border-l-2 p-1 text-xs md:text-base items-center hover:scale-x-105 hover:border-gray-500 dark:hover:border-gray-300 dark:border-gray-400 transform duration-500"
>
<div id={post?.publishTime}>
<div id={post?.publishDay}>
<span className="text-gray-400">{post.date?.start_date}</span>{' '}
&nbsp;
<Link
Expand Down
2 changes: 1 addition & 1 deletion themes/heo/components/ArticleAdjacent.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function ArticleAdjacent({ prev, next }) {

{/* 桌面端 */}

<div id='pc-next-post' className={`hidden md:block fixed z-20 right-16 bottom-4 duration-200 transition-all ${isScrollEnd ? 'mb-0 opacity-100' : '-mb-24 opacity-0'}`}>
<div id='pc-next-post' className={`hidden md:block fixed z-40 right-16 bottom-4 duration-200 transition-all ${isScrollEnd ? 'mb-0 opacity-100' : '-mb-24 opacity-0'}`}>
<Link
href={`/${next.slug}`}
className='cursor-pointer duration transition-all h-24 dark:bg-[#1e1e1e] border dark:border-gray-600 p-3 bg-white dark:text-gray-300 dark:hover:text-yellow-600 hover:text-white hover:font-bold hover:bg-gray-400 rounded-lg flex flex-col justify-between'
Expand Down
2 changes: 1 addition & 1 deletion themes/heo/components/LatestPostsGroupMini.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function LatestPostsGroupMini ({ latestPosts, siteInfo }) {
>
<div>
<div className='line-clamp-2 menu-link'>{post.title}</div>
<div className="text-gray-500">{post.lastEditedTime}</div>
<div className="text-gray-500">{post.lastEditedDay}</div>
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions themes/heo/components/PostHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ export default function PostHeader({ post, siteInfo }) {
href={`/archive#${formatDateFmt(post?.publishDate, 'yyyy-MM')}`}
passHref
className="pl-1 mr-2 cursor-pointer hover:underline">
<i className="fa-regular fa-calendar"></i> {post?.publishTime}
<i className="fa-regular fa-calendar"></i> {post?.publishDay}
</Link>
</>
)}

<div className="pl-1 mr-2">
<i className="fa-regular fa-calendar-check"></i> {post.lastEditedTime}
<i className="fa-regular fa-calendar-check"></i> {post.lastEditedDay}
</div>

</div>
Expand Down
Loading

0 comments on commit 7841b55

Please sign in to comment.