Skip to content

Commit 3f07262

Browse files
authored
Prevent emoji in url (#25)
* Link regex rule update (#20) * Updated the link regex rule to allow ; in url * Link regex rule update (#20) * Updated the link regex rule to allow ; in url * To remove emoji : x : from url, replace : with urlencoded version * Change parameter name * Remove unused group from regex
1 parent 0f60483 commit 3f07262

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/rules/link.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const LINK_REGEX = /\[(.+?)\]\(((?:(http[s]?|ftp):\/{2})?[\w\/\-+?#=.:;!%&]+)\)/g
2-
1+
const LINK_REGEX = /\[(.+?)\]\(((?:(?:http[s]?|ftp):\/{2})?)([\w\/\-+?#=.:;!%&]+)\)/g
32
export class Link {
43
static get RULE_NAME () { return 'link' }
54

65
static parse (source) {
7-
return source.replace(LINK_REGEX, '<a href="$2" target="_blank">$1</a>')
6+
return source.replace(LINK_REGEX, (match, linkName, urlProtocolDomain, urlPath) => {
7+
const url = urlProtocolDomain.trim() + urlPath.trim().replace(/:/g, '%3A')
8+
9+
return `<a href="${url}" target="_blank">${linkName}</a>`
10+
})
811
}
912
}

src/rules/linkify.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const LINK_REGEX = /(^|\s|>)((?:http(?:s)?:\/\/.)(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:;%_\+.~#?!&//=]*))/g
2-
1+
const LINK_REGEX = /(^|\s|>)((?:http(?:s)?:\/\/.)(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})\b([-a-zA-Z0-9@:;%_\+.~#?!&//=]*)/g
32
export class Linkify {
43
static get RULE_NAME () { return 'linkify' }
54

65
static parse (source) {
7-
return source.replace(LINK_REGEX, (all, before, url) => {
8-
url = url.trim()
9-
6+
return source.replace(LINK_REGEX, (all, before, urlProtocolDomain, urlPath) => {
7+
const url = urlProtocolDomain.trim() + urlPath.trim().replace(/:/g, '%3A')
108
const href = url.substr(0, 4) !== 'http' ? `http://${url}` : url
119

1210
return `${before}<a href="${href}" target="_blank">${url}</a>`

0 commit comments

Comments
 (0)