diff --git a/pages/index.js b/pages/index.js
index be549212..599b3cf9 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -27,7 +27,6 @@ export async function getStaticProps () {
BLOG.POSTS_PER_PAGE * page
)
if (THEME_CONFIG.POST_LIST_PREVIEW || BLOG.POST_LIST_PREVIEW) {
- console.log('加载预览')
for (const i in postsToShow) {
const post = postsToShow[i]
const blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES)
diff --git a/pages/page/[page].js b/pages/page/[page].js
index f0f67312..211a2f92 100644
--- a/pages/page/[page].js
+++ b/pages/page/[page].js
@@ -45,7 +45,6 @@ export async function getStaticProps ({ params: { page } }) {
)
// 加载预览
if (THEME_CONFIG.POST_LIST_PREVIEW || BLOG.POST_LIST_PREVIEW) {
- console.log('加载预览')
for (const i in postsToShow) {
const post = postsToShow[i]
const blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES)
diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js
new file mode 100644
index 00000000..00c6d891
--- /dev/null
+++ b/pages/search/[keyword].js
@@ -0,0 +1,98 @@
+import { getGlobalNotionData } from '@/lib/notion/getNotionData'
+import { LayoutSearch } from '@/themes'
+import BLOG from '@/blog.config'
+import { useGlobal } from '@/lib/global'
+import cache from 'memory-cache'
+
+export async function getStaticPaths () {
+ return {
+ paths: [],
+ fallback: true
+ }
+}
+
+/**
+ * 将对象的指定字段拼接到字符串
+ * @param sourceText
+ * @param targetObj
+ * @param key
+ * @returns {*}
+ */
+function concatPropsText (sourceText, targetObj, key) {
+ if (!targetObj) {
+ return sourceText
+ }
+ const textArray = targetObj[key]
+ const text = textArray ? textArray[0][0] : ''
+ if (text && text !== 'Untitled') {
+ return sourceText.concat(text)
+ }
+ return sourceText
+}
+
+export async function getStaticProps ({ params: { keyword } }) {
+ const {
+ allPosts,
+ categories,
+ tags,
+ postCount,
+ latestPosts,
+ customNav
+ } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] })
+
+ const filterPosts = []
+ allPosts.forEach(post => {
+ const cacheKey = 'page_block_' + post.id
+ const page = cache.get(cacheKey)
+ const tagContent = post.tags ? post.tags.join(' ') : ''
+ const categoryContent = post.category ? post.category.join(' ') : ''
+ let indexContent = post.title + ' ' + post.summary + ' ' + tagContent + ' ' + categoryContent
+ if (page != null) {
+ const contentIds = Object.keys(page.block)
+ contentIds.forEach(id => {
+ const properties = page?.block[id]?.value?.properties
+ indexContent = concatPropsText(indexContent, properties, 'title')
+ indexContent = concatPropsText(indexContent, properties, 'caption')
+ })
+ }
+ post.results = []
+ const index = indexContent.indexOf(keyword)
+ if (index > -1) {
+ filterPosts.push(post)
+ let start = index - 20
+ let end = index + 20
+ if (start < 0) start = 0
+ if (end > indexContent.length) end = indexContent.length
+ const referText = indexContent.substr(start, end).replaceAll(keyword, `${keyword}`)
+ post.results.push(`${referText}`)
+ }
+ })
+
+ console.log(filterPosts)
+
+ return {
+ props: {
+ posts: filterPosts,
+ tags,
+ categories,
+ postCount,
+ latestPosts,
+ customNav,
+ keyword
+ },
+ revalidate: 1
+ }
+}
+
+const Index = (props) => {
+ const { keyword } = props
+ const { locale } = useGlobal()
+ const meta = {
+ title: `${keyword || ''} | ${locale.NAV.SEARCH} | ${BLOG.TITLE} `,
+ description: BLOG.DESCRIPTION,
+ type: 'website'
+ }
+ return
{post.summary} + {/* 搜索结果 */} + {post.results && + {post.results.map(r => ......)} + + } +
} {showPreview && post?.blockMap &&