Browse Source

Don't linkify content of html <a> tags

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
13db2bd996
  1. 23
      lib/rules_typographer/linkify.js
  2. 24
      test/fixtures/remarkable/linkify.txt
  3. 5
      test/remarkable.js

23
lib/rules_typographer/linkify.js

@ -21,15 +21,25 @@ var autolinker = new Autolinker({
} }
}); });
function isLinkOpen(str) {
return /^<a[>\s]/i.test(str);
}
function isLinkClose(str) {
return /^<\/a\s*>/i.test(str);
}
module.exports = function linkify(t, state) { module.exports = function linkify(t, state) {
var i, token, text, nodes, ln, pos, level, var i, token, text, nodes, ln, pos, level,
htmlLinkLevel = 0,
tokens = state.tokens; tokens = state.tokens;
// We scan from the end, to keep position when new tags added.
// Use reversed logic in links start/end match
for (i = tokens.length - 1; i >= 0; i--) { for (i = tokens.length - 1; i >= 0; i--) {
token = tokens[i]; token = tokens[i];
// Skip content of links // Skip content of markdown links
if (token.type === 'link_close') { if (token.type === 'link_close') {
i--; i--;
while (tokens[i].type !== 'link_open' && tokens[i].level !== token.level) { while (tokens[i].type !== 'link_open' && tokens[i].level !== token.level) {
@ -39,6 +49,17 @@ module.exports = function linkify(t, state) {
continue; continue;
} }
// Skip content of html tag links
if (token.type === 'htmltag') {
if (isLinkOpen(token.content) && htmlLinkLevel > 0) {
htmlLinkLevel--;
}
if (isLinkClose(token.content)) {
htmlLinkLevel++;
}
}
if (htmlLinkLevel > 0) { continue; }
if (token.type === 'text' && if (token.type === 'text' &&
(token.content.indexOf('://') || (token.content.indexOf('://') ||
token.content.indexOf('www'))) { token.content.indexOf('www'))) {

24
test/fixtures/remarkable/linkify.txt

@ -4,8 +4,28 @@ url http://www.youtube.com/watch?v=5Jt5GEr4AYg.
. .
<p>url <a href="http://www.youtube.com/watch?v=5Jt5GEr4AYg">http://www.youtube.com/watch?v=5Jt5GEr4AYg</a>.</p> <p>url <a href="http://www.youtube.com/watch?v=5Jt5GEr4AYg">http://www.youtube.com/watch?v=5Jt5GEr4AYg</a>.</p>
. .
don't touch text in links
. .
don't touch text in links <https://example.com> [https://example.com](https://example.com)
. .
<p>don't touch text in links <a href="https://example.com">https://example.com</a></p> <p><a href="https://example.com">https://example.com</a></p>
. .
don't touch text in autolinks
.
<https://example.com>
.
<p><a href="https://example.com">https://example.com</a></p>
.
don't touch text in html <a> tags
.
<a href="https://example.com">https://example.com</a>
.
<p><a href="https://example.com">https://example.com</a></p>
.

5
test/remarkable.js

@ -6,11 +6,12 @@ var path = require('path');
var utils = require('./utils'); var utils = require('./utils');
var Remarked = require('../'); var Remarkable = require('../');
describe('Default', function () { describe('Default', function () {
var md = new Remarked({ var md = new Remarkable({
html: true,
langPrefix: '', langPrefix: '',
typographer: true, typographer: true,
linkify: true linkify: true

Loading…
Cancel
Save