forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
177 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables | ||
NEXT_PUBLIC_VERSION=4.0.16 | ||
NEXT_PUBLIC_VERSION=4.0.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import BLOG from '@/blog.config' | ||
import { getPostBlocks } from '@/lib/notion' | ||
import { getGlobalData } from '@/lib/notion/getNotionData' | ||
import { idToUuid } from 'notion-utils' | ||
import { getNotion } from '@/lib/notion/getNotion' | ||
import Slug, { getRecommendPost } from '..' | ||
import { uploadDataToAlgolia } from '@/lib/algolia' | ||
|
||
/** | ||
* 根据notion的slug访问页面 | ||
* 解析三级以上目录 /article/2023/10/29/test | ||
* @param {*} props | ||
* @returns | ||
*/ | ||
const PrefixSlug = props => { | ||
return <Slug {...props}/> | ||
} | ||
|
||
/** | ||
* 编译渲染页面路径 | ||
* @returns | ||
*/ | ||
export async function getStaticPaths() { | ||
if (!BLOG.isProd) { | ||
return { | ||
paths: [], | ||
fallback: true | ||
} | ||
} | ||
|
||
const from = 'slug-paths' | ||
const { allPages } = await getGlobalData({ from }) | ||
return { | ||
paths: allPages?.filter(row => hasMultipleSlashes(row.slug) && row.type.indexOf('Menu') < 0).map(row => ({ params: { prefix: row.slug.split('/')[0], slug: row.slug.split('/')[1], suffix: row.slug.split('/').slice(1) } })), | ||
fallback: true | ||
} | ||
} | ||
|
||
/** | ||
* 抓取页面数据 | ||
* @param {*} param0 | ||
* @returns | ||
*/ | ||
export async function getStaticProps({ params: { prefix, slug, suffix } }) { | ||
let fullSlug = prefix + '/' + slug + '/' + suffix.join('/') | ||
if (JSON.parse(BLOG.PSEUDO_STATIC)) { | ||
if (!fullSlug.endsWith('.html')) { | ||
fullSlug += '.html' | ||
} | ||
} | ||
const from = `slug-props-${fullSlug}` | ||
const props = await getGlobalData({ from }) | ||
// 在列表内查找文章 | ||
props.post = props?.allPages?.find((p) => { | ||
return p.slug === fullSlug || p.id === idToUuid(fullSlug) | ||
}) | ||
|
||
// 处理非列表内文章的内信息 | ||
if (!props?.post) { | ||
const pageId = fullSlug.slice(-1)[0] | ||
if (pageId.length >= 32) { | ||
const post = await getNotion(pageId) | ||
props.post = post | ||
} | ||
} | ||
|
||
// 无法获取文章 | ||
if (!props?.post) { | ||
props.post = null | ||
return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } | ||
} | ||
|
||
// 文章内容加载 | ||
if (!props?.posts?.blockMap) { | ||
props.post.blockMap = await getPostBlocks(props.post.id, from) | ||
} | ||
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA) | ||
if (BLOG.ALGOLIA_APP_ID) { | ||
uploadDataToAlgolia(props?.post) | ||
} | ||
|
||
// 推荐关联文章处理 | ||
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, BLOG.POST_RECOMMEND_COUNT) | ||
} else { | ||
props.prev = null | ||
props.next = null | ||
props.recommendPosts = [] | ||
} | ||
|
||
delete props.allPages | ||
return { | ||
props, | ||
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) | ||
} | ||
} | ||
|
||
/** | ||
* 判断是否包含两个以上的 / | ||
* @param {*} str | ||
* @returns | ||
*/ | ||
function hasMultipleSlashes(str) { | ||
const regex = /\/+/g; // 创建正则表达式,匹配所有的斜杠符号 | ||
const matches = str.match(regex); // 在字符串中找到所有匹配的斜杠符号 | ||
return matches && matches.length >= 2; // 判断匹配的斜杠符号数量是否大于等于2 | ||
} | ||
|
||
export default PrefixSlug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.