Debian Codegraph @

src/utilities.js

raw symbols SCIP
1 export function extend (destination) {
2 for (let i = 1; i < arguments.length; i++) {
3 const source = arguments[i]
4 for (const key in source) {
5 if (Object.prototype.hasOwnProperty.call(source, key)) destination[key] = source[key]
6 }
7 }
8 return destination
9 }
10
11 export function repeat (character, count) {
12 return Array(count + 1).join(character)
13 }
14
15 export function trimLeadingNewlines (string) {
16 return string.replace(/^\n*/, '')
17 }
18
19 export function trimTrailingNewlines (string) {
20 // avoid match-at-end regexp bottleneck, see #370
21 let indexEnd = string.length
22 while (indexEnd > 0 && string[indexEnd - 1] === '\n') indexEnd--
23 return string.substring(0, indexEnd)
24 }
25
26 export function trimNewlines (string) {
27 return trimTrailingNewlines(trimLeadingNewlines(string))
28 }
29
30 export var blockElements = [
31 'ADDRESS', 'ARTICLE', 'ASIDE', 'AUDIO', 'BLOCKQUOTE', 'BODY', 'CANVAS',
32 'CENTER', 'DD', 'DIR', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE',
33 'FOOTER', 'FORM', 'FRAMESET', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER',
34 'HGROUP', 'HR', 'HTML', 'ISINDEX', 'LI', 'MAIN', 'MENU', 'NAV', 'NOFRAMES',
35 'NOSCRIPT', 'OL', 'OUTPUT', 'P', 'PRE', 'SECTION', 'TABLE', 'TBODY', 'TD',
36 'TFOOT', 'TH', 'THEAD', 'TR', 'UL'
37 ]
38
39 export function isBlock (node) {
40 return is(node, blockElements)
41 }
42
43 export var voidElements = [
44 'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT',
45 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'
46 ]
47
48 export function isVoid (node) {
49 return is(node, voidElements)
50 }
51
52 export function hasVoid (node) {
53 return has(node, voidElements)
54 }
55
56 const meaningfulWhenBlankElements = [
57 'A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT',
58 'AUDIO', 'VIDEO'
59 ]
60
61 export function isMeaningfulWhenBlank (node) {
62 return is(node, meaningfulWhenBlankElements)
63 }
64
65 export function hasMeaningfulWhenBlank (node) {
66 return has(node, meaningfulWhenBlankElements)
67 }
68
69 function is (node, tagNames) {
70 return tagNames.indexOf(node.nodeName) >= 0
71 }
72
73 function has (node, tagNames) {
74 return (
75 node.getElementsByTagName &&
76 tagNames.some(function (tagName) {
77 return node.getElementsByTagName(tagName).length
78 })
79 )
80 }
81
82 const markdownEscapes = [
83 [/\\/g, '\\\\'],
84 [/\*/g, '\\*'],
85 [/^-/g, '\\-'],
86 [/^\+ /g, '\\+ '],
87 [/^(=+)/g, '\\$1'],
88 [/^(#{1,6}) /g, '\\$1 '],
89 [/`/g, '\\`'],
90 [/^~~~/g, '\\~~~'],
91 [/\[/g, '\\['],
92 [/\]/g, '\\]'],
93 [/^>/g, '\\>'],
94 [/_/g, '\\_'],
95 [/^(\d+)\. /g, '$1\\. ']
96 ]
97
98 export function escapeMarkdown (string) {
99 return markdownEscapes.reduce(function (accumulator, escape) {
100 return accumulator.replace(escape[0], escape[1])
101 }, string)
102 }