'use strict'; var assign = require('./common/utils').assign; var unescapeMd = require('./common/utils').unescapeMd; var replaceEntities = require('./common/utils').replaceEntities; //////////////////////////////////////////////////////////////////////////////// // 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_REPLACE_RE = /[&<>"]/g; var HTML_REPLACEMENTS = { '&': '&', '<': '<', '>': '>', '"': '"' }; function replaceUnsafeChar(ch) { return HTML_REPLACEMENTS[ch]; } function escapeHtml(str) { if (HTML_ESCAPE_TEST_RE.test(str)) { return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar); } return str; } function nextToken(tokens, idx) { if (++idx >= tokens.length - 2) { return idx; } if (tokens[idx].type === 'paragraph_open' && tokens[idx].tight) { if (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) { if (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight) { return nextToken(tokens, idx + 2); } } } return idx; } // check if we need to hide '\n' before next token function getBreak(tokens, idx) { idx = nextToken(tokens, idx); if (idx < tokens.length && tokens[idx].type === 'list_item_close') { return ''; } return '\n'; } //////////////////////////////////////////////////////////////////////////////// var rules = {}; rules.abbr_open = function (tokens, idx/*, options*/) { var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; return ''; }; rules.abbr_close = function (/*tokens, idx, options*/) { return ''; }; rules.blockquote_open = function (/*tokens, idx, options*/) { return '
\n'; }; rules.blockquote_close = function (tokens, idx /*, options*/) { return '' + getBreak(tokens, idx); }; rules.code = function (tokens, idx /*, options*/) { if (tokens[idx].block) { return '
' + escapeHtml(tokens[idx].content) + '
' + getBreak(tokens, idx);
}
return '' + escapeHtml(tokens[idx].content) + '
';
};
rules.fence = function (tokens, idx, options) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix || '';
var params, langName = '';
var highlighted;
if (token.params) {
params = token.params.split(/ +/g);
langName = escapeHtml(replaceEntities(unescapeMd(params[0])));
langClass = ' class="' + langPrefix + langName + '"';
}
if (options.highlight) {
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
} else {
highlighted = escapeHtml(token.content);
}
return ''
+ highlighted
+ '
' + getBreak(tokens, idx);
};
rules.heading_open = function (tokens, idx /*, options*/) {
return ''; }; rules.paragraph_close = function (tokens, idx /*, options*/) { var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content); return (tokens[idx].tight ? '' : '
') + (addBreak ? getBreak(tokens, idx) : ''); }; rules.link_open = function (tokens, idx /*, options*/) { var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; return ''; }; rules.link_close = function (/*tokens, idx, options*/) { return ''; }; rules.image = function (tokens, idx, options) { var src = ' src="' + escapeHtml(escapeUrl(tokens[idx].src)) + '"'; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; var suffix = options.xhtmlOut ? ' /' : ''; return ''; }; rules.table_open = function (/*tokens, idx, options*/) { return '