|
@ -9,19 +9,6 @@ var replaceEntities = require('./common/utils').replaceEntities; |
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
// Helpers
|
|
|
// Helpers
|
|
|
|
|
|
|
|
|
function escapeUrl(str) { |
|
|
|
|
|
try { |
|
|
|
|
|
return encodeURI(str); |
|
|
|
|
|
} catch (__) {} |
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
function unescapeUrl(str) { |
|
|
|
|
|
try { |
|
|
|
|
|
return decodeURI(str); |
|
|
|
|
|
} catch (__) {} |
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var HTML_ESCAPE_TEST_RE = /[&<>"]/; |
|
|
var HTML_ESCAPE_TEST_RE = /[&<>"]/; |
|
|
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g; |
|
|
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g; |
|
|
var HTML_REPLACEMENTS = { |
|
|
var HTML_REPLACEMENTS = { |
|
@ -44,13 +31,11 @@ function escapeHtml(str) { |
|
|
|
|
|
|
|
|
function nextToken(tokens, idx) { |
|
|
function nextToken(tokens, idx) { |
|
|
if (++idx >= tokens.length - 2) { return idx; } |
|
|
if (++idx >= tokens.length - 2) { return idx; } |
|
|
if (tokens[idx].type === 'paragraph_open' && tokens[idx].tight) { |
|
|
if ((tokens[idx].type === 'paragraph_open' && tokens[idx].tight) && |
|
|
if (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) { |
|
|
(tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) && |
|
|
if (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight) { |
|
|
(tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight)) { |
|
|
return nextToken(tokens, idx + 2); |
|
|
return nextToken(tokens, idx + 2); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return idx; |
|
|
return idx; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -72,8 +57,7 @@ var rules = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.abbr_open = function (tokens, idx/*, options*/) { |
|
|
rules.abbr_open = function (tokens, idx/*, options*/) { |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">'; |
|
|
return '<abbr' + title + '>'; |
|
|
|
|
|
}; |
|
|
}; |
|
|
rules.abbr_close = function (/*tokens, idx, options*/) { |
|
|
rules.abbr_close = function (/*tokens, idx, options*/) { |
|
|
return '</abbr>'; |
|
|
return '</abbr>'; |
|
@ -170,7 +154,7 @@ rules.paragraph_close = function (tokens, idx /*, options*/) { |
|
|
|
|
|
|
|
|
rules.link_open = function (tokens, idx /*, options*/) { |
|
|
rules.link_open = function (tokens, idx /*, options*/) { |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
return '<a href="' + escapeHtml(escapeUrl(unescapeUrl(replaceEntities(tokens[idx].href)))) + '"' + title + '>'; |
|
|
return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>'; |
|
|
}; |
|
|
}; |
|
|
rules.link_close = function (/*tokens, idx, options*/) { |
|
|
rules.link_close = function (/*tokens, idx, options*/) { |
|
|
return '</a>'; |
|
|
return '</a>'; |
|
@ -178,7 +162,7 @@ rules.link_close = function (/*tokens, idx, options*/) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.image = function (tokens, idx, options) { |
|
|
rules.image = function (tokens, idx, options) { |
|
|
var src = ' src="' + escapeHtml(escapeUrl(tokens[idx].src)) + '"'; |
|
|
var src = ' src="' + escapeHtml(encodeURI(tokens[idx].src)) + '"'; |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; |
|
|
var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; |
|
|
var suffix = options.xhtmlOut ? ' /' : ''; |
|
|
var suffix = options.xhtmlOut ? ' /' : ''; |
|
|