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

Transform only specific type to an element #82

Open
mickaelzhang opened this issue Feb 5, 2020 · 1 comment
Open

Transform only specific type to an element #82

mickaelzhang opened this issue Feb 5, 2020 · 1 comment

Comments

@mickaelzhang
Copy link

mickaelzhang commented Feb 5, 2020

Hi, I've been working on a component recently where we want to support only a subset of the element, basically only paragraph, strong and link for now.

For example:

This is some **markdown** text. Here a some link, 
just [here](https://www.google.com). And some random text.

- Bullet point 1
- Bullet point 2

Would render the appropriate bold, link and paragraph tag but for the bullet point, I want it to be rendered raw.

Is there a way to do that with the library? I've tried to only use my rules and not spread the default one but I have an error when I do that

const rules = {
  paragraph: {
    ...SimpleMarkdown.defaultRules.paragraph,
    react: (node, output, state) => (
      <Text key={state.key} size="text-100">
        {output(node.content, state)}
      </Text>
    ),
  },
  ...
}
@ariabuckles
Copy link
Owner

You should be able to do that with only your rules and not spreading the default, with one addition: you'll need the text rule to specify what happens to text that isn't matched by another rule (likely where your error was coming from).

const rules = {
  paragraph: {
    ...SimpleMarkdown.defaultRules.paragraph,
    react: (node, output, state) => (
      <Text key={state.key} size="text-100">
        {output(node.content, state)}
      </Text>
    ),
  },

  // ... the other rules you want here

  text: SimpleMarkdown.defaultRules.text,
};

One thing you might run into with the bullet points is that by default markdown treats multiple lines like that as part of a paragraph and ignores the line break.

You could probably change this behaviour by parsing the list and then changing the rendering to output the raw dashes rather than a <li>, or by preprocessing the text to replace all \ns with \n (two spaces followed by a newline) and including the br rule, which turns \n into a <br> element to separate the lines.

(Alternatively, if those don't work, you could change the br rule to match \n: match: anyScopeRegex(/^\n/), and change the text rule to break on newlines: match: anyScopeRegex(/^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff]|\n|\w+:\S|$)/)

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