From 14394b596d1933069b02dced14d757b3ad1825d8 Mon Sep 17 00:00:00 2001 From: wanglu Date: Sun, 26 Feb 2023 16:37:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E8=B7=AF=E5=BE=84=20#159?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/NewArticleModal/index.jsx | 8 ++ .../components/PublishDraftModal/index.jsx | 8 ++ .../src/components/UpdateModal/index.tsx | 8 ++ packages/admin/src/pages/Article/columns.jsx | 5 +- packages/admin/src/pages/Editor/index.jsx | 5 +- .../src/services/van-blog/getPathname.ts | 6 ++ .../admin/article/article.controller.ts | 4 +- .../controller/public/public.controller.ts | 15 ++-- .../src/provider/article/article.provider.ts | 79 +++++++++++++++++-- .../server/src/provider/isr/isr.provider.ts | 14 +++- .../server/src/provider/meta/meta.provider.ts | 4 +- .../server/src/provider/rss/rss.provider.ts | 2 +- .../src/provider/sitemap/sitemap.provider.ts | 2 +- .../src/provider/visit/visit.provider.ts | 2 +- packages/server/src/scheme/article.schema.ts | 3 + packages/server/src/types/article.dto.ts | 2 + packages/server/src/types/draft.dto.ts | 1 + packages/website/api/getArticleViewer.ts | 2 +- packages/website/api/getArticles.ts | 12 +-- .../website/components/ArticleList/index.tsx | 5 +- .../website/components/CopyRight/index.tsx | 6 +- .../website/components/PostCard/bottom.tsx | 13 +-- .../website/components/PostCard/index.tsx | 6 +- .../website/components/PostCard/title.tsx | 4 +- .../website/components/PostViewer/index.tsx | 5 +- packages/website/components/Reward/index.tsx | 2 +- .../website/components/UnLockCard/index.tsx | 6 +- packages/website/next.config.js | 6 +- packages/website/pages/index.tsx | 3 +- packages/website/pages/page/[p].tsx | 3 +- packages/website/pages/post/[id].tsx | 7 +- packages/website/types/article.ts | 1 + packages/website/utils/getArticlePath.ts | 8 ++ packages/website/utils/getPageProps.ts | 4 +- 34 files changed, 198 insertions(+), 63 deletions(-) create mode 100644 packages/admin/src/services/van-blog/getPathname.ts create mode 100644 packages/website/utils/getArticlePath.ts diff --git a/packages/admin/src/components/NewArticleModal/index.jsx b/packages/admin/src/components/NewArticleModal/index.jsx index 4a8f0374f..87736d274 100644 --- a/packages/admin/src/components/NewArticleModal/index.jsx +++ b/packages/admin/src/components/NewArticleModal/index.jsx @@ -63,6 +63,14 @@ export default function (props) { label="置顶优先级" placeholder="留空或0表示不置顶,其余数字越大表示优先级越高" /> + + + , { if (record?.hidden) { Modal.confirm({ @@ -158,7 +159,7 @@ export const columns = [ ), onOk: () => { - window.open(`/post/${record.id}`, '_blank'); + window.open(`/post/${getPathname(record)}`, '_blank'); return true; }, okText: '仍然访问', diff --git a/packages/admin/src/pages/Editor/index.jsx b/packages/admin/src/pages/Editor/index.jsx index 7b0b00bc9..b3dba3ffc 100644 --- a/packages/admin/src/pages/Editor/index.jsx +++ b/packages/admin/src/pages/Editor/index.jsx @@ -13,6 +13,7 @@ import { updateArticle, updateDraft, } from '@/services/van-blog/api'; +import { getPathname } from '@/services/van-blog/getPathname'; import { parseMarkdownFile, parseObjToMarkdown } from '@/services/van-blog/parseMarkdownFile'; import { useCacheState } from '@/services/van-blog/useCacheState'; import { DownOutlined } from '@ant-design/icons'; @@ -301,7 +302,7 @@ export default function () { ), onOk: () => { - window.open(`/post/${currObj.id}`, '_blank'); + window.open(`/post/${getPathname(currObj)}`, '_blank'); return true; }, okText: '仍然访问', @@ -309,7 +310,7 @@ export default function () { }); return; } - url = `/post/${currObj?.id}`; + url = `/post/${getPathname(currObj)}`; } else { url = '/about'; } diff --git a/packages/admin/src/services/van-blog/getPathname.ts b/packages/admin/src/services/van-blog/getPathname.ts new file mode 100644 index 000000000..418d23581 --- /dev/null +++ b/packages/admin/src/services/van-blog/getPathname.ts @@ -0,0 +1,6 @@ +export const getPathname = (obj: any) => { + if (!obj.pathname) { + return obj.id; + } + return obj.pathname; +}; diff --git a/packages/server/src/controller/admin/article/article.controller.ts b/packages/server/src/controller/admin/article/article.controller.ts index 96277c886..35fcf991f 100644 --- a/packages/server/src/controller/admin/article/article.controller.ts +++ b/packages/server/src/controller/admin/article/article.controller.ts @@ -67,8 +67,8 @@ export class ArticleController { } @Get('/:id') - async getOneById(@Param('id') id: number) { - const data = await this.articleProvider.getById(id, 'admin'); + async getOneByIdOrPathname(@Param('id') id: string) { + const data = await this.articleProvider.getByIdOrPathname(id, 'admin'); return { statusCode: 200, data, diff --git a/packages/server/src/controller/public/public.controller.ts b/packages/server/src/controller/public/public.controller.ts index ab4b1f3b0..2e50ec8d4 100644 --- a/packages/server/src/controller/public/public.controller.ts +++ b/packages/server/src/controller/public/public.controller.ts @@ -44,16 +44,19 @@ export class PublicController { }; } @Get('/article/:id') - async getArticleById(@Param('id') id: number) { - const data = await this.articleProvider.getByIdWithPreNext(id, 'public'); + async getArticleByIdOrPathname(@Param('id') id: string) { + const data = await this.articleProvider.getByIdOrPathnameWithPreNext( + id, + 'public', + ); return { statusCode: 200, data: data, }; } @Post('/article/:id') - async getArticleByIdWithPassword( - @Param('id') id: number, + async getArticleByIdOrPathnameWithPassword( + @Param('id') id: number | string, @Body() body: { password: string }, ) { const data = await this.articleProvider.getByIdWithPassword( @@ -91,7 +94,7 @@ export class PublicController { } const data = await this.metaProvider.addViewer( isNew, - url.pathname, + decodeURIComponent(url.pathname), isNewByPath, ); return { @@ -109,7 +112,7 @@ export class PublicController { }; } @Get('/article/viewer/:id') - async getViewerByArticleId(@Param('id') id: number) { + async getViewerByArticleIdOrPathname(@Param('id') id: number | string) { const data = await this.visitProvider.getByArticleId(id); return { statusCode: 200, diff --git a/packages/server/src/provider/article/article.provider.ts b/packages/server/src/provider/article/article.provider.ts index 632331a6f..573f1f2d3 100644 --- a/packages/server/src/provider/article/article.provider.ts +++ b/packages/server/src/provider/article/article.provider.ts @@ -49,6 +49,7 @@ export class ArticleProvider { hidden: 1, author: 1, copyright: 1, + pathname: 1, }; adminView = { @@ -69,6 +70,7 @@ export class ArticleProvider { visited: 1, author: 1, copyright: 1, + pathname: 1, }; listView = { @@ -87,6 +89,7 @@ export class ArticleProvider { visited: 1, author: 1, copyright: 1, + pathname: 1, }; toPublic(oldArticles: Article[]) { @@ -157,6 +160,26 @@ export class ArticleProvider { return res; } + async updateViewerByPathname(pathname: string, isNew: boolean) { + let article = await this.getByPathName(pathname, 'list'); + if (!article) { + // 这是通过 id 的吧。 + article = await this.getById(Number(pathname), 'list'); + if (!article) { + return; + } + } + const oldViewer = article.viewer || 0; + const oldVIsited = article.visited || 0; + const newViewer = oldViewer + 1; + const newVisited = isNew ? oldVIsited + 1 : oldVIsited; + const nowTime = new Date(); + await this.articleModel.updateOne( + { id: article.id }, + { visited: newVisited, viewer: newViewer, lastVisitedTime: nowTime }, + ); + } + async updateViewer(id: number, isNew: boolean) { const article = await this.getById(id, 'list'); if (!article) { @@ -663,6 +686,43 @@ export class ArticleProvider { return resData; } + async getByIdOrPathname(id: string | number, view: ArticleView) { + const articleByPathname = await this.getByPathName( + String(id), + this.getView(view), + ); + + if (articleByPathname) { + return articleByPathname; + } + return await this.getById(Number(id), this.getView(view)); + } + + async getByPathName(pathname: string, view: ArticleView): Promise
{ + const $and: any = [ + { + $or: [ + { + deleted: false, + }, + { + deleted: { $exists: false }, + }, + ], + }, + ]; + + return await this.articleModel + .findOne( + { + pathname: decodeURIComponent(pathname), + $and, + }, + this.getView(view), + ) + .exec(); + } + async getById(id: number, view: ArticleView): Promise
{ const $and: any = [ { @@ -687,17 +747,22 @@ export class ArticleProvider { ) .exec(); } - async getByIdWithPassword(id: number, password: string): Promise { - const article: any = await this.getById(id, 'admin'); + async getByIdWithPassword( + id: number | string, + password: string, + ): Promise { + const article: any = await this.getByIdOrPathname(id, 'admin'); if (!password) { return null; } if (!article) { return null; } - const category = await this.categoryModal.findOne({ - name: article.category, - }); + const category = + (await this.categoryModal.findOne({ + name: article.category, + })) || ({} as any); + const categoryPassword = category.private ? category.password : undefined; const targetPassword = categoryPassword ? categoryPassword @@ -712,8 +777,8 @@ export class ArticleProvider { } } } - async getByIdWithPreNext(id: number, view: ArticleView) { - const curArticle = await this.getById(id, view); + async getByIdOrPathnameWithPreNext(id: string | number, view: ArticleView) { + const curArticle = await this.getByIdOrPathname(id, view); if (!curArticle) { throw new NotFoundException('找不到文章'); } diff --git a/packages/server/src/provider/isr/isr.provider.ts b/packages/server/src/provider/isr/isr.provider.ts index 5a9096bbd..bdd0a3976 100644 --- a/packages/server/src/provider/isr/isr.provider.ts +++ b/packages/server/src/provider/isr/isr.provider.ts @@ -36,7 +36,15 @@ export class ISRProvider { // ! 配置差的机器可能并发多了会卡,所以改成串行的。 await this.activeUrls(this.urlList, false); - await this.activePath('post', activeConfig?.postId || undefined); + let postId: any = null; + const articleWithThisId = await this.articleProvider.getById( + postId, + 'list', + ); + if (articleWithThisId) { + postId = articleWithThisId.pathname || articleWithThisId.id; + } + await this.activePath('post', postId || undefined); await this.activePath('page'); await this.activePath('category'); await this.activePath('tag'); @@ -135,7 +143,7 @@ export class ISRProvider { beforeObj?: Article, ) { const { article, pre, next } = - await this.articleProvider.getByIdWithPreNext(id, 'list'); + await this.articleProvider.getByIdOrPathnameWithPreNext(id, 'list'); // 无论是什么事件都先触发文章本身、标签和分类。 this.activeUrl(`/post/${id}`, true); if (pre) { @@ -208,7 +216,7 @@ export class ISRProvider { async getArticleUrls() { const articles = await this.articleProvider.getAll('list', true, true); return articles.map((a) => { - return `/post/${a.id}`; + return `/post/${a.pathname || a.id}`; }); } } diff --git a/packages/server/src/provider/meta/meta.provider.ts b/packages/server/src/provider/meta/meta.provider.ts index 2682e11e4..fd89893eb 100644 --- a/packages/server/src/provider/meta/meta.provider.ts +++ b/packages/server/src/provider/meta/meta.provider.ts @@ -65,8 +65,8 @@ export class MetaProvider { const r = /\/post\//; const isArticlePath = r.test(pathname); if (isArticlePath) { - await this.articleProvider.updateViewer( - parseInt(pathname.replace('/post/', '')), + await this.articleProvider.updateViewerByPathname( + pathname.replace('/post/', ''), isNewByPath, ); } diff --git a/packages/server/src/provider/rss/rss.provider.ts b/packages/server/src/provider/rss/rss.provider.ts index 740258e4c..51238e9c3 100644 --- a/packages/server/src/provider/rss/rss.provider.ts +++ b/packages/server/src/provider/rss/rss.provider.ts @@ -87,7 +87,7 @@ export class RssProvider { author, }); for (const article of articles) { - const url = `${siteUrl}post/${article.id}`; + const url = `${siteUrl}post/${article.pathname || article.id}`; const category = { name: article.category, domain: `${siteUrl}/category/${article.category}`, diff --git a/packages/server/src/provider/sitemap/sitemap.provider.ts b/packages/server/src/provider/sitemap/sitemap.provider.ts index c76d3d185..0dd49e18d 100644 --- a/packages/server/src/provider/sitemap/sitemap.provider.ts +++ b/packages/server/src/provider/sitemap/sitemap.provider.ts @@ -53,7 +53,7 @@ export class SiteMapProvider { async getArticleUrls() { const articles = await this.articleProvider.getAll('list', false, false); return articles.map((a) => { - return `/post/${a.id}`; + return `/post/${a.pathname || a.id}`; }); } async getCategoryUrls() { diff --git a/packages/server/src/provider/visit/visit.provider.ts b/packages/server/src/provider/visit/visit.provider.ts index b9b2e937e..b6624071d 100644 --- a/packages/server/src/provider/visit/visit.provider.ts +++ b/packages/server/src/provider/visit/visit.provider.ts @@ -78,7 +78,7 @@ export class VisitProvider { async findByDateAndPath(date: string, pathname: string): Promise { return this.visitModel.findOne({ date, pathname }).exec(); } - async getByArticleId(id: number) { + async getByArticleId(id: number | string) { const pathname = id == 0 ? `/about` : `/post/${id}`; const result = await this.visitModel .find({ diff --git a/packages/server/src/scheme/article.schema.ts b/packages/server/src/scheme/article.schema.ts index 04342b1a0..b34597c60 100644 --- a/packages/server/src/scheme/article.schema.ts +++ b/packages/server/src/scheme/article.schema.ts @@ -29,6 +29,9 @@ export class Article extends Document { @Prop({ index: true }) author: string; + @Prop({ default: '', index: true }) + pathname: string; + @Prop({ default: false, index: true }) private: boolean; diff --git a/packages/server/src/types/article.dto.ts b/packages/server/src/types/article.dto.ts index 4816bf79a..7e051229a 100644 --- a/packages/server/src/types/article.dto.ts +++ b/packages/server/src/types/article.dto.ts @@ -13,6 +13,7 @@ export class CreateArticleDto { createdAt?: Date; author?: string; copyright?: string; + pathname?: string; } export class UpdateArticleDto { title?: string; @@ -29,6 +30,7 @@ export class UpdateArticleDto { updatedAt?: Date; author?: string; copyright?: string; + pathname?: string; } export class SearchArticleOption { page: number; diff --git a/packages/server/src/types/draft.dto.ts b/packages/server/src/types/draft.dto.ts index e9666a0d4..42c52434a 100644 --- a/packages/server/src/types/draft.dto.ts +++ b/packages/server/src/types/draft.dto.ts @@ -19,6 +19,7 @@ export class UpdateDraftDto { } export class PublishDraftDto { hidden?: boolean; + pathname?: string; private?: boolean; password?: string; copyright?: string; diff --git a/packages/website/api/getArticleViewer.ts b/packages/website/api/getArticleViewer.ts index 84b2182c2..cc9a2d4d2 100644 --- a/packages/website/api/getArticleViewer.ts +++ b/packages/website/api/getArticleViewer.ts @@ -1,4 +1,4 @@ -export const getArticleViewer = async (id: number) => { +export const getArticleViewer = async (id: number | string) => { try { const url = `/api/public/article/viewer/${id}`; const res = await fetch(url); diff --git a/packages/website/api/getArticles.ts b/packages/website/api/getArticles.ts index 98baeac54..7c039a68a 100644 --- a/packages/website/api/getArticles.ts +++ b/packages/website/api/getArticles.ts @@ -20,7 +20,7 @@ export const getArticlesByOption = async ( queryString += `${k}=${v}&`; } queryString = queryString.substring(0, queryString.length - 1); - queryString = encodeQuerystring(queryString) + queryString = encodeQuerystring(queryString); try { const url = `${config.baseUrl}api/public/article?${queryString}`; const res = await fetch(url); @@ -86,7 +86,7 @@ export const getArticlesByTag = async (tagName: string) => { } } }; -export const getArticleById = async (id: number) => { +export const getArticleByIdOrPathname = async (id: string) => { try { const url = `${config.baseUrl}api/public/article/${id}`; const res = await fetch(url); @@ -94,10 +94,10 @@ export const getArticleById = async (id: number) => { const { article, pre, next } = data; const r: any = { article }; if (pre) { - r.pre = { title: pre.title, id: pre.id }; + r.pre = { title: pre.title, id: pre.id, pathname: pre.pathname }; } if (next) { - r.next = { title: next.title, id: next.id }; + r.next = { title: next.title, id: next.id, pathname: next.pathname }; } return r; } catch (err) { @@ -110,8 +110,8 @@ export const getArticleById = async (id: number) => { } } }; -export const getArticleByIdWithPassword = async ( - id: number, +export const getArticleByIdOrPathnameWithPassword = async ( + id: number | string, password: string ) => { try { diff --git a/packages/website/components/ArticleList/index.tsx b/packages/website/components/ArticleList/index.tsx index 990c1fe07..af24e96ef 100644 --- a/packages/website/components/ArticleList/index.tsx +++ b/packages/website/components/ArticleList/index.tsx @@ -2,6 +2,7 @@ import { Article } from "../../types/article"; import dayjs from "dayjs"; import Link from "../Link"; import { getTarget } from "../Link/tools"; +import { getArticlePath } from "../../utils/getArticlePath"; export default function (props: { articles: Article[]; showYear?: boolean; @@ -13,7 +14,7 @@ export default function (props: { {props.articles.map((article, index) => { return ( @@ -21,7 +22,7 @@ export default function (props: { className="dark:border-dark-2 dark:hover:border-nav-dark-light flex items-center border-b pb-1 border-dashed cursor-pointer group border-gray-200 hover:border-gray-400 " key={article.id} target={getTarget(props.openArticleLinksInNewWindow)} - href={`/post/${article.id}`} + href={`/post/${getArticlePath(article)}`} >
{props.showYear diff --git a/packages/website/components/CopyRight/index.tsx b/packages/website/components/CopyRight/index.tsx index 84b721ffc..f6df0d798 100644 --- a/packages/website/components/CopyRight/index.tsx +++ b/packages/website/components/CopyRight/index.tsx @@ -4,7 +4,7 @@ import toast from "react-hot-toast"; export default function (props: { author: string; - id: number; + id: number | string; showDonate: boolean; copyrightAggreement: string; customCopyRight: string | null; @@ -33,7 +33,7 @@ export default function (props: {

本文链接: { toast.success("复制成功!", { className: "toast", @@ -44,7 +44,7 @@ export default function (props: { className="cursor-pointer border-b border-gray-100 hover:border-gray-500 dark:text-dark dark-border-hover dark:border-nav-dark" style={{ wordBreak: "break-all" }} > - {url} + {decodeURIComponent(url)}

diff --git a/packages/website/components/PostCard/bottom.tsx b/packages/website/components/PostCard/bottom.tsx index c083f5eef..177732a97 100644 --- a/packages/website/components/PostCard/bottom.tsx +++ b/packages/website/components/PostCard/bottom.tsx @@ -2,13 +2,14 @@ import Link from "../Link"; import { useMemo } from "react"; import { encodeQuerystring } from "../../utils/encode"; import { getTarget } from "../Link/tools"; +import { getArticlePath } from "../../utils/getArticlePath"; export function PostBottom(props: { type: "overview" | "article" | "about"; lock: boolean; tags?: string[]; - next?: { id: number; title: string }; - pre?: { id: number; title: string }; + next?: { id: number; title: string; pathname?: string }; + pre?: { id: number; title: string; pathname?: string }; openArticleLinksInNewWindow: boolean; }) { const show = useMemo(() => { @@ -42,12 +43,12 @@ export function PostBottom(props: {
{props.pre?.id && ( {`< ${props.pre?.title}`} @@ -57,12 +58,12 @@ export function PostBottom(props: {
{props.next?.id && ( {`${props.next?.title} >`} diff --git a/packages/website/components/PostCard/index.tsx b/packages/website/components/PostCard/index.tsx index 9b89a2f30..c8eb8fe9c 100644 --- a/packages/website/components/PostCard/index.tsx +++ b/packages/website/components/PostCard/index.tsx @@ -14,7 +14,7 @@ import TocMobile from "../TocMobile"; import { hasToc } from "../../utils/hasToc"; export default function (props: { - id: number; + id: number | string; title: string; updatedAt: Date; createdAt: Date; @@ -26,8 +26,8 @@ export default function (props: { payDark?: string[]; author?: string; tags?: string[]; - next?: { id: number; title: string }; - pre?: { id: number; title: string }; + next?: { id: number; title: string; pathname?: string }; + pre?: { id: number; title: string; pathname?: string }; enableComment: "true" | "false"; top: number; private: boolean; diff --git a/packages/website/components/PostCard/title.tsx b/packages/website/components/PostCard/title.tsx index 1a888b1b8..a658d530f 100644 --- a/packages/website/components/PostCard/title.tsx +++ b/packages/website/components/PostCard/title.tsx @@ -7,7 +7,7 @@ import { getTarget } from "../Link/tools"; export function Title(props: { type: "article" | "about" | "overview"; - id: number; + id: number | string; title: string; openArticleLinksInNewWindow: boolean; }) { @@ -47,7 +47,7 @@ export function SubTitle(props: { createdAt: Date; catelog: string; enableComment: "true" | "false"; - id: number; + id: number | string; openArticleLinksInNewWindow: boolean; }) { const iconSize = "16"; diff --git a/packages/website/components/PostViewer/index.tsx b/packages/website/components/PostViewer/index.tsx index c2ec6e3f1..4d17f4ed7 100644 --- a/packages/website/components/PostViewer/index.tsx +++ b/packages/website/components/PostViewer/index.tsx @@ -1,7 +1,10 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { getArticleViewer } from "../../api/getArticleViewer"; -export default function (props: { shouldAddViewer: boolean; id: number }) { +export default function (props: { + shouldAddViewer: boolean; + id: number | string; +}) { const [viewer, setViewer] = useState(0); const { current } = useRef({ hasInit: false }); const fetchViewer = useCallback(async () => { diff --git a/packages/website/components/Reward/index.tsx b/packages/website/components/Reward/index.tsx index b862fe551..e4cb5a819 100644 --- a/packages/website/components/Reward/index.tsx +++ b/packages/website/components/Reward/index.tsx @@ -8,7 +8,7 @@ export default function (props: { aliPayDark: string; weChatPayDark: string; author: string; - id: number; + id: number | string; }) { const [show, setShow] = useState(false); const { theme } = useContext(ThemeContext); diff --git a/packages/website/components/UnLockCard/index.tsx b/packages/website/components/UnLockCard/index.tsx index ba4cff9e3..cc8adf957 100644 --- a/packages/website/components/UnLockCard/index.tsx +++ b/packages/website/components/UnLockCard/index.tsx @@ -1,10 +1,10 @@ import { useState } from "react"; -import { getArticleByIdWithPassword } from "../../api/getArticles"; +import { getArticleByIdOrPathnameWithPassword } from "../../api/getArticles"; import toast from "react-hot-toast"; import Loading from "../Loading"; export default function (props: { - id: number; + id: number | string; setLock: (l: boolean) => void; setContent: (s: string) => void; }) { @@ -23,7 +23,7 @@ export default function (props: { }; const fetchArticle = async () => { try { - const res = await getArticleByIdWithPassword(props.id, value); + const res = await getArticleByIdOrPathnameWithPassword(props.id, value); if (!res) { onError("密码错误!请重试!"); return false; diff --git a/packages/website/next.config.js b/packages/website/next.config.js index 86bdcf989..2d5ef24fe 100644 --- a/packages/website/next.config.js +++ b/packages/website/next.config.js @@ -2,7 +2,7 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({ enabled: process.env.ANALYZE === "true", }); - +const isDev = process.env.NODE_ENV == "development"; const rewites = process.env.NODE_ENV == "development" ? { @@ -27,11 +27,13 @@ const getAllowDomains = () => { const arr = domainsInEnv.split(","); return arr; } else { + if (isDev) { + return ["pic.mereith.com"]; + } return []; } }; const getCdnUrl = () => { - const isDev = process.env.NODE_ENV == "development"; if (isDev) { return {}; } diff --git a/packages/website/pages/index.tsx b/packages/website/pages/index.tsx index a31c6d630..460b26d04 100644 --- a/packages/website/pages/index.tsx +++ b/packages/website/pages/index.tsx @@ -9,6 +9,7 @@ import { revalidate } from "../utils/loadConfig"; import Waline from "../components/WaLine"; import Head from "next/head"; import { getArticlesKeyWord } from "../utils/keywords"; +import { getArticlePath } from "../utils/getArticlePath"; export interface IndexPageProps { layoutProps: LayoutProps; authorCardProps: AuthorCardProps; @@ -41,7 +42,7 @@ const Home = (props: IndexPageProps) => { customCopyRight={null} private={article.private} top={article.top || 0} - id={article.id} + id={getArticlePath(article)} key={article.id} title={article.title} updatedAt={new Date(article.updatedAt)} diff --git a/packages/website/pages/page/[p].tsx b/packages/website/pages/page/[p].tsx index 64be40f3b..bcbf87e77 100644 --- a/packages/website/pages/page/[p].tsx +++ b/packages/website/pages/page/[p].tsx @@ -6,6 +6,7 @@ import PageNav from "../../components/PageNav"; import PostCard from "../../components/PostCard"; import Waline from "../../components/WaLine"; import { Article } from "../../types/article"; +import { getArticlePath } from "../../utils/getArticlePath"; import { LayoutProps } from "../../utils/getLayoutProps"; import { getPagePagesProps } from "../../utils/getPageProps"; import { getArticlesKeyWord } from "../../utils/keywords"; @@ -46,7 +47,7 @@ const PagePages = (props: PagePagesProps) => { } customCopyRight={null} top={article.top || 0} - id={article.id} + id={getArticlePath(article)} key={article.id} title={article.title} updatedAt={new Date(article.updatedAt)} diff --git a/packages/website/pages/post/[id].tsx b/packages/website/pages/post/[id].tsx index 41b9404c3..7396e9753 100644 --- a/packages/website/pages/post/[id].tsx +++ b/packages/website/pages/post/[id].tsx @@ -5,6 +5,7 @@ import Layout from "../../components/Layout"; import PostCard from "../../components/PostCard"; import Toc from "../../components/Toc"; import { Article } from "../../types/article"; +import { getArticlePath } from "../../utils/getArticlePath"; import { LayoutProps } from "../../utils/getLayoutProps"; import { getPostPagesProps } from "../../utils/getPageProps"; import { hasToc } from "../../utils/hasToc"; @@ -21,10 +22,12 @@ export interface PostPagesProps { pre: { id: number; title: string; + pathname?: string; }; next: { id: number; title: string; + pathname?: string; }; showSubMenu: "true" | "false"; } @@ -59,7 +62,7 @@ const PostPages = (props: PostPagesProps) => { } customCopyRight={props.article.copyright || null} top={props.article.top || 0} - id={props.article.id} + id={getArticlePath(props.article)} key={props.article.title} title={props.article.title} updatedAt={new Date(props.article.updatedAt)} @@ -93,7 +96,7 @@ export async function getStaticPaths() { }); const paths = data.articles.map((article) => ({ params: { - id: String(article.id), + id: String(getArticlePath(article)), }, })); return { diff --git a/packages/website/types/article.ts b/packages/website/types/article.ts index 3a5e9155a..9e249fbc8 100644 --- a/packages/website/types/article.ts +++ b/packages/website/types/article.ts @@ -10,4 +10,5 @@ export interface Article { private: boolean; author?: string; copyright?: string; + pathname?: string; } diff --git a/packages/website/utils/getArticlePath.ts b/packages/website/utils/getArticlePath.ts new file mode 100644 index 000000000..b1edb739d --- /dev/null +++ b/packages/website/utils/getArticlePath.ts @@ -0,0 +1,8 @@ +import { Article } from "../types/article"; + +export const getArticlePath = ( + article: Article | { id: number; pathname?: string } +) => { + const { id, pathname } = article; + return `${pathname ? pathname : id}`; +}; diff --git a/packages/website/utils/getPageProps.ts b/packages/website/utils/getPageProps.ts index 278891486..6ea081301 100644 --- a/packages/website/utils/getPageProps.ts +++ b/packages/website/utils/getPageProps.ts @@ -11,7 +11,7 @@ import { PostPagesProps } from "../pages/post/[id]"; import { PagePagesProps } from "../pages/page/[p]"; import { CategoryPagesProps } from "../pages/category/[category]"; import { - getArticleById, + getArticleByIdOrPathname, getArticlesByCategory, getArticlesByOption, getArticlesByTimeLine, @@ -167,7 +167,7 @@ export async function getPostPagesProps( data.meta.siteInfo?.payWechatDark || "", ], }; - const currArticleProps = await getArticleById(parseInt(curId)); + const currArticleProps = await getArticleByIdOrPathname(curId); const { article } = currArticleProps; const author = article?.author || data.meta.siteInfo.author; return {