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) {
var i, token, text, nodes, ln, pos, level,
htmlLinkLevel = 0,
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--) {
token = tokens[i];
// Skip content of links
// Skip content of markdown links
if (token.type === 'link_close') {
i--;
while (tokens[i].type !== 'link_open' && tokens[i].level !== token.level) {
@ -39,6 +49,17 @@ module.exports = function linkify(t, state) {
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' &&
(token.content.indexOf('://') ||
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>
.
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 Remarked = require('../');
var Remarkable = require('../');
describe('Default', function () {
var md = new Remarked({
var md = new Remarkable({
html: true,
langPrefix: '',
typographer: true,
linkify: true

Loading…
Cancel
Save