首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Material-UI样式和html / markdown

Material-UI样式和html / markdown
EN

Stack Overflow用户
提问于 2021-02-03 21:17:13
回答 3查看 2K关注 0票数 4

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

如何将material-ui主题(排版)应用于纯html?

不知何故

代码语言:javascript
复制
<div dangerouslySetInnerHTML={ {__html: marked(markdown code)}}/>

应具有material-ui字体定义的样式

EN

回答 3

Stack Overflow用户

发布于 2021-02-14 00:44:05

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

代码语言:javascript
复制
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>
  );
}

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

票数 7
EN

Stack Overflow用户

发布于 2021-02-03 22:04:14

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

代码语言:javascript
复制
<Typography
    variant="h2"
    color="primary"
    dangerouslySetInnerHTML={{ __html: "<p>Hi from inner HTML</p>" }}>
    
</Typography>

这里需要注意的一点是,当传递dangerouslySetInnerHTML时,不要将任何内容作为子级传递。

下面是一个有效的演示:

注意:还要确保函数marked(markdown code)返回字符串形式的超文本标记语言。

票数 1
EN

Stack Overflow用户

发布于 2021-02-19 05:58:39

使用markdown-to-jsx npm包。here是来自material ui模板的示例。

基本上,您必须创建一个ReactMarkdown喜欢的配置对象,该对象特定于material ui

代码语言:javascript
复制
import React from 'react';
import ReactMarkdown from 'markdown-to-jsx';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Link from '@material-ui/core/Link';

const styles = (theme) => ({
  listItem: {
    marginTop: theme.spacing(1),
  },
});

const options = {
  overrides: {
    h1: {
      component: Typography,
      props: {
        gutterBottom: true,
        variant: 'h5',
      },
    },
    h2: { component: Typography, props: { gutterBottom: true, variant: 'h6' } },
    h3: { component: Typography, props: { gutterBottom: true, variant: 'subtitle1' } },
    h4: {
      component: Typography,
      props: { gutterBottom: true, variant: 'caption', paragraph: true },
    },
    p: { component: Typography, props: { paragraph: true } },
    a: { component: Link },
    li: {
      component: withStyles(styles)(({ classes, ...props }) => (
        <li className={classes.listItem}>
          <Typography component="span" {...props} />
        </li>
      )),
    },
  },
};

export default function Markdown(props) {
  return <ReactMarkdown options={options} {...props} />;
}

我直接从他们的例子中得到了这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66028355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档