Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typography Markdown support? #12290

Closed
2 tasks done
9oelM opened this issue Jul 26, 2018 · 11 comments
Closed
2 tasks done

Typography Markdown support? #12290

9oelM opened this issue Jul 26, 2018 · 11 comments
Labels
component: Typography The React component. support: question Community support but can be turned into an improvement

Comments

@9oelM
Copy link

9oelM commented Jul 26, 2018

Context

  • So I was looking into various implementations of the Typography component.
  • And I saw that it's a bit troublesome to manually add many Typography components with props you want to put in, such as variant prop.
  • So I'm suggesting: why not put enable markdown prop (and some supplementary props) on Typography component and let it render the markdown with all kinds of headings and variants?
  • This is a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior

Currently, you've gotta do something like:

<Typography variant="headline" gutterBottom color = "primary" align = "center">
        Explanation of the problem
</Typography>
<Typography variant="subheading" gutterBottom color = "primary">
        So what Im tyring to explain is
</Typography>
<Typography variant="body1" gutterBottom color = "primary">
        The detail, right. Youve gotta write repeated pattern of codes. 
</Typography>

If you are writing generic texts.

Suggestion

Instead of writing Typography components all over, can't we do something like:

const content = `# Explanation of the problem
## So what I'm trying to explain is
The detail, right. You've gotta write repeated pattern of codes.`

<Typography markdown={content} option ={{
              all: {
                gutterBottom: true,
                color: 'primary'
              },
              h1: { // this h1 could be something like 'headline' as well, depends on naming.
                align: 'center'
              }
            }} />
  • Then, it would the render the same thing as the example in the Current Behavior section.
  • What we can do right now with Typography is a real pain if you are trying to write many texts with various props and heading types.
  • In contrast, markdown makes it easy to keep and see what you are writing as well, and doing so also separates the content from the props, which makes it easy to digest your codes when you are reading them.

Thanks!

@oliviertassinari oliviertassinari added out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) component: Typography The React component. labels Jul 26, 2018
@mbrookes
Copy link
Member

@9oelM You could perhaps take advantage of @material-ui/docs/MarkDownElement, but it isn't documented, so you'd have to dig into the code to understand how to use it & override the styles.

Alternatively there are existing React components such as https://github.com/rexxars/react-markdown

@oliviertassinari
Copy link
Member

Also, a better pattern is probably to do it the other way around. At least, it's what we have been doing on the project I'm working on. We have a markdown parser that uses the right typography variant based on the context automatically.

@oliviertassinari

This comment has been minimized.

@9oelM
Copy link
Author

9oelM commented Jul 29, 2018

Thanks! Will take reference of your example.

@oliviertassinari oliviertassinari changed the title [Feature Request] [Typography] Enable markdown inside Typography component? [Typography] Markdown support? Jul 29, 2018
@oliviertassinari oliviertassinari added discussion and removed out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) labels Jul 29, 2018
@benshanahan1
Copy link

Thanks, this is super helpful. Things are working as expected, however, I am getting the following warning:

Warning: Received `false` for a non-boolean attribute `ordered`.

If you want to write it to the DOM, pass a string instead: ordered="false" or ordered={value.toString()}.

If you used to conditionally omit it with ordered={condition && value}, pass ordered={condition ? value : undefined} instead.
    in span (created by Typography)
    in Typography (created by WithStyles(Typography))
    ... (rest of call stack omitted)

Any thoughts? Is this a problem with the Typography component itself?

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 14, 2019

@benshanahan1 We have added a Markdown demo in the repository. You can find the source:

return <ReactMarkdown options={options} {...props} />;

It's using markdown-to-jsx over react-markdown as it's faster and lighter.

Alternatively, if you really need performance, you would need to use a pure HTML approach, it's what we do for the docs:

more.dangerouslySetInnerHTML = { __html: renderedMarkdown };

@oliviertassinari oliviertassinari added support: question Community support but can be turned into an improvement and removed discussion labels Jan 14, 2019
@oliviertassinari oliviertassinari changed the title [Typography] Markdown support? Typography Markdown support? Jan 14, 2019
@benshanahan1
Copy link

I'll check it out, thank you!

@GrahamMThomas

This comment has been minimized.

@junibrosas
Copy link

@oliviertassinari Hi, regarding overriding default nodes to Typography, can your demo also be implemented using react-markdown?

@oliviertassinari
Copy link
Member

react-markdown was really slow, and doesn't seem to have changed. I would avoid it.

@alecananian
Copy link
Contributor

alecananian commented Apr 28, 2020

For those sticking with react-markdown, you'll need to use the renderers property to override elements. Example using the material-ui Typography component for paragraphs:

<ReactMarkdown
  source={text}
  renderers={{ paragraph: Typography }}
/>

or if you need to apply additional props to Typography:

<ReactMarkdown
  source={text}
  renderers={{
    paragraph: ({ children }) => (
      <Typography>
        {children}
      </Typography>
    ),
  }}
/>

The full list of overridable renderers is available in the Node types section of the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Typography The React component. support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

7 participants