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

How to have hashtags render as links? #628

Closed
bardic opened this issue Mar 29, 2024 · 3 comments
Closed

How to have hashtags render as links? #628

bardic opened this issue Mar 29, 2024 · 3 comments

Comments

@bardic
Copy link

bardic commented Mar 29, 2024

Hey all,

Trying to make a something like #tag render as a URL. Here's what I have so far

const REG = /\B(\#[a-zA-Z]+\b)(?!;)/gi;

<MDEditor 
    style={{width: "100%", height: "100%"}} 
    minHeight={500} 
    value={value} 
    onChange={(event) => {
        setValue(event || '');
    }} 
    previewOptions={{
        rehypePlugins: [
            [
            rehypeRewrite,
            {
                rewrite: (node : any, index: any, parent: any) => {
                if (node.type === "element" && node.tagName === "p") {
                    let text = getCodeString(node.children);
                    if (REG.test(text)) {
                        node.children = [
                        {
                            type: 'link',
                            url: 'https://example.com',
                            children: [{type: 'text', value: text} ]
                        }];
                    }
                }}
            },
        ]],
    }}
/>

The regex properly matches the text but instead of the node being replaced with a link, it just doesn't render anything.

Any guidance on what I may be doing wrong would be much appreciated

@bardic
Copy link
Author

bardic commented Mar 29, 2024

@jaywcjlove
Copy link
Member

@bardic

rewrite: (node: any, index: any, parent: any) => {
  if (node.type === "element") {
    let text = getCodeString(node.children);
    if (REG.test(text)) {
      node.tagName = "a";
      node.properties = {
        href: "https://example.com",
      };
    }
  }
},

@bardic bardic closed this as completed Apr 6, 2024
@bardic
Copy link
Author

bardic commented Apr 12, 2024

For completeness sake, here's what I've settled with after some tweaking

rehypeRewrite,
{
  rewrite: (node: any, index: any, parent: any) => {
    if (node.type === "element" && node.tagName != "a") {
      let text = getCodeString(node.children);
      let m;
      let parts: any[] = [];
      if (REG.test(text)) {
        m = text.split(" ") 

        for (let element of m) {
          if (REG.test(element)) {
            parts.push(
              {
                type: "element",
                tagName: "a",
                properties: {
                  href: "https://google.ca",
                },
                children: [{ type: "text", value: " " + element + " " }],
              })
          } else {
            parts.push(
              {
                type: 'element',
                tagName: 'span',
                properties: {},
                children: [
                  { type: 'text', value: element }
                ]
              })
          }
        }
      }
      console.log(parts)
      if (parts.length > 0) {
        node.children = parts;
      }
    }
  },
}

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