UNPKG

markdown-it-terminal

Version:

Plugin for markdown-it to parse and output markdown formatted for the terminal

80 lines (60 loc) 1.73 kB
// Utilities // 'use strict'; // Merge objects // function assign(obj /*from1, from2, from3, ...*/) { var sources = Array.prototype.slice.call(arguments, 1); sources.forEach(function (source) { if (!source) { return; } if (typeof source !== 'object') { throw new TypeError(source + 'must be object'); } Object.keys(source).forEach(function (key) { obj[key] = source[key]; }); }); return obj; } //////////////////////////////////////////////////////////////////////////////// var HTML_ESCAPE_TEST_RE = /[&<>"]/; var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g; var HTML_REPLACEMENTS = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }; 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; } //////////////////////////////////////////////////////////////////////////////// var styles = require('ansi-styles'); function compoundStyle(styleList) { var open = ''; var close = ''; styleList.forEach(function(style){ open += styles[style].open; }); styleList.reverse().forEach(function(style) { close += styles[style].close; }); return { open: open, close: close }; }; //////////////////////////////////////////////////////////////////////////////// // Re-export libraries commonly used in both markdown-it and its plugins, // so plugins won't have to depend on them explicitly, which reduces their // bundled size (e.g. a browser build). // exports.assign = assign; exports.escapeHtml = escapeHtml; exports.compoundStyle = compoundStyle;