React-markdown: Support shortcodes

Created on 1 Dec 2017  路  12Comments  路  Source: remarkjs/react-markdown

It would be very interesting to support shortcodes for rendering specific components, like Hugo does.

There's already a remark plugin for parsing.

Most helpful comment

const shortcodes = require('remark-shortcodes')

<Markdown
  source={input}
  plugins={[[shortcodes, {startBlock: "{{>", endBlock: "<}}"}]]}
  renderers={{shortcode: ShortcodeRenderer}}
/>

All 12 comments

const shortcodes = require('remark-shortcodes')

<Markdown
  source={input}
  plugins={[[shortcodes, {startBlock: "{{>", endBlock: "<}}"}]]}
  renderers={{shortcode: ShortcodeRenderer}}
/>

The code above seems to not work right now. Firstly, the start block and end block are a bit misleading: they should be {{< and >}}. Furthermore, the plugin does work but it doesn't "eat" the strings - maybe it should be used as an "astPlugin" instead @rexxars?

@1999 Just tested it, seems to work fine for me... the strings are replaced with whatever the ShortCodeRenderer renders.

@melbourne2991 that's interesting. Can you share an example of how you tested this? It didn't work when I tried it a week ago.

Sure (shortCodeRenders is just a dictionary of react function components):

const ShortCodeRenderer = ({ identifier, attributes }) => {
  const Renderer = shortCodeRenderers[identifier];

  if (!Renderer) {
    console.warn("No renderer for shortcode:", identifier);
    return null;
  }

  return <Renderer {...attributes} />;
};

export const MarkdownRenderer = ({ ...props }) => {
  return (
    <ReactMarkdown
      renderers={{
        code: CodeBlockRenderer,
        shortcode: ShortCodeRenderer
      }}
      plugins={[
        [
          shortcodes,
          {
            startBlock: "[[",
            endBlock: "]]"
          }
        ]
      ]}
      {...props}
    />
  );
};

Thanks! I took your code and put it into the sandbox + added the shortcodes syntax from Hugo. Still doesn't look like the plugin works: https://codesandbox.io/s/407yn2okl4?fontsize=14. Can you check what's wrong there?

@1999 Ah it looks as though what you're after is not supported by the shortcodes plugin, see this issue:

https://github.com/djm/remark-shortcodes/issues/7

I could confirm that the shortcode is not working. Can you shed any light on this? @rexxars

@phanatuan give https://github.com/remarkjs/react-markdown/issues/113#issuecomment-387216043 a try.
If you have a question feel free to reach out at https://github.com/remarkjs/remark/discussions
If you believe there is a bug please file a new issue with more detail

Hi there. Since version 5, this no longer works.

I tried adding the new node serializer property but it didn't solve the problem.

Again
If you have a question feel free to reach out at https://github.com/remarkjs/remark/discussions
If you believe there is a bug please file a new issue with more detail

For folks looking for more information on shortcodes and react-markdown v5+, please see https://github.com/remarkjs/react-markdown/issues/501

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LinusU picture LinusU  路  3Comments

wqh17101 picture wqh17101  路  3Comments

garrilla picture garrilla  路  4Comments

amalajeyan picture amalajeyan  路  4Comments

ghost picture ghost  路  4Comments