Skip to content

Commit

Permalink
Add option to overwrite the default prefilter for links
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Balmaceda committed Apr 19, 2017
1 parent a92f0a1 commit 77ad443
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/configs/commonmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
7 changes: 7 additions & 0 deletions lib/configs/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
7 changes: 7 additions & 0 deletions lib/configs/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
9 changes: 8 additions & 1 deletion lib/rules_core/linkify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ describe('API', function () {
assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\n');
});

it('Linkify prefilter', function () {
var md = new Remarkable({
linkifyPrefilter: function (str) {
// Only linkify if link starts with https://
return /^https?:\/\//;
}
});

assert.strictEqual(md.render('https://www.google.com'), '<p><a href="https://www.google.com">https://www.google.com</a></p>\n');
assert.strictEqual(md.render('www.google.com'), '<p>www.google.com</p>\n');
});

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

Expand Down

0 comments on commit 77ad443

Please sign in to comment.