Skip to content
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
20 changes: 13 additions & 7 deletions src/rules/link.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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 get RULE_NAME () {
return 'link'
}

static parse (source) {
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>`
})
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>`
}
)
}
}
23 changes: 14 additions & 9 deletions src/rules/linkify.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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, urlProtocolDomain, urlPath) => {
const url = urlProtocolDomain.trim() + urlPath.trim().replace(/:/g, '%3A')
const href = url.substr(0, 4) !== 'http' ? `http://${url}` : url
static get RULE_NAME() {
return "linkify";
}

return `${before}<a href="${href}" target="_blank">${url}</a>`
})
static parse(source) {
return source.replace(
LINK_REGEX,
(all, before, urlProtocolDomain, urlPath) => {
const url = new URL(source);
url.pathname = url.pathname.replace(/:/g, "%3A");
return `${before}<a href="${url.href}" target="_blank">${url.href}</a>`;
}
);
}
}