react-remarkable icon indicating copy to clipboard operation
react-remarkable copied to clipboard

Example with JS syntax highlight

Open cbroberg opened this issue 6 years ago • 4 comments

I know it is probably the wrong place to ask.

Any chance of finding an example with react-remarkable that includes JS code highlight?

I have tried everything :(

cbroberg avatar Sep 29 '17 23:09 cbroberg

import React from 'react';
import Markdown from 'react-remarkable';
import hljs from 'highlight.js';
import 'highlight.js/styles/github-gist.css';

const highlight = (str, lang) => {
    if (lang && hljs.getLanguage(lang)) {
        try {
            return hljs.highlight(lang, str).value;
        } catch (err) {
          console.error(err);
        }
    }

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

    return '';
};

const MarkdownWithJs = () =>
  
  <Markdown options={{highlight}}>{`
  # Header

  \`\`\`js
  const func = () => {
    // here is your js code
  }
  \`\`\`
  `}</Markdown>;

satahippy avatar Oct 12 '17 08:10 satahippy

\`\`\`js - it's a little bit ugly, but you need to escape ` characters...

satahippy avatar Oct 12 '17 08:10 satahippy

Thanks @satahippy I much appreciate your help

cbroberg avatar Nov 13 '17 18:11 cbroberg

why it doesn't work for me ?

import React from 'react';
import Markdown from 'react-remarkable';
import hljs from 'highlight.js';
import 'highlight.js/styles/github-gist.css';

const highlight = (str, lang) => {
    if (lang && hljs.getLanguage(lang)) {
        try {
            return hljs.highlight(lang, str).value;
        } catch (err) {
          console.error(err);
        }
    }

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

    return '';
};

const MarkdownWithJs = () =>
  
  <Markdown options={{highlight}}>{`
  # Header

  \`\`\`js
  const func = () => {
    // here is your js code
  }
  \`\`\`
  `}</Markdown>;

firstpersoncode avatar Feb 05 '19 20:02 firstpersoncode