diff --git a/lib/rules_typographer/linkify.js b/lib/rules_typographer/linkify.js index e6c2437..cd3880b 100644 --- a/lib/rules_typographer/linkify.js +++ b/lib/rules_typographer/linkify.js @@ -15,7 +15,7 @@ var autolinker = new Autolinker({ replaceFn: function (autolinker, match) { // Only collect matched strings but don't change anything. if (match.getType() === 'url') { - links.push(match.getUrl()); + links.push({ text: match.matchedText, url: match.getUrl() }); } return false; } @@ -75,9 +75,12 @@ module.exports = function linkify(t, state) { for (ln = 0; ln < links.length; ln++) { - if (!state.parser.validateLink(links[ln])) { continue; } + if (!state.parser.validateLink(links[ln].url)) { continue; } + + pos = text.indexOf(links[ln].text); + + if (pos === -1) { continue; } - pos = text.indexOf(links[ln]); if (pos) { level = level; nodes.push({ @@ -88,20 +91,20 @@ module.exports = function linkify(t, state) { } nodes.push({ type: 'link_open', - href: links[ln], + href: links[ln].url, title: '', level: level++ }); nodes.push({ type: 'text', - content: escapeHtml(links[ln]), + content: escapeHtml(links[ln].text), level: level }); nodes.push({ type: 'link_close', level: --level }); - text = text.slice(pos + links[ln].length); + text = text.slice(pos + links[ln].text.length); } if (text.length) { nodes.push({ diff --git a/test/fixtures/remarkable/linkify.txt b/test/fixtures/remarkable/linkify.txt index fb55754..4856943 100644 --- a/test/fixtures/remarkable/linkify.txt +++ b/test/fixtures/remarkable/linkify.txt @@ -29,3 +29,18 @@ don't touch text in html tags

https://example.com

. + +match links without protocol +. +www.example.org +. +

www.example.org

+. + + +properly cut domain end +. +www.example.org版权所有 +. +

www.example.org版权所有

+.