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

CodeBlock issues #267

Open
davide-scalzo opened this issue Aug 30, 2019 · 9 comments
Open

CodeBlock issues #267

davide-scalzo opened this issue Aug 30, 2019 · 9 comments

Comments

@davide-scalzo
Copy link

davide-scalzo commented Aug 30, 2019

Hi there, awesome light little library :)

I've identified a few issues with code blocks, they are as follows:

  • overrides code does not differentiate between the 3 code types internally, should be able to render differently based on the type of code node
  • code blocks with 3 tick mark render as inlineCode if not preceded by two new lines
@BenBrewerBowman
Copy link

BenBrewerBowman commented Sep 23, 2019

I am also running into this issue. I am not able to render a custom component for javascript vs python vs unspecified codeblocks.

@qkzzxb
Copy link

qkzzxb commented Oct 16, 2019

You can change index.js in 771 line

  : `${input.replace(TRIM_NEWLINES_AND_TRAILING_WHITESPACE_R, '')}\n`,

But I don't know what else will happen.

@ErikAGriffin
Copy link

ErikAGriffin commented Oct 16, 2019

I'm also having the issue whereby a <Markdown> tag that is conditionally rendered later does not add the class="hljs script-type" to the <code> blocks and also not include the individual <span> elements to highlight words.

For example, if using a tab system and the following:

{ aboutTab === activeTab && (
    <article className={styles.detailContent}>
      <Markdown className={styles.markdown}>{myMarkdown}</Markdown>
    </article>
)}

If the aboutTab === activeTab does not equal true on the initial page load, any codeblocks in that markdown are rendered like this:

<code>
my Code
    indented
    but all white
end
</code>

instead of this:

<code class="hljs angelscript">
my <span class="hljs-keyword">Code</span>
    indented
    <span class="hljs-keyword">but</span> all white
<span class="hljs-keyword">end</span>
</code>

The current workaround I've found is to use a class with display: none; to conditionally switch content vs. the use of jsx {condition && (<jsx>)}

@akemrir
Copy link

akemrir commented Oct 29, 2019

Same here. But I am rendering this markdown element on text area change, and it does not render code block properly.

@FerusAndBeyond
Copy link

Is this going to be fixed? This is quite a big issue for those that want to have custom code blocks.

@CodyCline
Copy link

CodyCline commented Jul 28, 2020

One workaround is to create your code block with a highlighter of your choice (I used prismjs) and add it to the overrides as a custom component. I did this and it a allowed me to have a custom code blocks and an inline code that work nicely together.

Within markdown, All I had to do was call the codeblock like this <MyCodeBlock> and use inline code like this `code` . This may not be the best solution for everyone, but it suits my needs very well for the moment.

Implementation below:

import React from 'react';
import Markdown from 'markdown-to-jsx';
import { CodeBlock, InlineCode } from 'dir/to/your/component';

//Test string
const testMD = (`
#Title
This is some inline \`code\` to view.

<MyCodeBlock language="javascript">
const x = "myCodeBlock";
</MyCodeBlock
`);

<Markdown
  children={testMD} //Or other prop
  options={{
    overrides: { 
    code: InlineCode
    MyCodeBlock: CodeBlock,

    }
  }}
/>

@trulyronak
Copy link

Bump on this issue — markdown still renders CodeBlock content for inline code elements

@resilva
Copy link

resilva commented Feb 17, 2021

What about propagating the code type as a props ?

It could be used with something (not tested :)) like that :

<Markdown
  children={...}
  options={{
    overrides: { 
        code: CustomCode
    }
  }}
/>

const CustomCode = (props: CustomCodeProps): React.ReactElement => {
    const { children, type }  props;

    // handle inline block
    if (type == 'inline') {
        return <span>{children}</span>
    }

    // Handle block code, with highlight for example
};

CustomCode.defaultProps = {
    type: 'block',
};

export type CodeProps = {
    children: ReactNode;
    type?: string;
};

I think it could be implemented with something like that, but I'm not accommodated with the lib code so it's probably not enough :

diff --git a/index.tsx b/index.tsx
index 9dae694..4a0ca12 100644
--- a/index.tsx
+++ b/index.tsx
@@ -1110,7 +1110,7 @@ export function compiler(
       react(node, output, state) {
         return (
           <pre key={state.key}>
-            <code className={node.lang ? `lang-${node.lang}` : ''}>
+            <code type={node.type ? node.type : 'block'} className={node.lang ? `lang-${node.lang}` : ''}>
               {node.content}
             </code>
           </pre>
@@ -1139,7 +1139,7 @@ export function compiler(
         }
       },
       react(node, output, state) {
-        return <code key={state.key}>{node.content}</code>
+        return <code key={state.key} type='inline'>{node.content}</code>
       },
     } as MarkdownToJSX.Rule<{ content: string }>,

It probably miss some things to be good but it describes the idea.

@eldos-dl
Copy link

eldos-dl commented Jul 3, 2021

  • code blocks with 3 tick mark render as inlineCode if not preceded by two new lines

This issue means the library is not markdown compliant right?

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

10 participants