Skip to content

Commit b344065

Browse files
author
Leandro Balmaceda
committed
Add option to overwrite the default prefilter for links
1 parent a92f0a1 commit b344065

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

lib/configs/commonmark.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ module.exports = {
1212
linkify: false, // autoconvert URL-like texts to links
1313
linkTarget: '', // set target to open link in
1414

15+
// linkifyPrefilter. Function that overwrite the default link prefilter,
16+
// specified by this regex: /www|@|\:\/\//
17+
//
18+
// @param { string } link that will be tested.
19+
// @returns { Boolean } whether or not the link passes the prefilter.
20+
linkifyPrefilter: null,
21+
1522
// Enable some language-neutral replacements + quotes beautification
1623
typographer: false,
1724

lib/configs/default.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ module.exports = {
1212
linkify: false, // autoconvert URL-like texts to links
1313
linkTarget: '', // set target to open link in
1414

15+
// linkifyPrefilter. Function that overwrite the default link prefilter,
16+
// specified by this regex: /www|@|\:\/\//
17+
//
18+
// @param { string } link that will be tested.
19+
// @returns { Boolean } whether or not the link passes the prefilter.
20+
linkifyPrefilter: null,
21+
1522
// Enable some language-neutral replacements + quotes beautification
1623
typographer: false,
1724

lib/configs/full.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ module.exports = {
1212
linkify: false, // autoconvert URL-like texts to links
1313
linkTarget: '', // set target to open link in
1414

15+
// linkifyPrefilter. Function that overwrite the default link prefilter,
16+
// specified by this regex: /www|@|\:\/\//
17+
//
18+
// @param { string } link that will be tested.
19+
// @returns { Boolean } whether or not the link passes the prefilter.
20+
linkifyPrefilter: null,
21+
1522
// Enable some language-neutral replacements + quotes beautification
1623
typographer: false,
1724

lib/rules_core/linkify.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ module.exports = function linkify(state) {
9595
}
9696
if (htmlLinkLevel > 0) { continue; }
9797

98-
if (token.type === 'text' && LINK_SCAN_RE.test(token.content)) {
98+
var linkifyPrefilter = state.options.linkifyPrefilter;
99+
var passPrefilter = LINK_SCAN_RE.test(token.content);
100+
if (linkifyPrefilter && typeof linkifyPrefilter === 'function') {
101+
// Use the custom prefilter for links.
102+
passPrefilter = linkifyPrefilter(token.content);
103+
}
104+
105+
if (token.type === 'text' && passPrefilter) {
99106

100107
// Init linkifier in lazy manner, only if required.
101108
if (!linkifier) {

test/misc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ describe('API', function () {
105105
assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\n');
106106
});
107107

108+
it('Linkify prefilter', function () {
109+
var md = new Remarkable({
110+
linkify: true,
111+
linkifyPrefilter: function (str) {
112+
// Only linkify if link starts with https://
113+
return /^https?:\/\//.test(str);
114+
}
115+
});
116+
117+
assert.strictEqual(md.render('https://www.google.com'),
118+
'<p><a href="https://www.google.com">https://www.google.com</a></p>\n');
119+
assert.strictEqual(md.render('www.google.com'), '<p>www.google.com</p>\n');
120+
});
121+
108122
it('force hardbreaks', function () {
109123
var md = new Remarkable({ breaks: true });
110124

0 commit comments

Comments
 (0)