Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling nested code block (triple backtick inside codeblock) #493

Open
rizqyhbb opened this issue May 2, 2023 · 2 comments
Open

Handling nested code block (triple backtick inside codeblock) #493

rizqyhbb opened this issue May 2, 2023 · 2 comments

Comments

@rizqyhbb
Copy link

rizqyhbb commented May 2, 2023

Information

  1. version: 7.2.0
  2. I'm also using react-syntax-highlighter
  3. Framework: remix

I'm facing issue where i have code block like this:

const text = 'this is just an example\n```javascript\nconsole.log(`hello`)\n```'

as you can see that github mark down make it good code block, but with markdown-to-jsx this is what i get
image

@espressom
Copy link

espressom commented May 18, 2023

const CodeBlock = ({ children }: { children: React.ReactElement }) => {
  const { className, children: code } = children.props;

  const language = className?.replace("lang-", "");
  return (
    <SyntaxHighlighter language={language}>
      {code}
    </SyntaxHighlighter>
  );
};
import Markdown from "markdown-to-jsx";

<Markdown
    options={{
        forceBlock: true,
        overrides: { pre: { component: CodeBlock } }
    }}>
     {content}
</Markdown>

In this way, code blocks will be rendered properly. (It works for my project)

@rizqyhbb
Copy link
Author

const CodeBlock = ({ children }: { children: React.ReactElement }) => {
  const { className, children: code } = children.props;

  const language = className?.replace("lang-", "");
  return (
    <SyntaxHighlighter language={language}>
      {code}
    </SyntaxHighlighter>
  );
};
import Markdown from "markdown-to-jsx";

<Markdown
    options={{
        forceBlock: true,
        overrides: { pre: { component: CodeBlock } }
    }}>
     {content}
</Markdown>

In this way, code blocks will be rendered properly. (It works for my project)

Thanks for your response, It still not give me a good result. Can you make sure that your code block is triple backtick (i.e ```javascript) not quad (i.e ````javascript).

I know that four backticks will solve the problem, somehow I don't have control to the response that I get from OpenAI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants