From de27479242b3a0a07db3ce73753a91506c5a448e Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Thu, 18 Jul 2024 21:02:37 +0800 Subject: [PATCH] feat(post) add header, fix style --- frontend/.gitignore | 1 + frontend/app/blog/fetch_posts_list.ts | 3 +- frontend/app/blog/layout.tsx | 3 +- frontend/app/blog/page.tsx | 2 +- frontend/app/blog/post/[id]/Headers.tsx | 80 ++++++++++++++++++++ frontend/app/blog/post/[id]/PostCard.tsx | 95 ++++++++++++++++++++++++ frontend/app/blog/post/[id]/generate.tsx | 49 ------------ frontend/app/blog/post/[id]/page.tsx | 7 +- frontend/app/layout.tsx | 4 +- frontend/components/Navbar.tsx | 2 +- frontend/next.config.mjs | 4 + frontend/package.json | 3 +- 12 files changed, 194 insertions(+), 59 deletions(-) create mode 100644 frontend/app/blog/post/[id]/Headers.tsx create mode 100644 frontend/app/blog/post/[id]/PostCard.tsx delete mode 100644 frontend/app/blog/post/[id]/generate.tsx diff --git a/frontend/.gitignore b/frontend/.gitignore index bd9b839..b556ad2 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -40,3 +40,4 @@ bun.lockb # ide related .idea +/package-lock.json diff --git a/frontend/app/blog/fetch_posts_list.ts b/frontend/app/blog/fetch_posts_list.ts index 6e9303f..855b2cc 100644 --- a/frontend/app/blog/fetch_posts_list.ts +++ b/frontend/app/blog/fetch_posts_list.ts @@ -1,7 +1,8 @@ import axios from 'axios'; export default async function fetch_posts_list(): Promise { - const response = await axios(`${ process.env.BACKEND_URL }/get_post_list`); + const request_url = `${ process.env.BACKEND_URL }/get_post_list`; + const response = await axios(request_url); if (response.status !== 200) { throw new Error(response.statusText); } diff --git a/frontend/app/blog/layout.tsx b/frontend/app/blog/layout.tsx index 612e10f..6ec814e 100644 --- a/frontend/app/blog/layout.tsx +++ b/frontend/app/blog/layout.tsx @@ -6,7 +6,8 @@ function Layout({ children: React.ReactNode; }) { return ( -
+ //
+
{ children }
); diff --git a/frontend/app/blog/page.tsx b/frontend/app/blog/page.tsx index 5e14605..3f37e6f 100644 --- a/frontend/app/blog/page.tsx +++ b/frontend/app/blog/page.tsx @@ -29,7 +29,7 @@ async function Blog() { return ( - Fatal: Failed to load post list. + Fatal: { e.toString() } ); } diff --git a/frontend/app/blog/post/[id]/Headers.tsx b/frontend/app/blog/post/[id]/Headers.tsx new file mode 100644 index 0000000..6bba4bf --- /dev/null +++ b/frontend/app/blog/post/[id]/Headers.tsx @@ -0,0 +1,80 @@ +import React from 'react'; +import Image from 'next/image'; +import { Chip } from '@nextui-org/react'; +import { MdCalendarMonth } from 'react-icons/md'; + +type Props = { + post: any +}; + + +const Headers = (props: Props) => { + let post = props.post; + let title = (post as any)['title']; + let sub_title = (post as any)['sub_title']; + // let summary = (post as any)['summary']; + let category = (post as any)['category']; + let tags = (post as any)['tags']; + let last_update = new Date((post as any)['last_update']); + let first_update = new Date((post as any)['first_update']); + let post_id = (post as any)['post_id']; + let header_img = (post as any)['header_img']; + + return ( + <> + { header_img !== '' &&
+ { + { +
} + +
+
+ { category } +

{ title }

+

{ sub_title }

+
+ +
+
+
    + { + tags.map((tag: string, key: any) => ( +
  • + { tag } +
  • + )) + } +
+
+
+ +

+ { first_update.toLocaleString('default', { + month: `long`, + day: `numeric`, + year: `numeric` + }) } +

+
+
+
+ + + ); +}; +export default Headers; diff --git a/frontend/app/blog/post/[id]/PostCard.tsx b/frontend/app/blog/post/[id]/PostCard.tsx new file mode 100644 index 0000000..5d646cb --- /dev/null +++ b/frontend/app/blog/post/[id]/PostCard.tsx @@ -0,0 +1,95 @@ +import React from 'react'; +import Markdown from 'react-markdown'; +import { Card, CardBody } from '@nextui-org/card'; +import remarkGfm from 'remark-gfm'; +import remarkToc from 'remark-toc'; +import remarkFrontmatter from 'remark-frontmatter'; +import axios from 'axios'; +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; +import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism'; +import rehypeRaw from 'rehype-raw'; +import rehypeSanitize from 'rehype-sanitize'; +import Headers from '@/app/blog/post/[id]/Headers'; + +type Props = { + id: string +} + +async function PostCard(props: Props) { + + let response: any; + try { + response = await axios(`${ process.env.BACKEND_URL }/get_post/${ props.id }`); + } catch (e) { + return ( + + + Fatal: Failed to load the post. + + + ); + } + + + let post_data = response.data; + + return ( +
+ + + {/* card headers*/ } + + + {/* card body */ } + + + + { + String((children as any)['props']['children']).replace(/\n$/, '') + } + + + ); + } else { + return ( +
+                                            
+                                                { children }
+                                            
+                                        
+ ); + } + } + } } + >{ (post_data as any)['content'] }
+
+
+
+ ); +} + +export default PostCard; diff --git a/frontend/app/blog/post/[id]/generate.tsx b/frontend/app/blog/post/[id]/generate.tsx deleted file mode 100644 index 72053c3..0000000 --- a/frontend/app/blog/post/[id]/generate.tsx +++ /dev/null @@ -1,49 +0,0 @@ -import React from 'react'; -import Markdown from 'react-markdown'; -import { Card, CardBody } from '@nextui-org/card'; -import remarkGfm from 'remark-gfm'; -import rehypeHighlight from 'rehype-highlight'; -import rehypeSanitize from 'rehype-sanitize'; -import rehypeRaw from 'rehype-raw'; -import remarkToc from 'remark-toc'; -import remarkFrontmatter from 'remark-frontmatter'; -import axios from 'axios'; - -type Props = { - id: string -} - -async function Generate(props: Props) { - - let response: any; - try { - response = await axios(`${ process.env.BACKEND_URL }/get_post/${ props.id }`); - } catch (e) { - return ( - - - Fatal: Failed to load the post. - - - ); - } - - - let post_data = response.data; - - return ( -
- - - { (post_data as any)['content'] } - - -
- ); -} - -export default Generate; diff --git a/frontend/app/blog/post/[id]/page.tsx b/frontend/app/blog/post/[id]/page.tsx index 470c296..8d1c710 100644 --- a/frontend/app/blog/post/[id]/page.tsx +++ b/frontend/app/blog/post/[id]/page.tsx @@ -1,17 +1,18 @@ import React from 'react'; -import Generate from '@/app/blog/post/[id]/generate'; +import PostCard from '@/app/blog/post/[id]/PostCard'; import { MotionDiv } from '@/app/public/MotionDiv'; function BlogReader({ params }: { params: { id: string } }) { return ( -
- +
+
); diff --git a/frontend/app/layout.tsx b/frontend/app/layout.tsx index 5c78b6a..e7b286c 100644 --- a/frontend/app/layout.tsx +++ b/frontend/app/layout.tsx @@ -20,9 +20,9 @@ export default function RootLayout({ -
+
-
+
{ children }
diff --git a/frontend/components/Navbar.tsx b/frontend/components/Navbar.tsx index 8be058d..4d5d147 100644 --- a/frontend/components/Navbar.tsx +++ b/frontend/components/Navbar.tsx @@ -111,7 +111,7 @@ function Navbar() { return ( <>
+ className={ `flex flex-col min-w-[50px] md:min-w-[256px] mr-0 md:mr-2 ml-2 md:ml-8 min-h-dvh pt-8 sticky` }>
diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs index 2216b31..2de3023 100644 --- a/frontend/next.config.mjs +++ b/frontend/next.config.mjs @@ -5,6 +5,10 @@ const nextConfig = { { protocol: 'https', hostname: 'oss.lumither.com' + }, + { + protocol: 'https', + hostname: 'pic.re' } ] } diff --git a/frontend/package.json b/frontend/package.json index f4a0254..35c8e4f 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "@nextui-org/react": "^2.3.5", + "@types/react-syntax-highlighter": "^15.5.13", "axios": "^1.6.8", "framer-motion": "^11.2.10", "lxgw-wenkai-webfont": "^1.7.0", @@ -19,7 +20,7 @@ "react-dom": "^18", "react-icons": "^5.1.0", "react-markdown": "^9.0.1", - "rehype-highlight": "^7.0.0", + "react-syntax-highlighter": "^15.5.0", "rehype-raw": "^7.0.0", "rehype-sanitize": "^6.0.0", "remark-frontmatter": "^5.0.0",