Skip to content

Commit

Permalink
Prevent emoji in url (#25)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dkeza authored May 14, 2020
1 parent 0f60483 commit 3f07262
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/rules/link.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const LINK_REGEX = /\[(.+?)\]\(((?:(http[s]?|ftp):\/{2})?[\w\/\-+?#=.:;!%&]+)\)/g

const LINK_REGEX = /\[(.+?)\]\(((?:(?:http[s]?|ftp):\/{2})?)([\w\/\-+?#=.:;!%&]+)\)/g
export class Link {
static get RULE_NAME () { return 'link' }

static parse (source) {
return source.replace(LINK_REGEX, '<a href="$2" target="_blank">$1</a>')
return source.replace(LINK_REGEX, (match, linkName, urlProtocolDomain, urlPath) => {
const url = urlProtocolDomain.trim() + urlPath.trim().replace(/:/g, '%3A')

return `<a href="${url}" target="_blank">${linkName}</a>`
})
}
}
8 changes: 3 additions & 5 deletions src/rules/linkify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const LINK_REGEX = /(^|\s|>)((?:http(?:s)?:\/\/.)(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:;%_\+.~#?!&//=]*))/g

const LINK_REGEX = /(^|\s|>)((?:http(?:s)?:\/\/.)(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})\b([-a-zA-Z0-9@:;%_\+.~#?!&//=]*)/g
export class Linkify {
static get RULE_NAME () { return 'linkify' }

static parse (source) {
return source.replace(LINK_REGEX, (all, before, url) => {
url = url.trim()

return source.replace(LINK_REGEX, (all, before, urlProtocolDomain, urlPath) => {
const url = urlProtocolDomain.trim() + urlPath.trim().replace(/:/g, '%3A')
const href = url.substr(0, 4) !== 'http' ? `http://${url}` : url

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

0 comments on commit 3f07262

Please sign in to comment.