markdown-it / markdown-it-container

Fenced container plugin for markdown-it markdown parser
MIT License
496 stars 74 forks source link

Add content: option for custom rendering of contents #3

Closed pumpikano closed 8 years ago

pumpikano commented 8 years ago

I think this is a nice approach. I'm opening this PR to see if there interest in getting this merged.

puzrin commented 8 years ago

IMHO that duplicates existing functionality without adding noticeable value

pumpikano commented 8 years ago

In that case, I might be simply misunderstanding how to use the plugin effectively. What is the preferred way to custom render container content?

puzrin commented 8 years ago

May be i missunderstood your goals. Could you explain why do you need to customize CONTENT of that block? This plugin is for adding HTML wrappers while keeping all markup inside. May be you need to customize fenced block? Need more examples from real world to understand.

pumpikano commented 8 years ago

In my case, I am implementing a way to add custom embeds. As contrived example, I have a container named embed whose content is yaml:

::: embed
name: path/to/component
props:
    a: 12
    b: some property
:::

In my use case, the renderer loads a React component at the specified path and renders the component with the provided props data. So the content of the container should not be rendered in this case.

With something like this PR, it is very easy to do this, something like this (off the top of my head):

md.use(markdownContainer, 'embed', {
    content: function (tokens, idx) {
        var data = yaml.safeLoad(tokens[idx].markup);
        var Component = require(data.name);
        return ReactDOMServer.renderToString(React.createElement(Component, data.props));
    }
});

I think this generalized the plugin in a way that is still appropriate to call it a "container". Do you have a suggestion for a different way to accomplish this sort of thing with markdown-it?

puzrin commented 8 years ago

That's wrong approach for this package. It's done to have markdown in container. Because that requires properly parse of nested blocks.

If you need just a container for custom data - look into direction of fenced blocks. For example, you can customize fenced block renderer.

pumpikano commented 8 years ago

Ok, I will look into that, thanks.