File tree Expand file tree Collapse file tree 5 files changed +43
-1
lines changed Expand file tree Collapse file tree 5 files changed +43
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff line change @@ -105,6 +105,20 @@ describe('API', function () {
105105 assert . strictEqual ( md . render ( '```\n&\n```' ) , '<pre><code>&\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 / ^ h t t p s ? : \/ \/ / . 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
You can’t perform that action at this time.
0 commit comments