improve regex locator for urls and mentions
This commit is contained in:
parent
d9882143bb
commit
2ceea79236
1 changed files with 9 additions and 21 deletions
|
@ -1,37 +1,33 @@
|
||||||
import { parseURI } from 'lbry-redux';
|
import { parseURI } from 'lbry-redux';
|
||||||
import visit from 'unist-util-visit';
|
import visit from 'unist-util-visit';
|
||||||
import wordCharacter from 'is-word-character';
|
|
||||||
|
|
||||||
const protocol = 'lbry://';
|
const protocol = 'lbry://';
|
||||||
const uriRegex = /^(lbry:\/\/)[^\s]*/g;
|
const uriRegex = /(lbry:\/\/)[^\s()"]*/g;
|
||||||
|
|
||||||
const mentionToken = '@';
|
const mentionToken = '@';
|
||||||
const mentionTokenCode = 64; // @
|
const mentionTokenCode = 64; // @
|
||||||
const mentionRegex = /^@[^\s()]*/gm;
|
const mentionRegex = /@[^\s()"]*/gm;
|
||||||
|
|
||||||
function invalidChar(char) {
|
const invalidRegex = /[-_.+=?!@#$%^&*:;,{}<>\w/\\]/;
|
||||||
const dot = 46; // '.'
|
|
||||||
const dash = 45; // '-'
|
|
||||||
const slash = 47; // '/'
|
|
||||||
|
|
||||||
return char === dot || char === dash || char === slash || char === mentionTokenCode || wordCharacter(char);
|
// Find channel mention
|
||||||
}
|
|
||||||
|
|
||||||
// Find a possible mention.
|
|
||||||
function locateMention(value, fromIndex) {
|
function locateMention(value, fromIndex) {
|
||||||
var index = value.indexOf(mentionToken, fromIndex);
|
var index = value.indexOf(mentionToken, fromIndex);
|
||||||
|
|
||||||
if (index !== -1 && invalidChar(value.charCodeAt(index - 1))) {
|
// skip intervalid mention
|
||||||
|
if (index > 0 && invalidRegex.test(value.charAt(index - 1))) {
|
||||||
return locateMention(value, index + 1);
|
return locateMention(value, index + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find claim url
|
||||||
function locateURI(value, fromIndex) {
|
function locateURI(value, fromIndex) {
|
||||||
var index = value.indexOf(protocol, fromIndex);
|
var index = value.indexOf(protocol, fromIndex);
|
||||||
|
|
||||||
if (index !== -1 && invalidChar(value.charCodeAt(index - 1))) {
|
// Skip invalid uri
|
||||||
|
if (index > 0 && invalidRegex.test(value.charAt(index - 1))) {
|
||||||
return locateMention(value, index + 1);
|
return locateMention(value, index + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,10 +73,6 @@ function tokenizeMention(eat, value, silent) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.charCodeAt(0) !== mentionTokenCode) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const match = value.match(mentionRegex);
|
const match = value.match(mentionRegex);
|
||||||
|
|
||||||
return validateURI(match, eat, self);
|
return validateURI(match, eat, self);
|
||||||
|
@ -92,10 +84,6 @@ function tokenizeURI(eat, value, silent) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!value.startsWith(protocol)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const match = value.match(uriRegex);
|
const match = value.match(uriRegex);
|
||||||
|
|
||||||
return validateURI(match, eat);
|
return validateURI(match, eat);
|
||||||
|
|
Loading…
Add table
Reference in a new issue