Files
blog/frontend/src/components/Markdown.tsx
T
zhaoxi bfc83a2400 feat: 个人博客全栈实现(Rust/Axum + React/Tailwind + PostgreSQL)
后端(Axum + sqlx):
- GitHub OAuth 三层权限(管理员/用户/游客),MOCK_OAUTH 本地调试开关
- 文章 CRUD、草稿自动保存、发布/私有分离,发布时渲染 HTML
- 目录(folder 路径分层)、标签、评论、RSS、热门文章(按评论数)
- pages 表驱动动态内容:关于页、首页标题、个人名片
- S3 兼容对象存储图片上传接口(rust-s3,未配置时明确报错)
- 发布时自动提取正文首图作为封面 cover_url
- sqlx migrate 管理 5 个迁移

前端(React + Tailwind):
- 白天 Editorial / 黑夜星空双主题
- 首页(列表 + 热门侧栏 + 可编辑大标题)、文库(目录树+标签+搜索)
- Profile 页(GitHub 风格名片卡,全部字段后台可编辑)
- 后台:文件树文章管理、CodeMirror 编辑器(实时预览/自动保存/传图)
- Markdown 渲染:KaTeX 公式、代码块语言标注 + 双主题语法高亮
- 有封面的文章卡片自动整行展示,全局 ErrorBoundary

部署:
- docker-compose 本地三容器(postgres/backend/frontend)
- k8s manifests(适配 Caddy 终结 TLS → 隧道 → traefik 明文路由)
- 前端静态文件可由 Caddy 直接托管,集群只需 backend + postgres
- 可选 postgres 每日备份到 S3 的 CronJob
2026-07-18 19:15:49 +08:00

43 lines
1.4 KiB
TypeScript

import { isValidElement, type ComponentPropsWithoutRef, type ReactNode } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeHighlight from 'rehype-highlight';
import rehypeSlug from 'rehype-slug';
import rehypeKatex from 'rehype-katex';
/** 从 code 元素的 className(如 "language-python")中提取语言名 */
function extractLanguage(children: ReactNode): string {
if (isValidElement<{ className?: string }>(children)) {
const match = /language-(\w+)/.exec(children.props.className ?? '');
if (match) return match[1];
}
return 'text';
}
/** 自定义代码块:浅色底框 + 顶部语言标注栏 */
function Pre({ children }: ComponentPropsWithoutRef<'pre'>) {
const lang = extractLanguage(children);
return (
<div className="code-block-wrapper">
<div className="code-block-header">
<span>{lang}</span>
</div>
<pre className="code-block-content">{children}</pre>
</div>
);
}
/** 统一的 Markdown 渲染组件,文章页与编辑器预览共用 */
export function Markdown({ content }: { content: string }) {
return (
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeKatex, rehypeHighlight, rehypeSlug]}
components={{ pre: Pre }}
>
{content}
</ReactMarkdown>
);
}