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

Fixing/improving linkRegExp #453

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,8 @@ This is useful when the document needs to be changed programmatically, but those

### linkRegExp

This is the regular expression used to automatically mark up links when inserting HTML or after pressing space. You can change it if you want to use a custom regular expression for detecting links, or set to `/[]/` to turn off link detection.
This is the regular expression used to automatically mark up links when inserting HTML or after pressing space. You can change it if you want to use a custom regular expression for detecting links, or set to `/[]/` to turn off link detection. To append to the existing regex, set it to `linkRegExp.source + '|' + newLinkRegExp`. For compatibility with linkRegExpHandlers use named capture groups (`?<name>`).

### linkRegExpHandlers

This is a map of handlers for different types of matches in linkRegExp. For example, linkRegExp has a named group 'url' that matches urls, and a named group 'email' that matches emails. linkRegExpHandler['url'] and linkRegExpHandler['email'] are functions that take in the matching string and returns what the link should have in its 'href'.
21 changes: 12 additions & 9 deletions source/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ class Squire {
// Only look on boundaries
'\\b(?:' +
// Capture group 1: URLs
'(' +
'(?<url>' +
// Add links to URLS
// Starts with:
'(?:' +
Expand Down Expand Up @@ -1775,7 +1775,7 @@ class Squire {
'\\([^\\s()<>]+\\)' +
')' +
// Capture group 2: Emails
')|(' +
')|(?<email>' +
// Add links to emails
'[\\w\\-.%+]+@(?:[\\w\\-]+\\.)+[a-z]{2,}\\b' +
// Allow query parameters in the mailto: style
Expand All @@ -1788,8 +1788,12 @@ class Squire {
);
*/
linkRegExp =
/\b(?:((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.\-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;

/\b(?:(?<url>(?:(?:[a-z+]+:)?\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.\-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|(?<email>[\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;
linkRegExpHandlers = {
'url': (m) => {return /^(?:[a-z+]+:)?\/\//i.test(m)
? m : 'https://' + m},
'email': (m) => {return 'mailto:' + m},
'default': (m) => {return m}};
addDetectedLinks(
searchInNode: DocumentFragment | Node,
root?: DocumentFragment | HTMLElement,
Expand All @@ -1815,15 +1819,14 @@ class Squire {
node,
);
}
let handler = Object.keys(this.linkRegExpHandlers).filter(
key => Object.keys(match.groups).includes(key) && match.groups[key])[0]
|| 'default';
const child = createElement(
'A',
Object.assign(
{
href: match[1]
? /^(?:ht|f)tps?:/i.test(match[1])
? match[1]
: 'http://' + match[1]
: 'mailto:' + match[0],
href: this.linkRegExpHandlers[handler](match[0])
},
defaultAttributes,
),
Expand Down
11 changes: 8 additions & 3 deletions test/squire.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ describe('Squire RTE', () => {
'https://google.com': 'https://google.com/',
'https://www.google.com': 'https://www.google.com/',
'https://www.google.com/': 'https://www.google.com/',
'https://google.com/?': 'https://google.com/',
'https://google.com?': 'https://google.com/',
'https://google.com?a': 'https://google.com/?a',
'HTTPS://google.com/?': 'https://google.com/', // Test protocol matching
'ftp://google.com?': 'ftp://google.com/', // Test protocol matching
'redis://google.com?a': 'redis://google.com?a', // Test protocol matching
'https://google.com?a=': 'https://google.com/?a=',
'https://google.com?a=b': 'https://google.com/?a=b',
'https://google.com?a=b?': 'https://google.com/?a=b',
Expand All @@ -486,10 +486,15 @@ describe('Squire RTE', () => {
'https://google.com?a=b&c=d&': 'https://google.com/?a=b&c=d',
'https://google.com?a=b&c=d&e=': 'https://google.com/?a=b&c=d&e=',
'https://google.com?a=b&c=d&e=f': 'https://google.com/?a=b&c=d&e=f',
'www.google.com': 'https://www.google.com/', // Test prepending protocol
'foobar': 'http://localhost/foobar', // Test default handler
'search': 'http://localhost/replace', // Test custom handler
};

Object.keys(LINK_MAP).forEach((input) => {
it('should auto convert links to anchor: ' + input, () => {
editor.linkRegExp = new RegExp(editor.linkRegExp.source + "|(foobar)|(?<extra>search)", editor.linkRegExp.flags);
editor.linkRegExpHandlers['extra'] = (m) => {return 'replace'};
editor.insertHTML(input);
const link = document.querySelector('a')!;
expect(link.href).toBe(LINK_MAP[input]);
Expand Down