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 包一层

相关推荐
用户904706683578 分钟前
Nuxt css 如何写?
前端
夏天想9 分钟前
element-plus的输入数字组件el-input-number 显示了 加减按钮(+ -) 和 小三角箭头(上下箭头),怎么去掉+,-或者箭头
前端·javascript·vue.js
0思必得010 分钟前
[Web自动化] Selenium基础介绍
前端·python·selenium·自动化·web自动化
Filotimo_12 分钟前
前端.d.ts文件作用
前端
进击的野人12 分钟前
Vue 3 响应式数据解构:toRef 与 toRefs 的深度解析
前端·vue.js·前端框架
ohyeah14 分钟前
CSS 作用域隔离实战:React、Vue 与 Styled Components 的三种范式
前端
二哈喇子!30 分钟前
前端HTML、CSS、JS、VUE 汇总
开发语言·前端
小白路过30 分钟前
node-sass和sass兼容性使用
前端·rust·sass
IT_陈寒30 分钟前
Python 3.12 新特性实战:这5个改进让我的开发效率提升40%
前端·人工智能·后端
两个西柚呀32 分钟前
每日前端面试题-防抖和节流
前端