'use strict'; const React = require('react'); const hljs = require('highlight.js'); const Markdown = require('remarkable'); const toSlug = require('./toSlug.js'); const CWD = process.cwd(); /** * The anchors plugin adds GFM-style anchors to headings. */ function anchors(md) { md.renderer.rules.heading_open = function(tokens, idx /*, options, env */) { const textToken = tokens[idx + 1]; return ( '' ); }; } class Remarkable extends React.Component { content() { if (this.props.source) { return ( ); } else { return React.Children.map(this.props.children, child => { if (typeof child === 'string') { return ( ); } else { return child; } }); } } renderMarkdown(source) { if (!this.md) { const siteConfig = require(CWD + '/siteConfig.js'); this.md = new Markdown({ // Highlight.js expects hljs css classes on the code element. // This results in

        langPrefix: 'hljs css ',
        highlight: function(str, lang) {
          lang =
            lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
          if (lang && hljs.getLanguage(lang)) {
            try {
              return hljs.highlight(lang, str).value;
            } catch (err) {}
          }

          try {
            return hljs.highlightAuto(str).value;
          } catch (err) {}

          return '';
        },
        html: true,
        linkify: true,
      });

      // Register anchors plugin
      this.md.use(anchors);

      // Allow client sites to register their own plugins
      if (siteConfig.markdownPlugins) {
        siteConfig.markdownPlugins.forEach(function(plugin) {
          this.md.use(plugin);
        }, this);
      }
    }
    const html = this.md.render(source);

    // Ensure fenced code blocks use Highlight.js hljs class
    // https://github.com/jonschlinkert/remarkable/issues/224
    return html.replace(/
/g, '
');
  }

  render() {
    var Container = this.props.container;

    return {this.content()};
  }
}

Remarkable.defaultProps = {
  container: 'div',
};

module.exports = Remarkable;