react项目实现预览markdown,以及代码高亮

7 篇文章 0 订阅
5 篇文章 0 订阅

react项目实现预览markdown,以及代码高亮

前言

不少的react项目中需要实现markdown预览以及代码高亮的功能,效果如下。
在这里插入图片描述
在这里插入图片描述
上面图片展示的内容是我在个人项目中实现的效果,用到了两个库react-markdownreact-syntax-highlighter,一个用于预览markdown文本,另外一个用于代码高亮展示。

一、react-syntax-highlighter的使用

我下载的版本是15.4.5。
下面是我自己在项目使用中封装的组件

import React from 'react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus, coyWithoutShadows, darcula } from 'react-syntax-highlighter/dist/esm/styles/prism';// 代码高亮主题风格

// vscDarkPlus vscode 暗色主题
// darcula  webstorm 暗色主题
// coyWithoutShadows 上面展示的效果

type tProps = {
  textContent: string;
  language: string;
  darkMode?: boolean;
}

const them = {
  dark: vscDarkPlus,
  light: coyWithoutShadows
};

const OmsSyntaxHighlight = (props: tProps) => {
  const { textContent, darkMode, language = 'txt' } = props;
  if (typeof darkMode === 'undefined') {
    them.light = darcula;
  }
  if (typeof darkMode === 'boolean') {
    them.light = coyWithoutShadows;
  }
  return (
    <SyntaxHighlighter
      showLineNumbers={true} // 是否展示左侧行数
      lineNumberStyle={{ color: '#ddd', fontSize: 10 }} // 左侧行数的样式
      style={darkMode ? them.dark : them.light}  // 主题风格
      language={language}  // 需要语言类型 如css, jsx , javascript 等
      PreTag='div'  
    >
      {String(textContent).replace(/\n$/, '')}
    </SyntaxHighlighter>
  );
};

export default OmsSyntaxHighlight;
import React from 'react';
import { OmsSyntaxHighlight } from 'xxxxx';

const textContent = 'console.log(1);'

const Demo = () => {
 return <OmsSyntaxHighlight textContent={textContent} language={javascript} darkMode />
}

可以根据需求实现二次封装达到想要的效果,比如在我的项目中我需要实现代码高亮的主题随我的项目主题改变实现亮暗主题的切换效果。
在这里插入图片描述
在这里插入图片描述
如果我没有传入darkMode这个参数,默认的就是webstorm的风格,官方提供了很多风格。感兴趣的话可以去这里查看
在这里插入图片描述

二、react-markdown的使用

import React from 'react';
import ReactMarkdown from 'react-markdown';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus, coyWithoutShadows, darcula } from 'react-syntax-highlighter/dist/esm/styles/prism';

// darcula webstorm
// vscDarkPlus vscode暗色主题

type tProps = {
  textContent: string
  darkMode?: boolean; // markdown文本
}

const them = {
  dark: vscDarkPlus,
  light: coyWithoutShadows
};

const OmsViewMarkdown = (props: tProps) => {
  const { textContent, darkMode } = props;
  if (typeof darkMode === 'undefined') {
    them.light = darcula;
  }
  if (typeof darkMode === 'boolean') {
    them.light = coyWithoutShadows;
  }
  return (
    <ReactMarkdown
      components={{
        code({ node, inline, className, children, ...props }) {
          const match = /language-(\w+)/.exec(className || '');
          return !inline && match ? (
            <SyntaxHighlighter
              showLineNumbers={true}
              style={darkMode ? them.dark : them.light}
              language={match[1]}
              PreTag='div'
              {...props}
            >
              {String(children).replace(/\n$/, '')}
            </SyntaxHighlighter>
          ) : (
            <code className={className} {...props}>
              {children}
            </code>
          );
        }
      }}
    >
      {textContent}
    </ReactMarkdown>
  );
};

export default OmsViewMarkdown;

import React from 'react';
import { OmsViewMarkdown} from 'xxxxx';

const textContent = `
## 项目简介
本项目后端使用gin、gorm和ssh、sftp开发。旨在编写一个轻量,易用,多平台的运维项目。
前端使用react、typescript、vite构建。
现阶段目的是做一个阉割版的xshell并简单的实现ansible或者saltstack的部分功能。

### 目前已经实现的功能
1. 隧道, 类似ssh的-L和-R
2. cron任务和长进程的管理
3. ssh命令批量执行
4. 文件批量的上传 流式传输支持大文件
5. 基于sftp文件浏览器

### 查看后端代码请移步到 [oms](https://github.com/ssbeatty/oms)`;

const Demo = () => {
 return <OmsViewMarkdown textContent={textContent}  darkMode />
}

在这里插入图片描述

总结

以上就是两个库比较简单的使用方法,感兴趣的小伙伴可以在自己的项目中试一下。另外本文中的图片来自个人项目oms运维管理系统目前实现了文件浏览上传等功能。感兴趣的小伙伴可以看看👀github连接 gitee连接
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
React-markdown是一个用于在React项目中渲染Markdown文件的组件。它可以将Markdown文件转换为HTML,并将其显示在网页上。通过使用React-markdown,您可以在React应用程序中实现在线浏览Markdown文件的功能。 Markdown-navbar是一个用于自动生成侧边导航栏目录的插件。它可以根据Markdown文件的标题层级结构生成导航栏,并且可以通过点击导航栏中的链接来快速跳转到相应的部分。 使用React-markdownMarkdown-navbar可以实现React项目中浏览Markdown文件并自动生成侧边导航栏的功能。首先,您需要安装相关的依赖包,如yarn add react-markdown和yarn add markdown-navbar。然后,您可以使用React-markdown来渲染Markdown文件内容,并在合适的地方添加Markdown-navbar组件,这样就可以实现自动生成侧边导航栏目录的效果。 例如,在React项目中引入React-markdownMarkdown-navbar,并使用React-markdown来渲染Markdown文件的内容。您可以通过fetch函数获取Markdown文件的内容,并将其传递给ReactMarkdown组件进行渲染。同时,您还可以使用Markdown-navbar组件来生成侧边导航栏目录。通过这样的配置,您就可以实现React项目中浏览Markdown文件并自动生成侧边导航栏的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [使用react-markdownmarkdown-navbar实现在线浏览markdown文件并自动生成侧边导航栏目录(react项目)](https://blog.csdn.net/xz060585/article/details/129494847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [markdown-navbar:React 的最佳降价导航栏](https://download.csdn.net/download/weixin_42102713/20451909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值