当前位置: 首页 > news >正文

react native中markdown添加数学公式的支持

在react native中实现markdown最佳选择当然是 react-native-markdown-display,不过有一个问题是它不支持数学公式的显示,我的解决方法是通过rules和markdownit属性进行功能扩展
需要安装的lib

 "katex": "^0.16.22","markdown-it-math": "^4.1.1","markdown-it-texmath": "^1.0.0","react-native-mathjax-svg": "^0.9.9",

功能代码如下:

import React, { useMemo, useCallback } from 'react';
import {Linking, ScrollView, StyleSheet, Text, View} from "react-native";
import Toast from "react-native-simple-toast";
import Markdown, {ASTNode,MarkdownIt,RenderRules,
} from "react-native-markdown-display";
import MathJax from 'react-native-mathjax-svg'
import MarkdownItMath from "markdown-it-math";// 简易 Markdown 预览组件
interface MarkdownViewProps {markdown: string;scrollable?: boolean;
}function MarkdownView({ markdown, scrollable = false }: MarkdownViewProps) {const markdownStyles = useMemo(() => StyleSheet.create({body: {color: '#333',fontSize: 16,lineHeight: 24,flexWrap:'wrap',backgroundColor:'green',overflow:'scroll'},heading1: {fontSize: 24,fontWeight: 'bold',marginVertical: 8,color: 'red',},heading2: {fontSize: 20,fontWeight: 'bold',marginVertical: 8,color: 'blue',},heading3: {fontSize: 18,fontWeight: 'bold',marginVertical: 6,color: 'green',},paragraph: {marginVertical: 8,fontSize: 14,lineHeight: 20,color: '#2E3742',},list_item: {marginVertical: 4,fontSize: 14,},code_inline: {backgroundColor: '#f1f1f1',color: '#e53935',borderRadius: 3,paddingHorizontal: 4,},code_block: {backgroundColor: '#f5f5f5',padding: 12,borderRadius: 6,marginVertical: 8,fontSize: 14,},blockquote: {borderLeftWidth: 4,borderLeftColor: '#3465F5',paddingLeft: 12,marginVertical: 8,backgroundColor: '#F8F9FA',paddingVertical: 6,borderRadius: 4,},link: {color: '#3465F5',textDecorationLine: 'underline',},bullet_list: {marginLeft: 8,},ordered_list: {marginLeft: 8,},}), []);const markdownItInstance = new MarkdownIt({typographer: true,}).use(MarkdownItMath, {inlineOpen: '$',inlineClose: '$',blockOpen: '$$',blockClose: '$$',}).use(MarkdownItMath, {inlineOpen: '\\(',inlineClose: '\\)',blockOpen: '\\[',blockClose: '\\]',})const renderEquation = (node: ASTNode) => {return  <MathJax key={node.key}>{node.content}</MathJax>}const rules: RenderRules = {math_inline: renderEquation,math_block: renderEquation,textgroup: (node, children) => {return (<Text key={node.key} selectable={true}>{children}</Text>)},}const MarkdownContent = useMemo(() => (<Markdownstyle={markdownStyles}rules={rules}markdownit={markdownItInstance}>{markdown}</Markdown>), [markdown, markdownStyles]);if (scrollable) {return (<ScrollView style={styles.markdownPreview}>{MarkdownContent}</ScrollView>);}return (<View style={styles.markdownContainer}>{MarkdownContent}</View>);
}const styles = StyleSheet.create({equationContainer: {overflow: 'hidden',alignSelf: 'flex-start', // 防止拉伸maxWidth: '100%',       // 公式最大宽度},markdownPreview: {flex: 1,padding: 0,},markdownContainer: {padding: 0,borderWidth:1,backgroundColor:'red'},markdownText: {fontSize: 14,lineHeight: 22,color: '#1F2937',},
});export default MarkdownView;

现在有一个已知问题是公式的内容超出一行的时候渲染内容会超出屏幕不会换行,这个我得抽时间来解决

http://www.lryc.cn/news/609043.html

相关文章:

  • Java ++i 与 i++ 底层原理
  • 机器翻译入门:定义、发展简史与核心价值
  • [自动化Adapt] 录制引擎
  • MVCC:数据库事务隔离的 “时空魔法”
  • mysql管理
  • 【Linux系统】进程间通信:匿名管道
  • http://localhost:8080/photos/xxx.png的本地图片访问方案
  • 常见的框架漏洞(Thinkphp,spring,Shiro)
  • io_submit系统调用及示例
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-54,(知识点:硬件设计流程)
  • 知识随记-----MySQL 连接池健康检测与 RAII 资源管理技术
  • vulnhub-noob靶机攻略
  • ICT模拟零件测试方法--电位器测试
  • 【QT】常⽤控件详解(二)windowOpacitycursorfontsetToolTipfocusPolicystyleSheet
  • 8.1.3 TiDB集群方案雨Replication原理
  • git用户设置
  • 嵌入式 C 语言入门:多文件编程实践笔记 —— 从文件创建到调用
  • Python Seaborn【数据可视化库】 全面讲解
  • C++ 之 【模拟实现 优先级队列】
  • Java 大视界 -- Java 大数据机器学习模型在金融市场情绪分析与投资决策辅助中的应用(379)
  • 控制建模matlab练习05:比例积分控制-①系统建模
  • 【游戏比赛demo灵感】Scenario No.9(又名:World Agent)
  • 【Python✨】解决 Conda 安装 MoviePy 报错问题
  • 【Linux系统编程】进程信号
  • Rust 同步方式访问 REST API 的完整指南
  • python学智能算法(三十一)|SVM-Slater条件理解
  • Rust:如何开发Windows 动态链接库 DLL
  • 【AI编程工具IDE/CLI/插件专栏】-国外IDE与Cursor能力对比
  • 08.Redis 持久化
  • Pytorch实现一个简单的贝叶斯卷积神经网络模型