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 markdown tags #454

Open
asimaranov opened this issue Nov 7, 2022 · 5 comments
Open

Custom markdown tags #454

asimaranov opened this issue Nov 7, 2022 · 5 comments

Comments

@asimaranov
Copy link

asimaranov commented Nov 7, 2022

Hello there. Are there ways to add custom markdown tags? For example I want to make text between $ signs to be rendered with my style. So $text$ in markdown should become <mytag>text</mytag> in the result html

@jaywcjlove
Copy link
Member

jaywcjlove commented Nov 7, 2022

/**
* This is reset [react-markdown](https://github.com/rexxars/react-markdown) settings.
*/
previewOptions?: Omit<MarkdownPreviewProps, 'source'>;

@asimaranov This is possible. You need previewOptions to write a rehype plugin or use rehype-rewrite

<MDEditor 
  previewOptions={{
    remarkPlugins: [],
    rehypePlugins: []
  }}
/>

@asimaranov
Copy link
Author

asimaranov commented Nov 7, 2022

Thanks, but I have problems with tags inside my new tag. I wrote the following retype plugin

const merge = (str: string) => {
  return str.replace(/\$([^$]*)\$/g, function(string, first){
    return "█".repeat(first.length);
  })
};

const rewrite = (node: any, index: any, parent: any) => {
  console.log(node)
  if (node.type === "text") {
    node.value = merge(node.value);
  }
};

It works as expected for simple text
image
But if there'are inner tags, it fails
image

How can I fix it?

@jaywcjlove
Copy link
Member

@asimaranov like this? https://codesandbox.io/embed/markdown-editor-for-react-uiwjs-react-md-editor-issues-454-5wb792?fontsize=14&hidenavigation=1&theme=dark

image

import React from "react";
import ReactDOM from "react-dom";
import MDEditor from "@uiw/react-md-editor";
import rehypeRewrite, { getCodeString } from "rehype-rewrite";
// No import is required in the WebPack.
// import "@uiw/react-md-editor/dist/markdown-editor.css";

const mkdStr = `
Markdown Editor

Editor $good$ 

Editor $good \`google\` $
`;

const REG = /\$(.*?)\$/gi;

function App() {
  const [value, setValue] = React.useState(mkdStr);
  return (
    <div className="container">
      <h3>Auto</h3>
      <MDEditor
        height={200}
        previewOptions={{
          rehypePlugins: [
            [
              rehypeRewrite,
              {
                rewrite: (node, index, parent) => {
                  if (node.type === "element" && node.tagName === "p") {
                    let text = getCodeString(node.children);
                    if (REG.test(text)) {
                      text = text.replace(REG, (txt) => {
                        return "█".repeat(txt.length);
                      });
                      node.children = [
                        {
                          type: "text",
                          value: text
                        }
                      ];
                    }
                  }
                }
              }
            ]
          ]
        }}
        value={value}
        onChange={setValue}
      />
    </div>
  );
}

@asimaranov
Copy link
Author

Wow, thanks, you're my hero

@jstiburek
Copy link

@jaywcjlove

Hello,
I have kinda similar use case.

Is there way to catch a char in actual writing with rehypeRewrite ?
I want to detect when user write @ and show him options to select users to tag. Then replace text behing @ with selected option.

Thank you

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

3 participants