markdown-it / linkify-it

Links recognition library with full unicode support
http://markdown-it.github.io/linkify-it/
MIT License
655 stars 63 forks source link

How to return 0 in the validate function ? #107

Closed Ranhiru closed 1 year ago

Ranhiru commented 1 year ago

Hello!

I am attempting to match some particular words and trying to linkify them.

See example below.

linkify.add('nda agreement', {
  validate: function (text, pos, self) {
    return 1;
  },
  normalize: function (match) {
    match.url = 'https://www.my-website.com/real-nda-agreement-link';
  }
});

linkify.add('other agreement', {
  validate: function (text, pos, self) {
    return 1;
  },
  normalize: function (match) {
    match.url = 'https://www.my-website.com/real-other-agreement-link';
  }
});

const text = `I, [name of user], agree to the NDA agreement & other agreement.`

console.log(linkify.match(text));

this returns

[
  Match {
    schema: 'nda agreement',
    index: 32,
    lastIndex: 46,
    raw: 'NDA agreement ',
    text: 'NDA agreement ',
    url: 'https://www.my-website.com/real-nda-agreement-link'
  },
  Match {
    schema: 'other agreement',
    index: 48,
    lastIndex: 64,
    raw: 'other agreement.',
    text: 'other agreement.',
    url: 'https://www.my-website.com/real-other-agreement-link'
  }
]

the issue I'm trying to fix is that if that if I return 0 from the validate function, the match is not included in the list of matchers.

How do I tell the validate function to stop checking tail after link prefix ?

puzrin commented 1 year ago

This package is not suitable for any text pattern matching. It's intended to match URL-s, those MUST start with protocol.