react-native-markdown-display icon indicating copy to clipboard operation
react-native-markdown-display copied to clipboard

code_block can not styled.

Open SaimumIslam opened this issue 1 year ago • 7 comments

can anyone help please

SaimumIslam avatar Dec 08 '24 18:12 SaimumIslam

facing the same issue

priyanshuvishnoi avatar Dec 22 '24 16:12 priyanshuvishnoi

I am also facing the same issue, Not working even after overriding the rules.

Also marginTop on code_inline is also not wokring. Can anyone suggest why ?

 {!isLoading && (
        <Markdown
          rules={{
            // Override the 'code' rule to render code blocks as plain text
            code_inline: (node, children, parent, styles) => {
              return (
                <View
                  key={node.key}
                  style={{
                    paddingTop: 5,
                    borderRadius: 8,
                    paddingBottom: 2,
                    paddingHorizontal: 10,
                    backgroundColor: Colors.white,
                  }}
                >
                  <MyText>{node.content}</MyText>
                </View>
              );
            },
            // code_block: (node, children, parent, styles) => {
            //   return (
            //     <View
            //       key={node.key}
            //       style={{ padding: 2, backgroundColor: Colors.red, borderRadius: 5 }}
            //     >
            //       <MyText>{node.content}</MyText>
            //       <MyText>{children}</MyText>
            //     </View>
            //   );
            // },
          }}
          style={{
            // styles.answer are also not working in Expo App
            body: { ...styles.answer, color: answerColor },
            heading3: { ...styles.markdownHeading, color: answerColor },

            // Not working in Expo Go, try in development mode
            // code_inline: {
            //   borderRadius: 8,
            //   paddingHorizontal: 5,
            //   paddingTop: 5,
            //   backgroundColor: Colors.white,
            // },
            // code_block: {
            //   color: "red",
            //   fontSize: 30,
            //   borderRadius: 20,
            //   backgroundColor: Colors.green,
            // },
          }}
        >
          {answer}
        </Markdown>
      )}

lvlupharshit avatar Dec 24 '24 13:12 lvlupharshit

Any update ?

johannbuscail avatar Dec 27 '24 13:12 johannbuscail

If you use the debugPrintTree attribute you can actually see the ``` block is rendered as fence not code_block

My solution below, it works.

import SyntaxHighlighter from "react-native-syntax-highlighter";
import { docco } from "react-syntax-highlighter/styles/hljs";

const markdownRules = {
    fence: (node, children, parent, styles, inheritedStyles = {}) => {
      // we trim new lines off the end of code blocks because the parser sends an extra one.
      let { content } = node;

      if (
        typeof node.content === "string" &&
        node.content.charAt(node.content.length - 1) === "\n"
      ) {
        content = node.content.substring(0, node.content.length - 1);
      }

      return (
        <SyntaxHighlighter
          language={node.sourceInfo}
          style={docco}
          highlighter={"hljs"}
        >
          {content}
        </SyntaxHighlighter>
      );
    },
  };

 <Markdown
        debugPrintTree
        rules={markdownRules}

nohzafk avatar Jan 02 '25 13:01 nohzafk

styling the fence worked for me. thanks 😄

priyanshuvishnoi avatar Jan 02 '25 18:01 priyanshuvishnoi

Thanks, This is working for me for big codes ! I also need to apply styles to inline codes like that appear in between the lines. Amy I ask for help in this please :)

style={{
            body: { ...styles.answer, color: answerColor },
            heading3: { ...styles.markdownHeading, color: answerColor },

            // Nothing is working here
            // code_inline: {
            //   borderRadius: 8,
            //   paddingHorizontal: 5,
            //   paddingTop: 5,
            //   backgroundColor: Colors.white,
            // },

// Working
            fence: {
              fontSize: 18,
              borderRadius: 20,
              marginVertical: 15,
              paddingVertical: 20,
              paddingHorizontal: 20,
              backgroundColor: blockColor,
              fontFamily: "Metropolis-Medium",
              color: isLightTheme ? Colors.pureBlack : Colors.white,
            },
          }}

lvlupharshit avatar Jan 03 '25 01:01 lvlupharshit

Thank you for calling out the "fence" solution!

Banditolabs avatar Mar 08 '25 06:03 Banditolabs