export function extend (destination) { for (let i = 1; i < arguments.length; i++) { const source = arguments[i] for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) destination[key] = source[key] } } return destination } export function repeat (character, count) { return Array(count + 1).join(character) } export function trimLeadingNewlines (string) { return string.replace(/^\n*/, '') } export function trimTrailingNewlines (string) { // avoid match-at-end regexp bottleneck, see #370 let indexEnd = string.length while (indexEnd > 0 && string[indexEnd - 1] === '\n') indexEnd-- return string.substring(0, indexEnd) } export function trimNewlines (string) { return trimTrailingNewlines(trimLeadingNewlines(string)) } export var blockElements = [ 'ADDRESS', 'ARTICLE', 'ASIDE', 'AUDIO', 'BLOCKQUOTE', 'BODY', 'CANVAS', 'CENTER', 'DD', 'DIR', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE', 'FOOTER', 'FORM', 'FRAMESET', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER', 'HGROUP', 'HR', 'HTML', 'ISINDEX', 'LI', 'MAIN', 'MENU', 'NAV', 'NOFRAMES', 'NOSCRIPT', 'OL', 'OUTPUT', 'P', 'PRE', 'SECTION', 'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'UL' ] export function isBlock (node) { return is(node, blockElements) } export var voidElements = [ 'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR' ] export function isVoid (node) { return is(node, voidElements) } export function hasVoid (node) { return has(node, voidElements) } const meaningfulWhenBlankElements = [ 'A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT', 'AUDIO', 'VIDEO' ] export function isMeaningfulWhenBlank (node) { return is(node, meaningfulWhenBlankElements) } export function hasMeaningfulWhenBlank (node) { return has(node, meaningfulWhenBlankElements) } function is (node, tagNames) { return tagNames.indexOf(node.nodeName) >= 0 } function has (node, tagNames) { return ( node.getElementsByTagName && tagNames.some(function (tagName) { return node.getElementsByTagName(tagName).length }) ) } const markdownEscapes = [ [/\\/g, '\\\\'], [/\*/g, '\\*'], [/^-/g, '\\-'], [/^\+ /g, '\\+ '], [/^(=+)/g, '\\$1'], [/^(#{1,6}) /g, '\\$1 '], [/`/g, '\\`'], [/^~~~/g, '\\~~~'], [/\[/g, '\\['], [/\]/g, '\\]'], [/^>/g, '\\>'], [/_/g, '\\_'], [/^(\d+)\. /g, '$1\\. '] ] export function escapeMarkdown (string) { return markdownEscapes.reduce(function (accumulator, escape) { return accumulator.replace(escape[0], escape[1]) }, string) }