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

Link: Support inputRules #337

Closed
michalsnik opened this issue Jun 3, 2019 · 7 comments
Closed

Link: Support inputRules #337

michalsnik opened this issue Jun 3, 2019 · 7 comments
Labels
Type: Feature The issue or pullrequest is a new feature

Comments

@michalsnik
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently Link node only supports pasting, that is when I paste link in the editor I get a nicely formatted tag. It doesn't however work if I try to write the link myself, either directly or by using Markdown syntax.

Describe the solution you'd like
I would like to see two inputRules added in Link node:

  • one that transforms typed URL automatically
  • second that transforms markdown syntax to Link node ([Example link](http://google.com))

Additional context
I tried to implement it myself in the project I'm working on, however it's not entirely clear for me if it's doable without additional changes in the underlying logic. Hence I'm asking for your help.

The first inputRule that I considered is as follows:

const LINK_NODE_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;

// ...

inputRules({ type }) {
    return [
      markInputRule(LINK_NODE_REGEX, type, match => {
          return { src: match[0] };
      }),
      // second rule
    ]
}

I used markInputRule in order to just add mark around the matched text. This partially works, however after typing URL that matches the REGEX, I can't type more text - it immediately transforms the text to link and prevents me from finishing typing the proper URL. Adding white space at the end of the regex results in similar behavior. Do I miss something here?

For the second inputRule, I would use nodeInputRule to transform [Text](http://google.com) into Link node, however I don't see how this method would set innerText on the Link node. Is there a more proper method for achieving desired effect?

I'm not expecting you implementing all of it, guidance would be more than enough. I'm happy to contribute and help making this project even better :)

@MrBuggy
Copy link

MrBuggy commented Jun 21, 2019

Hi @michalsnik
Can you show us the whole code (also commands, schema etc.) and if possible the markup.
Cheers.

@rdlh
Copy link

rdlh commented Sep 10, 2019

Hi @michalsnik,

Could you please try with this regex:

/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)(\s|\n)$/g

The ending $ is doing the trick in our own app code.

@dougalg
Copy link

dougalg commented Jun 3, 2020

For anyone else who comes here, I was able to get it to work with:

/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-zA-Z]{2,}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)[\s\n]$/g

It seems like the final matching group must be empty, otherwise it will use the length of that group as the length of text to mark.

@hanspagel
Copy link
Contributor

Thanks for sharing @rdlh and @dougalg! I’m closing this here for now, but plan to add this as an example to the new documentation. 🙌

@dougalg
Copy link

dougalg commented Jan 7, 2021

Thanks, @hanspagel ! Really looking forward to TipTap 2!

@07akioni
Copy link

07akioni commented Nov 8, 2021

Thanks for sharing @rdlh and @dougalg! I’m closing this here for now, but plan to add this as an example to the new documentation. 🙌

Has it been done?

@philippkuehn
Copy link
Contributor

@07akioni no, but you can follow up here #1209

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature The issue or pullrequest is a new feature
Projects
None yet
Development

No branches or pull requests

7 participants