forked from markdown-it/markdown-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add linkifier rule to inline chain for full links
prevents emphasis from appearing in `http://example.org/foo._bar_.baz`
- Loading branch information
Showing
9 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Process links like https://example.org/ | ||
|
||
'use strict'; | ||
|
||
|
||
// RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) | ||
var SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i; | ||
|
||
|
||
module.exports = function linkify(state, silent) { | ||
var pos, max, match, proto, link, url, fullUrl, token; | ||
|
||
if (!state.md.options.linkify) return false; | ||
if (state.linkLevel > 0) return false; | ||
|
||
pos = state.pos; | ||
max = state.posMax; | ||
|
||
if (pos + 3 > max) return false; | ||
if (state.src.charCodeAt(pos) !== 0x3A/* : */) return false; | ||
if (state.src.charCodeAt(pos + 1) !== 0x2F/* / */) return false; | ||
if (state.src.charCodeAt(pos + 2) !== 0x2F/* / */) return false; | ||
|
||
match = state.pending.match(SCHEME_RE); | ||
if (!match) return false; | ||
|
||
proto = match[1]; | ||
|
||
link = state.md.linkify.matchAtStart(state.src.slice(pos - proto.length)); | ||
if (!link) return false; | ||
|
||
url = link.url; | ||
|
||
// disallow '*' at the end of the link (conflicts with emphasis) | ||
url = url.replace(/\*+$/, ''); | ||
|
||
fullUrl = state.md.normalizeLink(url); | ||
if (!state.md.validateLink(fullUrl)) return false; | ||
|
||
if (!silent) { | ||
state.pending = state.pending.slice(0, -proto.length); | ||
|
||
token = state.push('link_open', 'a', 1); | ||
token.attrs = [ [ 'href', fullUrl ] ]; | ||
token.markup = 'linkify'; | ||
token.info = 'auto'; | ||
|
||
token = state.push('text', '', 0); | ||
token.content = state.md.normalizeLinkText(url); | ||
|
||
token = state.push('link_close', 'a', -1); | ||
token.markup = 'linkify'; | ||
token.info = 'auto'; | ||
} | ||
|
||
state.pos += url.length - proto.length; | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters