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

相关推荐
我的xiaodoujiao10 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
岁月宁静10 小时前
从0到1:智能汇 AI 全栈实战,拆解多模态 AI 应用开发全流程
前端·vue.js·node.js
廾匸64010 小时前
语义化标签
前端·javascript·html
烛阴10 小时前
隐式vs显式:解密C#类型转换的底层逻辑
前端·c#
Fantasydg10 小时前
AJAX JSON学习
前端·学习·ajax
瓢儿菜201811 小时前
Web开发:什么是 HTTP 状态码?
前端·网络协议·http
1024小神11 小时前
swiftui使用WKWebView加载自签的https服务,允许不安全访问
前端
anyup11 小时前
支持鸿蒙!开源三个月,uView Pro 开源库近期更新全面大盘点,及未来计划
前端·vue.js·uni-app
BBB努力学习程序设计12 小时前
用Bootstrap一天搞定响应式网站:前端小白的救命稻草
前端·html
嘴平伊之豬12 小时前
跟着AI速度cli源码三-交互问答系统
前端·node.js