mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-02 23:16:51 +00:00
多语言支持初版,不支持static export
This commit is contained in:
17
pages/404.js
17
pages/404.js
@@ -1,7 +1,7 @@
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 404
|
||||
@@ -10,12 +10,17 @@ import { siteConfig } from '@/lib/config'
|
||||
*/
|
||||
const NoFound = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps () {
|
||||
const props = (await getGlobalData({ from: '404' })) || {}
|
||||
export async function getStaticProps(req) {
|
||||
const { locale } = req
|
||||
|
||||
const props = (await getGlobalData({ from: '404', locale })) || {}
|
||||
return { props }
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,11 @@ export async function getStaticPaths() {
|
||||
paths: allPages
|
||||
?.filter(row => checkSlug(row))
|
||||
.map(row => ({
|
||||
params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1], suffix: row.slug.split('/').slice(1) }
|
||||
params: {
|
||||
prefix: row.slug.split('/')[0],
|
||||
slug: row.slug.split('/')[1],
|
||||
suffix: row.slug.split('/').slice(1)
|
||||
}
|
||||
})),
|
||||
fallback: true
|
||||
}
|
||||
@@ -46,7 +50,10 @@ export async function getStaticPaths() {
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
export async function getStaticProps({ params: { prefix, slug, suffix } }) {
|
||||
export async function getStaticProps({
|
||||
params: { prefix, slug, suffix },
|
||||
locale
|
||||
}) {
|
||||
let fullSlug = prefix + '/' + slug + '/' + suffix.join('/')
|
||||
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
|
||||
if (!fullSlug.endsWith('.html')) {
|
||||
@@ -54,10 +61,13 @@ export async function getStaticProps({ params: { prefix, slug, suffix } }) {
|
||||
}
|
||||
}
|
||||
const from = `slug-props-${fullSlug}`
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
// 在列表内查找文章
|
||||
props.post = props?.allPages?.find(p => {
|
||||
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
return (
|
||||
p.type.indexOf('Menu') < 0 &&
|
||||
(p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
)
|
||||
})
|
||||
|
||||
// 处理非列表内文章的内信息
|
||||
@@ -85,12 +95,18 @@ export async function getStaticProps({ params: { prefix, slug, suffix } }) {
|
||||
}
|
||||
|
||||
// 推荐关联文章处理
|
||||
const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
if (allPosts && allPosts.length > 0) {
|
||||
const index = allPosts.indexOf(props.post)
|
||||
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
|
||||
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
|
||||
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
|
||||
props.recommendPosts = getRecommendPost(
|
||||
props.post,
|
||||
allPosts,
|
||||
siteConfig('POST_RECOMMEND_COUNT')
|
||||
)
|
||||
} else {
|
||||
props.prev = null
|
||||
props.next = null
|
||||
@@ -109,7 +125,11 @@ function checkSlug(row) {
|
||||
if (slug.startsWith('/')) {
|
||||
slug = slug.substring(1)
|
||||
}
|
||||
return (slug.match(/\//g) || []).length >= 2 && row.type.indexOf('Menu') < 0 && !checkContainHttp(slug)
|
||||
return (
|
||||
(slug.match(/\//g) || []).length >= 2 &&
|
||||
row.type.indexOf('Menu') < 0 &&
|
||||
!checkContainHttp(slug)
|
||||
)
|
||||
}
|
||||
|
||||
export default PrefixSlug
|
||||
|
||||
@@ -28,14 +28,16 @@ export async function getStaticPaths() {
|
||||
const { allPages } = await getGlobalData({ from })
|
||||
const paths = allPages
|
||||
?.filter(row => checkSlug(row))
|
||||
.map(row => ({ params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1] } }))
|
||||
.map(row => ({
|
||||
params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1] }
|
||||
}))
|
||||
return {
|
||||
paths: paths,
|
||||
fallback: true
|
||||
}
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params: { prefix, slug } }) {
|
||||
export async function getStaticProps({ params: { prefix, slug }, locale }) {
|
||||
let fullSlug = prefix + '/' + slug
|
||||
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
|
||||
if (!fullSlug.endsWith('.html')) {
|
||||
@@ -43,10 +45,13 @@ export async function getStaticProps({ params: { prefix, slug } }) {
|
||||
}
|
||||
}
|
||||
const from = `slug-props-${fullSlug}`
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
// 在列表内查找文章
|
||||
props.post = props?.allPages?.find(p => {
|
||||
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
return (
|
||||
p.type.indexOf('Menu') < 0 &&
|
||||
(p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
)
|
||||
})
|
||||
|
||||
// 处理非列表内文章的内信息
|
||||
@@ -74,12 +79,18 @@ export async function getStaticProps({ params: { prefix, slug } }) {
|
||||
}
|
||||
|
||||
// 推荐关联文章处理
|
||||
const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
if (allPosts && allPosts.length > 0) {
|
||||
const index = allPosts.indexOf(props.post)
|
||||
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
|
||||
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
|
||||
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
|
||||
props.recommendPosts = getRecommendPost(
|
||||
props.post,
|
||||
allPosts,
|
||||
siteConfig('POST_RECOMMEND_COUNT')
|
||||
)
|
||||
} else {
|
||||
props.prev = null
|
||||
props.next = null
|
||||
@@ -97,6 +108,10 @@ function checkSlug(row) {
|
||||
if (slug.startsWith('/')) {
|
||||
slug = slug.substring(1)
|
||||
}
|
||||
return (slug.match(/\//g) || []).length === 1 && !checkContainHttp(slug) && row.type.indexOf('Menu') < 0
|
||||
return (
|
||||
(slug.match(/\//g) || []).length === 1 &&
|
||||
!checkContainHttp(slug) &&
|
||||
row.type.indexOf('Menu') < 0
|
||||
)
|
||||
}
|
||||
export default PrefixSlug
|
||||
|
||||
@@ -53,7 +53,10 @@ const Slug = props => {
|
||||
|
||||
props = { ...props, lock, setLock, validPassword }
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
@@ -67,14 +70,16 @@ export async function getStaticPaths() {
|
||||
|
||||
const from = 'slug-paths'
|
||||
const { allPages } = await getGlobalData({ from })
|
||||
const paths = allPages?.filter(row => checkSlug(row)).map(row => ({ params: { prefix: row.slug } }))
|
||||
const paths = allPages
|
||||
?.filter(row => checkSlug(row))
|
||||
.map(row => ({ params: { prefix: row.slug } }))
|
||||
return {
|
||||
paths: paths,
|
||||
fallback: true
|
||||
}
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params: { prefix } }) {
|
||||
export async function getStaticProps({ params: { prefix }, locale }) {
|
||||
let fullSlug = prefix
|
||||
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
|
||||
if (!fullSlug.endsWith('.html')) {
|
||||
@@ -82,10 +87,13 @@ export async function getStaticProps({ params: { prefix } }) {
|
||||
}
|
||||
}
|
||||
const from = `slug-props-${fullSlug}`
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
// 在列表内查找文章
|
||||
props.post = props?.allPages?.find(p => {
|
||||
return p.type.indexOf('Menu') < 0 && (p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
return (
|
||||
p.type.indexOf('Menu') < 0 &&
|
||||
(p.slug === fullSlug || p.id === idToUuid(fullSlug))
|
||||
)
|
||||
})
|
||||
|
||||
// 处理非列表内文章的内信息
|
||||
@@ -113,12 +121,18 @@ export async function getStaticProps({ params: { prefix } }) {
|
||||
}
|
||||
|
||||
// 推荐关联文章处理
|
||||
const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
if (allPosts && allPosts.length > 0) {
|
||||
const index = allPosts.indexOf(props.post)
|
||||
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
|
||||
props.next = allPosts.slice(index + 1, index + 2)[0] ?? allPosts[0]
|
||||
props.recommendPosts = getRecommendPost(props.post, allPosts, siteConfig('POST_RECOMMEND_COUNT'))
|
||||
props.recommendPosts = getRecommendPost(
|
||||
props.post,
|
||||
allPosts,
|
||||
siteConfig('POST_RECOMMEND_COUNT')
|
||||
)
|
||||
} else {
|
||||
props.prev = null
|
||||
props.next = null
|
||||
@@ -172,7 +186,11 @@ function checkSlug(row) {
|
||||
if (slug.startsWith('/')) {
|
||||
slug = slug.substring(1)
|
||||
}
|
||||
return (slug.match(/\//g) || []).length === 0 && !checkContainHttp(slug) && row.type.indexOf('Menu') < 0
|
||||
return (
|
||||
(slug.match(/\//g) || []).length === 0 &&
|
||||
!checkContainHttp(slug) &&
|
||||
row.type.indexOf('Menu') < 0
|
||||
)
|
||||
}
|
||||
|
||||
export default Slug
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { useEffect } from 'react'
|
||||
import BLOG from '@/blog.config'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { isBrowser } from '@/lib/utils'
|
||||
import { formatDateFmt } from '@/lib/utils/formatDate'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useEffect } from 'react'
|
||||
|
||||
const ArchiveIndex = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (isBrowser) {
|
||||
@@ -28,10 +31,12 @@ const ArchiveIndex = props => {
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
const props = await getGlobalData({ from: 'archive-index' })
|
||||
export async function getStaticProps({ locale }) {
|
||||
const props = await getGlobalData({ from: 'archive-index', locale })
|
||||
// 处理分页
|
||||
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
props.posts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
delete props.allPages
|
||||
|
||||
const postsSortByDate = Object.create(props.posts)
|
||||
|
||||
@@ -11,19 +11,26 @@ import { useRouter } from 'next/router'
|
||||
*/
|
||||
export default function Category(props) {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params: { category } }) {
|
||||
export async function getStaticProps({ params: { category }, locale }) {
|
||||
const from = 'category-props'
|
||||
let props = await getGlobalData({ from })
|
||||
let props = await getGlobalData({ from, locale })
|
||||
|
||||
// 过滤状态
|
||||
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
props.posts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
// 处理过滤
|
||||
props.posts = props.posts.filter(post => post && post.category && post.category.includes(category))
|
||||
props.posts = props.posts.filter(
|
||||
post => post && post.category && post.category.includes(category)
|
||||
)
|
||||
// 处理文章页数
|
||||
props.postCount = props.posts.length
|
||||
// 处理分页
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import React from 'react'
|
||||
import BLOG from '@/blog.config'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 分类首页
|
||||
@@ -12,13 +11,16 @@ import { siteConfig } from '@/lib/config'
|
||||
*/
|
||||
export default function Category(props) {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
const props = await getGlobalData({ from: 'category-index-props' })
|
||||
export async function getStaticProps({ locale }) {
|
||||
const props = await getGlobalData({ from: 'category-index-props', locale })
|
||||
delete props.allPages
|
||||
return {
|
||||
props,
|
||||
|
||||
@@ -24,9 +24,10 @@ const Index = props => {
|
||||
* SSG 获取数据
|
||||
* @returns
|
||||
*/
|
||||
export async function getStaticProps() {
|
||||
export async function getStaticProps(req) {
|
||||
const { locale } = req
|
||||
const from = 'index'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
|
||||
props.posts = props.allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
|
||||
@@ -11,14 +11,17 @@ import { useRouter } from 'next/router'
|
||||
*/
|
||||
const Page = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticPaths() {
|
||||
export async function getStaticPaths({ locale }) {
|
||||
const from = 'page-paths'
|
||||
const { postCount } = await getGlobalData({ from })
|
||||
const { postCount } = await getGlobalData({ from, locale })
|
||||
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
|
||||
return {
|
||||
// remove first page, we 're not gonna handle that.
|
||||
@@ -33,9 +36,14 @@ export async function getStaticProps({ params: { page } }) {
|
||||
const from = `page-${page}`
|
||||
const props = await getGlobalData({ from })
|
||||
const { allPages } = props
|
||||
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
// 处理分页
|
||||
props.posts = allPosts.slice(siteConfig('POSTS_PER_PAGE') * (page - 1), siteConfig('POSTS_PER_PAGE') * page)
|
||||
props.posts = allPosts.slice(
|
||||
siteConfig('POSTS_PER_PAGE') * (page - 1),
|
||||
siteConfig('POSTS_PER_PAGE') * page
|
||||
)
|
||||
props.page = page
|
||||
|
||||
// 处理预览
|
||||
@@ -45,7 +53,11 @@ export async function getStaticProps({ params: { page } }) {
|
||||
if (post.password && post.password !== '') {
|
||||
continue
|
||||
}
|
||||
post.blockMap = await getPostBlocks(post.id, 'slug', siteConfig('POST_PREVIEW_LINES'))
|
||||
post.blockMap = await getPostBlocks(
|
||||
post.id,
|
||||
'slug',
|
||||
siteConfig('POST_PREVIEW_LINES')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,10 @@ import { useRouter } from 'next/router'
|
||||
|
||||
const Index = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
return <Layout {...props} />
|
||||
}
|
||||
@@ -17,13 +20,15 @@ const Index = props => {
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
export async function getStaticProps({ params: { keyword } }) {
|
||||
export async function getStaticProps({ params: { keyword }, locale }) {
|
||||
const props = await getGlobalData({
|
||||
from: 'search-props',
|
||||
pageType: ['Post']
|
||||
locale
|
||||
})
|
||||
const { allPages } = props
|
||||
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
props.posts = await filterByMemCache(allPosts, keyword)
|
||||
props.postCount = props.posts.length
|
||||
// 处理分页
|
||||
@@ -87,7 +92,8 @@ function getTextContent(textArray) {
|
||||
* @param {*} obj
|
||||
* @returns
|
||||
*/
|
||||
const isIterable = obj => obj != null && typeof obj[Symbol.iterator] === 'function'
|
||||
const isIterable = obj =>
|
||||
obj != null && typeof obj[Symbol.iterator] === 'function'
|
||||
|
||||
/**
|
||||
* 在内存缓存中进行全文索引
|
||||
@@ -103,8 +109,12 @@ async function filterByMemCache(allPosts, keyword) {
|
||||
for (const post of allPosts) {
|
||||
const cacheKey = 'page_block_' + post.id
|
||||
const page = await getDataFromCache(cacheKey, true)
|
||||
const tagContent = post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : ''
|
||||
const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : ''
|
||||
const tagContent =
|
||||
post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : ''
|
||||
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
|
||||
const indexContent = getPageContentText(post, page)
|
||||
|
||||
@@ -8,7 +8,10 @@ import { useRouter } from 'next/router'
|
||||
const Index = props => {
|
||||
const { keyword } = props
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
props = { ...props, currentSearch: keyword }
|
||||
|
||||
return <Layout {...props} />
|
||||
@@ -19,17 +22,23 @@ const Index = props => {
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
export async function getStaticProps({ params: { keyword, page } }) {
|
||||
export async function getStaticProps({ params: { keyword, page }, locale }) {
|
||||
const props = await getGlobalData({
|
||||
from: 'search-props',
|
||||
pageType: ['Post']
|
||||
pageType: ['Post'],
|
||||
locale
|
||||
})
|
||||
const { allPages } = props
|
||||
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
const allPosts = allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
props.posts = await filterByMemCache(allPosts, keyword)
|
||||
props.postCount = props.posts.length
|
||||
// 处理分页
|
||||
props.posts = props.posts.slice(siteConfig('POSTS_PER_PAGE') * (page - 1), siteConfig('POSTS_PER_PAGE') * page)
|
||||
props.posts = props.posts.slice(
|
||||
siteConfig('POSTS_PER_PAGE') * (page - 1),
|
||||
siteConfig('POSTS_PER_PAGE') * page
|
||||
)
|
||||
props.keyword = keyword
|
||||
props.page = page
|
||||
delete props.allPages
|
||||
@@ -87,7 +96,8 @@ function getTextContent(textArray) {
|
||||
* @param {*} obj
|
||||
* @returns
|
||||
*/
|
||||
const isIterable = obj => obj != null && typeof obj[Symbol.iterator] === 'function'
|
||||
const isIterable = obj =>
|
||||
obj != null && typeof obj[Symbol.iterator] === 'function'
|
||||
|
||||
/**
|
||||
* 在内存缓存中进行全文索引
|
||||
@@ -103,8 +113,12 @@ async function filterByMemCache(allPosts, keyword) {
|
||||
for (const post of allPosts) {
|
||||
const cacheKey = 'page_block_' + post.id
|
||||
const page = await getDataFromCache(cacheKey, true)
|
||||
const tagContent = post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : ''
|
||||
const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : ''
|
||||
const tagContent =
|
||||
post?.tags && Array.isArray(post?.tags) ? post?.tags.join(' ') : ''
|
||||
const categoryContent =
|
||||
post.category && Array.isArray(post.category)
|
||||
? post.category.join(' ')
|
||||
: ''
|
||||
const articleInfo = post.title + post.summary + tagContent + categoryContent
|
||||
let hit = articleInfo.indexOf(keyword) > -1
|
||||
let indexContent = [post.summary]
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { useRouter } from 'next/router'
|
||||
import BLOG from '@/blog.config'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 搜索路由
|
||||
@@ -13,7 +13,10 @@ const Search = props => {
|
||||
const { posts } = props
|
||||
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
const router = useRouter()
|
||||
const keyword = router?.query?.s
|
||||
@@ -25,7 +28,7 @@ const Search = props => {
|
||||
const tagContent = post?.tags ? post?.tags.join(' ') : ''
|
||||
const categoryContent = post.category ? post.category.join(' ') : ''
|
||||
const searchContent =
|
||||
post.title + post.summary + tagContent + categoryContent
|
||||
post.title + post.summary + tagContent + categoryContent
|
||||
return searchContent.toLowerCase().includes(keyword.toLowerCase())
|
||||
})
|
||||
} else {
|
||||
@@ -40,13 +43,15 @@ const Search = props => {
|
||||
/**
|
||||
* 浏览器前端搜索
|
||||
*/
|
||||
export async function getStaticProps() {
|
||||
export async function getStaticProps({ locale }) {
|
||||
const props = await getGlobalData({
|
||||
from: 'search-props',
|
||||
pageType: ['Post']
|
||||
locale
|
||||
})
|
||||
const { allPages } = props
|
||||
props.posts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
props.posts = allPages?.filter(
|
||||
page => page.type === 'Post' && page.status === 'Published'
|
||||
)
|
||||
return {
|
||||
props,
|
||||
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 登录
|
||||
@@ -11,13 +11,18 @@ import { siteConfig } from '@/lib/config'
|
||||
*/
|
||||
const SignIn = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
export async function getStaticProps(req) {
|
||||
const { locale } = req
|
||||
|
||||
const from = 'SignIn'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
|
||||
delete props.allPages
|
||||
return {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 注册
|
||||
@@ -11,13 +11,18 @@ import { siteConfig } from '@/lib/config'
|
||||
*/
|
||||
const SignUp = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
export async function getStaticProps(req) {
|
||||
const { locale } = req
|
||||
|
||||
const from = 'SignIn'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
|
||||
delete props.allPages
|
||||
return {
|
||||
|
||||
@@ -11,14 +11,17 @@ import { useRouter } from 'next/router'
|
||||
*/
|
||||
const Tag = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params: { tag } }) {
|
||||
export async function getStaticProps({ params: { tag }, locale }) {
|
||||
const from = 'tag-props'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
|
||||
// 过滤状态
|
||||
props.posts = props.allPages
|
||||
|
||||
@@ -6,13 +6,16 @@ import { useRouter } from 'next/router'
|
||||
|
||||
const Tag = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps({ params: { tag, page } }) {
|
||||
export async function getStaticProps({ params: { tag, page }, locale }) {
|
||||
const from = 'tag-page-props'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
// 过滤状态、标签
|
||||
props.posts = props.allPages
|
||||
?.filter(page => page.type === 'Post' && page.status === 'Published')
|
||||
@@ -20,7 +23,10 @@ export async function getStaticProps({ params: { tag, page } }) {
|
||||
// 处理文章数
|
||||
props.postCount = props.posts.length
|
||||
// 处理分页
|
||||
props.posts = props.posts.slice(siteConfig('POSTS_PER_PAGE') * (page - 1), siteConfig('POSTS_PER_PAGE') * page)
|
||||
props.posts = props.posts.slice(
|
||||
siteConfig('POSTS_PER_PAGE') * (page - 1),
|
||||
siteConfig('POSTS_PER_PAGE') * page
|
||||
)
|
||||
|
||||
props.tag = tag
|
||||
props.page = page
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import BLOG from '@/blog.config'
|
||||
import { useRouter } from 'next/router'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
import { getGlobalData } from '@/lib/db/getSiteData'
|
||||
import { getLayoutByTheme } from '@/themes/theme'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
/**
|
||||
* 标签首页
|
||||
@@ -11,13 +11,18 @@ import { siteConfig } from '@/lib/config'
|
||||
*/
|
||||
const TagIndex = props => {
|
||||
// 根据页面路径加载不同Layout文件
|
||||
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() })
|
||||
const Layout = getLayoutByTheme({
|
||||
theme: siteConfig('THEME'),
|
||||
router: useRouter()
|
||||
})
|
||||
return <Layout {...props} />
|
||||
}
|
||||
|
||||
export async function getStaticProps() {
|
||||
export async function getStaticProps(req) {
|
||||
const { locale } = req
|
||||
|
||||
const from = 'tag-index-props'
|
||||
const props = await getGlobalData({ from })
|
||||
const props = await getGlobalData({ from, locale })
|
||||
delete props.allPages
|
||||
return {
|
||||
props,
|
||||
|
||||
Reference in New Issue
Block a user