Random Thoughts

This blog development process

Markdown renderer component that can render other React components

Monday, April 9th, 2018

Alright, previously I wrote about how this site renders content from Markdown files, you can read it here: Creating React component with Markdown renderer and code syntax highlight. In this post I'll explain some cool upgrades I made in the MarkdownRenderer component used in this site.

How it was working so far

Just to give you some context, this site uses markdown static files to store the content. There is no rich-text editor interface to provide fancy features for different kinds of data representation. If I want to include an image in the middle of the post I had to explicitly declare the markup using HTML tags:

  <img alt="The alternative textual representation" src="full-path/of/the-image.png" />
  <figcaption>Some helpful caption</figcaption>

It's not difficult, although too repetitive and prone to failures. That's actually a bit boring to do repeatedly every time. The markdown itself provides a lot of patterns to simplify the data structures, and it's awesome. But markdown is limited and that's why for many non-textual contents some more complex data structures are required.

What's expected

You know what would be cool? React components inside markdown content!

Using a React component I can simplify the data structure patterns and also include incredible full feature behaviors. Ya know, React! Let's take some examples:


As mentioned before, this is how I was using to include images in the markdown:

  <img alt="The alternative textual representation" src="path/of/the/image.png" />
  <figcaption>Some helpful caption</figcaption>

And this is how it is with Image React component:

  alt="The alternative textual representation"
  caption="Some helpful caption"

That's simpler right? It may not look absurdly simpler at a glance, but if we take a further look at the Image component we'll see a whole bunch of cool features and a world of possibilities.

Embed Codepen

The API of the React components are made in the way I desire. I don't have to follow any HTML5 compliant specification or community patterns. Let's see a more complex example:

<!-- Using Iframe -->
  title="Card pack"
  style="width: 100%;"
  See the Pen <a href="https://codepen.io/bernardodiasc/pen/rdZmjK/">Card pack</a> by Bernardo Dias (<a href="https://codepen.io/bernardodiasc">@bernardodiasc</a>) on <a href="https://codepen.io">CodePen</a>.

<!-- Or the recommended way -->
  data-pen-title="Card pack"
  See the Pen <a href="https://codepen.io/bernardodiasc/pen/rdZmjK/">Card pack</a> by Bernardo Dias (<a href="https://codepen.io/bernardodiasc">@bernardodiasc</a>) on <a href="https://codepen.io">CodePen</a>.
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

So much can be simplified and standardized here:

  title="Card pack"

Pretty better! Let the React component to handle the general patterns. Let the markdown content simpler.

Embed video

Just one last example :)


To become:

<Video hash="dnzE9ip5U04" />

Sizing properties will be pretty much the same almost all times for this site use case and then can be optional. This is the same for all other attributes.

How to do such awesome thing?

Now let's get to the actual solution!

To make this upgrade in my existing MarkdownRenderer component I made a switch of the https://github.com/markedjs/marked/ lib in favor of https://github.com/probablyup/markdown-to-jsx.

Honestly, I can't complain about the previous implementation of this component. It was missing just one feature to provide such awesomeness: the ability to override elements. And that's one of the coolest features of markdown-to-jsx.

Code blocks syntax highlight

Make this move had a drawback regarding the code blocks and syntax highlight. Will explain about below, but because of that, I reconsidered the switching move. At first, I considered implementing the desired feature in the https://github.com/utatti/react-render-html, basically allowing the overrides options, the same as the markdown-to-jsx. That would require much more time for me, unfortunately, I need to pragmatical here. What makes a bit difficult is that react-render-html relies on https://github.com/inikulin/parse5, and that's very strict HTML5 spec compliant parser. Anyway, this is still a possibility if I find time for an open source contribution and if the library author desires.

Back on the drawback, it's actually no big deal. The point is that markdown-to-jsx is compatible with highlight.js BUT it only adds the code language classes in the code tag. This would require highlight.js to parse the code contents on the client side. I didn't want that, because I have pre-rendered feature in this site and I want to take advantage on that and relieve the client device from extra processing as much as possible (I mean, it's already hydrating the React app...).

The solution was to override the pre tags with a custom CodeBlock component:

const CodeBlock = (node) => {
  const props = node.children.props
  const language = props.className ? props.className.replace(/^(lang-)/, '') : ''
  const code = props.children || ''
  const htmlBlock = language
    ? hljs.highlight(language, code).value
    : hljs.highlightAuto(code).value
  return renderHTML(`<pre><code class="${props.className}">${htmlBlock}</code></pre>`)

Well, that still relies on react-render-html and as noted in the library, parse5 is a heavyweight library. Luckily, as I said, this site has pre-rendering, it will work with JS disabled (try it). So I'm not much worried about the weight of the JS bundle since it's not a blocker for the main experience.

The markdown renderer component

Alright alright, despite that code block syntax highlight situation, everything else had a smooth transition. Here's how the component render looks like now:

class MarkdownRenderer extends PureComponent {
  static defaultProps = {
    text: '',

  render() {
    const { text } = this.props
    return text ? (
          overrides: {
            pre: {
              component: CodeBlock
    ) : null

Notice the overrides options, I can do a lot more with that, it's really cool and worth to check out the library docs https://github.com/probablyup/markdown-to-jsx#optionsoverrides---override-any-html-tags-representation.

Bottom line

The data structures on the markup are all about content representation. And the patterns are there to relieve the mental burden of repetitive tasks, such as writing these markup data structures. Templates for markup has always been there, although they are cooler and powerful now in the age of client-side JS rendering libraries.

Simple is better!