Material-UI 样式和 html/markdown

ic3*_*ic3 13 reactjs material-ui javascript-marked

我们的应用程序是使用 Material-UI 库(带有主题)构建的。作为此应用程序的一部分,我们将 markdown 解析为 html(标记库)。

如何将material-ui主题(Typography)应用到纯html?

不知何故

<div dangerouslySetInnerHTML={ {__html: marked(markdown code)}}/>
Run Code Online (Sandbox Code Playgroud)

应该具有由material-ui Typography 定义的样式

Rya*_*ell 16

所有变体的样式Typography都在主题中theme.typography.<variant>(您可以在此处浏览默认主题中的这些条目: https: //material-ui.com/customization/default-theme/#default-theme)。您可以利用它来创建样式来定位您想要支持的标签,如下例所示:

import React from "react";
import { makeStyles } from "@material-ui/core";
import Typography from "@material-ui/core/Typography";

const useStyles = makeStyles((theme) => {
  const tags = ["h1", "h2", "h3", "h4", "h5", "h6"];
  const nestedRules = {};
  tags.forEach((tag) => {
    nestedRules[`& ${tag}`] = { ...theme.typography[tag] };
  });
  return {
    root: nestedRules
  };
});

export default function App() {
  const classes = useStyles();
  return (
    <Typography
      className={classes.root}
      variant="body1"
      dangerouslySetInnerHTML={{
        __html:
          "<h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6><p>default body1</p>"
      }}
    ></Typography>
  );
}
Run Code Online (Sandbox Code Playgroud)

编辑样式 html 标签

相关答案:material-ui makeStyles: how to style by tag name?


Raj*_*jiv 6

使用常规Typography组件并以与问题中传递类似的方式传递该 HTML。

<Typography
    variant="h2"
    color="primary"
    dangerouslySetInnerHTML={{ __html: "<p>Hi from inner HTML</p>" }}>
    
</Typography>
Run Code Online (Sandbox Code Playgroud)

这里有一个问题,当dangerouslySetInnerHTML通过时,不要像孩子那样传递任何东西。

这是一个工作演示:

编辑神经饼干-nedrz

注意:还要确保该函数marked(markdown code)以字符串形式返回 HTML。