Browse Source

Linkify fix. Closes #51

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
719d4c1888
  1. 15
      lib/rules_typographer/linkify.js
  2. 15
      test/fixtures/remarkable/linkify.txt

15
lib/rules_typographer/linkify.js

@ -15,7 +15,7 @@ var autolinker = new Autolinker({
replaceFn: function (autolinker, match) { replaceFn: function (autolinker, match) {
// Only collect matched strings but don't change anything. // Only collect matched strings but don't change anything.
if (match.getType() === 'url') { if (match.getType() === 'url') {
links.push(match.getUrl()); links.push({ text: match.matchedText, url: match.getUrl() });
} }
return false; return false;
} }
@ -75,9 +75,12 @@ module.exports = function linkify(t, state) {
for (ln = 0; ln < links.length; ln++) { 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) { if (pos) {
level = level; level = level;
nodes.push({ nodes.push({
@ -88,20 +91,20 @@ module.exports = function linkify(t, state) {
} }
nodes.push({ nodes.push({
type: 'link_open', type: 'link_open',
href: links[ln], href: links[ln].url,
title: '', title: '',
level: level++ level: level++
}); });
nodes.push({ nodes.push({
type: 'text', type: 'text',
content: escapeHtml(links[ln]), content: escapeHtml(links[ln].text),
level: level level: level
}); });
nodes.push({ nodes.push({
type: 'link_close', type: 'link_close',
level: --level level: --level
}); });
text = text.slice(pos + links[ln].length); text = text.slice(pos + links[ln].text.length);
} }
if (text.length) { if (text.length) {
nodes.push({ nodes.push({

15
test/fixtures/remarkable/linkify.txt

@ -29,3 +29,18 @@ don't touch text in html <a> tags
<p><a href="https://example.com">https://example.com</a></p> <p><a href="https://example.com">https://example.com</a></p>
. .
match links without protocol
.
www.example.org
.
<p><a href="http://www.example.org">www.example.org</a></p>
.
properly cut domain end
.
www.example.org版权所有
.
<p><a href="http://www.example.org">www.example.org</a>版权所有</p>
.

Loading…
Cancel
Save