question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot get rule to work - need examples - please!

See original GitHub issue

Hi, I am trying to write a new rule to allow ‘[ ]’ sequences to be parsed and replaced with a box character (using FontAwesome). I have the following in my code:

<MarkdownView
    styles={{
        untickedCheckbox: {
          fontFamily: 'FontAwesome'
        }
      }}
    rules={{
        untickedCheckbox: {
            match: function(source, state, lookbehind) {
                return /^\[([\s]+?)\](?!\])/.exec(source);
            },
            parse: function(capture, recurseParse, state) {
                return {
                    content: [],
                };
            },
            render: (node, output, state, styles) => {
                <Text style={{fontFamily: 'FontAwesome'}}>&#xf096;</Text>
            }
        },
    }}
>{value}</MarkdownView>

My parse function doesn’t need to do anything since the whole of the matched sequence is replaced with a react-native <Text> component. This is my input markdown:

[ ] This is an empty checkbox

All I get back is:

This is an empty checkbox

How can I get the <Text> component to appear?

Any examples on how to add a rule would be most helpful - I’ve gone through the simple-markdown code but can’t get this to work. In fact, any help would be most appreciated.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
zzorbacommented, Mar 3, 2018

Okay, after what took way too long reverse engineering the library, I managed to get this to work with some custom rules… Caveat: the tags I was trying to parse out are leaf tags, a node about how I believe output should be used is at the end, but I haven’t tested it.

Here is a sample rule definition just as an example to read a bold/italic html tag. This worked for me, but who knows if its the ‘best’ way or not.

const BoldHtmlTagRule = {
  match: SimpleMarkdown.anyScopeRegex(new RegExp('^<b>(.+?)<\\/b>')),
  order: 1,
  quality: () => 100,
  parse: (capture, nestedParse, state) => {
    return { text: capture[1] };
  },
  render: (node, output, state) => {
    return (
      <Text key={state.key} style={styles.boldText}>
        { node.text }
      </Text>
    );
  },
};
const ItalicHtmlTagRule = {
  match: SimpleMarkdown.anyScopeRegex(new RegExp('^<i>(.+?)<\\/i>')),
  order: 1,
  quality: () => 100,
  parse: (capture, nestedParse, state) => {
    return { text: capture[1] };
  },
  render: (node, output, state) => {
    return (
      <Text key={state.key} style={styles.italicText}>
        { node.text }
      </Text>
    );
  },
};

And here is how I invoke the MarkdownView component.

<MarkdownView
  rules={{
    bTag: BoldHtmlTagRule,
    iTag: ItalicHtmlTagRule,
  }}
>
  { this.props.text }
</MarkdownView>

This works, with one caveat – it produces a warning about the order key failing a propType match. Order controls when the rule gets applied, and the simple-markdown library seems to need it. Lower means it runs first, so by putting in 1 my rules run first (what I want, but maybe not what you want).

Without the undocumented order key it fails, but if you put it in it complains that its not a function. If you make it a function it fails. I can’t pinpoint where the validation error comes from, so I just ignored it.

Now, if the custom rule you are trying to implement wraps nested content, you want to return a tag and then call output(node.nestedTextYouParsedOut, state) to ensure the tree keeps getting built. I didn’t have to do this for my simple use case.

Hope this helps.

0reactions
mmckinley8commented, Sep 7, 2022

I’m also having this issue, I can’t even get any of the parameters to print in the render function

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix It When Outlook Rules Are Not Working - Lifewire
Choose a condition and an action to take and select OK. To make email rules in Outlook.com, go to Settings > View all...
Read more >
How to Fix Outlook Rules Not Working in Windows 10
There are several different reasons why your Outlook rules aren't working. For example, some settings prevent Outlook from properly carrying out rules.
Read more >
Some rules are disabled and can't be enabled - Outlook
Click Manage Rules and Alerts. On the E-mail Rules tab, click the rule that you want to rename. Click Change Rules, and then...
Read more >
How to Create Rules in Outlook - YouTube
In this step-by-step tutorial, learn how to use Rules in Microsoft Outlook. Rules allow you to move, flag, and respond to email messages ......
Read more >
How To Fix Outlook 2013 Rules in Error - YouTube
In this video, I will show you guys how to fix Microsoft Outlook rules in error showing " can't move to specified folder'....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found