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

custom syntax and custom components are not working #847

Closed
4 tasks done
fengliner opened this issue Jul 29, 2024 · 3 comments
Closed
4 tasks done

custom syntax and custom components are not working #847

fengliner opened this issue Jul 29, 2024 · 3 comments
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on

Comments

@fengliner
Copy link

Initial checklist

Affected packages and versions

react-markdown@9.0.1

Link to runnable example

No response

Steps to reproduce

I want to add a custom syntax, which is warped by :::tip::: and :::/tip:::,will render by my custom component. But, it is not working. My demo code is blow

function customInfoPlugin() {
  return (tree) => {
    let tipOpen = false;
    let tipNode = null;

    visit(tree, 'text', (node) => {
      const value = node.value.trim();
      if (value === ':::tip:::') {
        if (!tipOpen) {
          tipOpen = true;
          tipNode = { type: 'tip', children: [], value: '' };
          node.value = ''; // Clear the opening tag
        } else {
          throw new Error('Nested tips are not allowed.');
        }
      } else if (value === ':::/tip:::') {
        if (tipOpen) {
          tipOpen = false;
          node.value = ''; // Clear the closing tag
          tree.children[0].children.splice(tree.children.indexOf(node.parent), 1, tipNode);
        } else {
          throw new Error('Closing tip without opening.');
        }
      } else if (tipOpen) {
        tipNode.value = value;
      }
    });
  };
}

<ReactMarkdown
      remarkPlugins={[RemarkBreaks as () => undefined, customInfoPlugin]}
      components={
        {
          tip: (infoProps) => {
            return <div>=====test=====</div>;
          },
        } as any
      }
    >
      {`:::tip:::       
      This is a custom tip!
      :::/tip:::`}
    </ReactMarkdown>

Expected behavior

The content warped by :::tip::: and :::/tip::: will render by tip component.

Actual behavior

It was rendered as text.

Runtime

No response

Package manager

No response

OS

No response

Build and bundle tools

No response

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Jul 29, 2024
@ChristianMurphy
Copy link
Member

Welcome @fengliner! 👋
The markdown doesn't have a tip node type, so it doesn't know what to do with your custom node and skips it.
I think you are looking for data hName and hProperties which influence the HTML output/components.

https://github.com/syntax-tree/mdast-util-to-hast?tab=readme-ov-file#fields-on-nodes

@ChristianMurphy ChristianMurphy closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
@ChristianMurphy ChristianMurphy added the 🙋 no/question This does not need any changes label Jul 29, 2024

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually labels Jul 29, 2024
@wooorm
Copy link
Member

wooorm commented Jul 30, 2024

It looks like you are trying to make your own version of https://github.com/remarkjs/remark-directive. Perhaps you can use that instead btw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

3 participants