Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to overwrite the default prefilter for links #247

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
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
14 changes: 14 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ describe('API', function () {
assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\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'),
'<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