From b344065debe0bf9dee7c054a01c2925ee99f830e Mon Sep 17 00:00:00 2001 From: Leandro Balmaceda Date: Thu, 1 Dec 2016 14:41:22 -0300 Subject: [PATCH] Add option to overwrite the default prefilter for links --- lib/configs/commonmark.js | 7 +++++++ lib/configs/default.js | 7 +++++++ lib/configs/full.js | 7 +++++++ lib/rules_core/linkify.js | 9 ++++++++- test/misc.js | 14 ++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/configs/commonmark.js b/lib/configs/commonmark.js index 40d8c1d0..a13a7272 100644 --- a/lib/configs/commonmark.js +++ b/lib/configs/commonmark.js @@ -12,6 +12,13 @@ module.exports = { linkify: false, // autoconvert URL-like texts to links linkTarget: '', // set target to open link in + // linkifyPrefilter. Function that overwrite the default link prefilter, + // specified by this regex: /www|@|\:\/\// + // + // @param { string } link that will be tested. + // @returns { Boolean } whether or not the link passes the prefilter. + linkifyPrefilter: null, + // Enable some language-neutral replacements + quotes beautification typographer: false, diff --git a/lib/configs/default.js b/lib/configs/default.js index ae01184b..7dee16bb 100644 --- a/lib/configs/default.js +++ b/lib/configs/default.js @@ -12,6 +12,13 @@ module.exports = { linkify: false, // autoconvert URL-like texts to links linkTarget: '', // set target to open link in + // linkifyPrefilter. Function that overwrite the default link prefilter, + // specified by this regex: /www|@|\:\/\// + // + // @param { string } link that will be tested. + // @returns { Boolean } whether or not the link passes the prefilter. + linkifyPrefilter: null, + // Enable some language-neutral replacements + quotes beautification typographer: false, diff --git a/lib/configs/full.js b/lib/configs/full.js index f1ac24d6..d39c2835 100644 --- a/lib/configs/full.js +++ b/lib/configs/full.js @@ -12,6 +12,13 @@ module.exports = { linkify: false, // autoconvert URL-like texts to links linkTarget: '', // set target to open link in + // linkifyPrefilter. Function that overwrite the default link prefilter, + // specified by this regex: /www|@|\:\/\// + // + // @param { string } link that will be tested. + // @returns { Boolean } whether or not the link passes the prefilter. + linkifyPrefilter: null, + // Enable some language-neutral replacements + quotes beautification typographer: false, diff --git a/lib/rules_core/linkify.js b/lib/rules_core/linkify.js index e9bc88c5..245e2b69 100644 --- a/lib/rules_core/linkify.js +++ b/lib/rules_core/linkify.js @@ -95,7 +95,14 @@ module.exports = function linkify(state) { } if (htmlLinkLevel > 0) { continue; } - if (token.type === 'text' && LINK_SCAN_RE.test(token.content)) { + var linkifyPrefilter = state.options.linkifyPrefilter; + var passPrefilter = LINK_SCAN_RE.test(token.content); + if (linkifyPrefilter && typeof linkifyPrefilter === 'function') { + // Use the custom prefilter for links. + passPrefilter = linkifyPrefilter(token.content); + } + + if (token.type === 'text' && passPrefilter) { // Init linkifier in lazy manner, only if required. if (!linkifier) { diff --git a/test/misc.js b/test/misc.js index 6e7e30ec..541e50b3 100644 --- a/test/misc.js +++ b/test/misc.js @@ -105,6 +105,20 @@ describe('API', function () { assert.strictEqual(md.render('```\n&\n```'), '
&\n
\n'); }); + it('Linkify prefilter', function () { + var md = new Remarkable({ + linkify: true, + linkifyPrefilter: function (str) { + // Only linkify if link starts with https:// + return /^https?:\/\//.test(str); + } + }); + + assert.strictEqual(md.render('https://www.google.com'), + '

https://www.google.com

\n'); + assert.strictEqual(md.render('www.google.com'), '

www.google.com

\n'); + }); + it('force hardbreaks', function () { var md = new Remarkable({ breaks: true });