From 2ceea79236f2ff310957d4d6d006a8289cbd0792 Mon Sep 17 00:00:00 2001 From: Baltazar Gomez Date: Sat, 12 Oct 2019 16:03:31 -0600 Subject: [PATCH] improve regex locator for urls and mentions --- src/ui/util/remark-lbry.js | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/ui/util/remark-lbry.js b/src/ui/util/remark-lbry.js index 1414fde2c..4ab1e4229 100644 --- a/src/ui/util/remark-lbry.js +++ b/src/ui/util/remark-lbry.js @@ -1,37 +1,33 @@ import { parseURI } from 'lbry-redux'; import visit from 'unist-util-visit'; -import wordCharacter from 'is-word-character'; const protocol = 'lbry://'; -const uriRegex = /^(lbry:\/\/)[^\s]*/g; +const uriRegex = /(lbry:\/\/)[^\s()"]*/g; const mentionToken = '@'; const mentionTokenCode = 64; // @ -const mentionRegex = /^@[^\s()]*/gm; +const mentionRegex = /@[^\s()"]*/gm; -function invalidChar(char) { - const dot = 46; // '.' - const dash = 45; // '-' - const slash = 47; // '/' +const invalidRegex = /[-_.+=?!@#$%^&*:;,{}<>\w/\\]/; - return char === dot || char === dash || char === slash || char === mentionTokenCode || wordCharacter(char); -} - -// Find a possible mention. +// Find channel mention function locateMention(value, 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 index; } +// Find claim url function locateURI(value, 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); } @@ -77,10 +73,6 @@ function tokenizeMention(eat, value, silent) { return true; } - if (value.charCodeAt(0) !== mentionTokenCode) { - return; - } - const match = value.match(mentionRegex); return validateURI(match, eat, self); @@ -92,10 +84,6 @@ function tokenizeURI(eat, value, silent) { return true; } - if (!value.startsWith(protocol)) { - return; - } - const match = value.match(uriRegex); return validateURI(match, eat);