React 个人博客 集成 MDX / tailwindcss/typography

onlion rick-chou.github.io/react-aweso...
代码地址 github.com/rick-chou/o...

博客系列文章

第一版的方案用的是 vite-plugin-markdown 用的是 beta 版本

"vite-plugin-markdown": "^2.2.0-2",

ts 复制代码
import { Mode, plugin as md } from 'vite-plugin-markdown';

// vite plugin
md({ mode: [Mode.HTML, Mode.MARKDOWN, Mode.REACT, Mode.TOC] }),

然后用 tailwind 的 @tailwindcss/typography plugin

jsx 复制代码
import { html } from 'README.md';

<div
  css={animationDelay(0.1)}
  className="px-8 pb-8 prose prose-slate overflow-y-scroll lg:prose-xl max-w-none dark:prose-invert"
  dangerouslySetInnerHTML={{ __html: html }}
/>;

其实整体下来效果还不错 可参考 @tailwindcss/typography Live Demo

现在的方案是接入 @mdx-js/react

一方面它支持 jsx 语法

另一方面可以用它的社区插件实现更多的定制化的功能

ts 复制代码
import mdx from '@mdx-js/rollup';
// 实现代码高亮
import rehypeHighlight from 'rehype-highlight';
// 给代码块添加props 例如添加文件名
import rehypeMdxCodeProps from 'rehype-mdx-code-props';
// 支持类似Github Markdown语法
import remarkGfm from 'remark-gfm';

// plugin
  mdx({
    jsxImportSource: '@emotion/react',
    providerImportSource: '@mdx-js/react',
    remarkPlugins: [remarkGfm],
    rehypePlugins: [rehypeHighlight, rehypeMdxCodeProps],
  }),

改写 Article 组件 添加 not-prose class 类名可以告诉 tailwind 这里不需要 typography 插件的样式

tsx 复制代码
import { MDXProvider } from '@mdx-js/react';
import { type FC, type PropsWithChildren } from 'react';
import { codeBlockStyle, codeBtnStyle, codeFilename } from './style';
import './theme.scss';

const Article: FC<PropsWithChildren<{ classname?: string }>> = ({
  children,
  classname = '',
}) => {
  return (
    <div
      className={`px-8 pb-8 prose prose-slate overflow-scroll max-w-[45vw] dark:prose-invert !select-text no-scrollbar ${classname}`}>
      <MDXProvider
        components={{
          a: props => <a target="_blank" {...props} className="italic"></a>,
          pre(props: any) {
            return (
              <div className="not-prose">
                <figure css={codeBlockStyle}>
                  <figcaption>
                    <span css={codeBtnStyle} />
                    <span css={codeBtnStyle} />
                    <span css={codeBtnStyle} />
                    <span css={codeFilename}>
                      {props.filename ??
                        props?.children?.props?.className?.split('-').at(-1)}
                    </span>
                  </figcaption>
                  <pre {...props} className="!bg-[#002b36]"></pre>
                </figure>
              </div>
            );
          },
        }}>
        {children}
      </MDXProvider>
    </div>
  );
};

export default Article;

因为项目中引用了 tailwindcss

ts 复制代码
corePlugins: {
  preflight: true,
}

且在配置中除去了默认样式 所以还得用 @tailwindcss/typography 包一层

相关推荐
崔庆才丨静觅6 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60617 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了7 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅7 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅7 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅8 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅8 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊8 小时前
jwt介绍
前端
爱敲代码的小鱼8 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax