diff --git a/blog.config.js b/blog.config.js index 2a2ce299..6bba2385 100644 --- a/blog.config.js +++ b/blog.config.js @@ -124,6 +124,11 @@ const BLOG = { POSTS_PER_PAGE: 12, // post counts per page POSTS_SORT_BY: process.env.NEXT_PUBLIC_POST_SORT_BY || 'notion', // 排序方式 'date'按时间,'notion'由notion控制 + ALGOLIA_APP_ID: process.env.ALGOLIA_APP_ID || null, // 在这里查看 https://dashboard.algolia.com/account/api-keys/ + ALGOLIA_APP_KEY: process.env.ALGOLIA_APP_KEY || null, // 管理后台的KEY,不会暴露给前端,在这里查看 https://dashboard.algolia.com/account/api-keys/ + ALGOLIA_APP_SEARCH_KEY: process.env.ALGOLIA_APP_SEARCH_KEY || null, // 客户端搜索用的KEY + ALGOLIA_INDEX: process.env.ALGOLIA_INDEX || null, // 在Algolia中创建一个index用作数据库 + PREVIEW_CATEGORY_COUNT: 16, // 首页最多展示的分类数量,0为不限制 PREVIEW_TAG_COUNT: 16, // 首页最多展示的标签数量,0为不限制 diff --git a/lib/algolia.js b/lib/algolia.js new file mode 100644 index 00000000..d54b3ea6 --- /dev/null +++ b/lib/algolia.js @@ -0,0 +1,29 @@ +import BLOG from '@/blog.config' +import { getPageContentText } from '@/pages/search/[keyword]' +import algoliasearch from 'algoliasearch' + +/** + * 上传数据 + */ +const uploadDataToAlgolia = (post) => { + // Connect and authenticate with your Algolia app + const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_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) + } + index.saveObject(record).wait().then(r => { + console.log('Algolia索引', r, record) + }) +} + +export { uploadDataToAlgolia } diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 89126d9c..298795f6 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -185,10 +185,10 @@ export function getNavPages({ allPages }) { const result = allNavPages.map(item => ({ id: item.id, title: item.title || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug })) const groupedArray = result.reduce((groups, item) => { - const categoryName = item.category ? item.category.join('/') : '' // 将category转换为字符串 + const categoryName = item?.category ? item?.category : '' // 将category转换为字符串 const lastGroup = groups[groups.length - 1] // 获取最后一个分组 - if (!lastGroup || lastGroup.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组 + if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组 groups.push({ category: categoryName, items: [] }) } diff --git a/lib/notion/getPageProperties.js b/lib/notion/getPageProperties.js index 47e497ce..ab6e5431 100644 --- a/lib/notion/getPageProperties.js +++ b/lib/notion/getPageProperties.js @@ -69,9 +69,10 @@ export default async function getPageProperties(id, block, schema, authToken, ta }) } - // type\status是下拉选框 取数组第一个 - properties.type = properties.type?.[0] - properties.status = properties.status?.[0] + // type\status\category 是单选下拉框 取数组第一个 + properties.type = properties.type?.[0] || '' + properties.status = properties.status?.[0] || '' + properties.category = properties.category?.[0] || '' // 映射值:用户个性化type和status字段的下拉框选项,在此映射回代码的英文标识 mapProperties(properties) diff --git a/package.json b/package.json index adc8d380..6203ee96 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@headlessui/react": "^1.7.15", "@next/bundle-analyzer": "^12.1.1", "@vercel/analytics": "^1.0.0", + "algoliasearch": "^4.18.0", "animate.css": "^4.1.1", "animejs": "^3.2.1", "aos": "^3.0.0-beta.6", diff --git a/pages/[...slug].js b/pages/[...slug].js index 1523c674..6db02d63 100644 --- a/pages/[...slug].js +++ b/pages/[...slug].js @@ -9,6 +9,7 @@ import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents' import { getLayoutByTheme } from '@/themes/theme' import md5 from 'js-md5' import { isBrowser } from '@/lib/utils' +import { uploadDataToAlgolia } from '@/lib/algolia' /** * 根据notion的slug访问页面 @@ -128,6 +129,10 @@ export async function getStaticProps({ params: { slug } }) { props.post.blockMap = await getPostBlocks(props.post.id, from) } + if (BLOG.ALGOLIA_APP_ID && BLOG.ALGOLIA_APP_KEY) { + uploadDataToAlgolia(props?.post) + } + // 推荐关联文章处理 const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') if (allPosts && allPosts.length > 0) { diff --git a/pages/index.js b/pages/index.js index af85c245..fd478612 100644 --- a/pages/index.js +++ b/pages/index.js @@ -6,6 +6,7 @@ import { generateRobotsTxt } from '@/lib/robots.txt' import { useRouter } from 'next/router' import { getLayoutByTheme } from '@/themes/theme' + /** * 首页布局 * @param {*} props diff --git a/pages/search/[keyword]/index.js b/pages/search/[keyword]/index.js index c2e9817c..93fa6826 100644 --- a/pages/search/[keyword]/index.js +++ b/pages/search/[keyword]/index.js @@ -121,16 +121,7 @@ async function filterByMemCache(allPosts, keyword) { const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : '' const articleInfo = post.title + post.summary + tagContent + categoryContent let hit = articleInfo.toLowerCase().indexOf(keyword) > -1 - let indexContent = [post.summary] - // 防止搜到加密文章的内容 - if (page && page.block && !post.password) { - const contentIds = Object.keys(page.block) - contentIds.forEach(id => { - const properties = page?.block[id]?.value?.properties - indexContent = appendText(indexContent, properties, 'title') - indexContent = appendText(indexContent, properties, 'caption') - }) - } + const indexContent = getPageContentText(post, page) // console.log('全文搜索缓存', cacheKey, page != null) post.results = [] let hitCount = 0 @@ -157,4 +148,18 @@ async function filterByMemCache(allPosts, keyword) { return filterPosts } +export function getPageContentText(post, pageBlockMap) { + let indexContent = [] + // 防止搜到加密文章的内容 + if (pageBlockMap && pageBlockMap.block && !post.password) { + const contentIds = Object.keys(pageBlockMap.block) + contentIds.forEach(id => { + const properties = pageBlockMap?.block[id]?.value?.properties + indexContent = appendText(indexContent, properties, 'title') + indexContent = appendText(indexContent, properties, 'caption') + }) + } + return indexContent.join('') +} + export default Index