-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathgeneric-formatters.js
153 lines (136 loc) · 4.02 KB
/
generic-formatters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { render } from 'dom-serializer';
// eslint-disable-next-line import/no-unassigned-import
import './typedefs';
/**
* Dummy formatter that discards the input and does nothing.
*
* @type { FormatCallback }
*/
function formatSkip (elem, walk, builder, formatOptions) {
/* do nothing */
}
/**
* Insert the given string literal inline instead of a tag.
*
* @type { FormatCallback }
*/
function formatInlineString (elem, walk, builder, formatOptions) {
builder.addLiteral(formatOptions.string || '');
}
/**
* Insert a block with the given string literal instead of a tag.
*
* @type { FormatCallback }
*/
function formatBlockString (elem, walk, builder, formatOptions) {
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 2 });
builder.addLiteral(formatOptions.string || '');
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 2 });
}
/**
* Process an inline-level element.
*
* @type { FormatCallback }
*/
function formatInline (elem, walk, builder, formatOptions) {
walk(elem.children, builder);
}
/**
* Process a block-level container.
*
* @type { FormatCallback }
*/
function formatBlock (elem, walk, builder, formatOptions) {
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 2 });
walk(elem.children, builder);
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 2 });
}
function renderOpenTag (elem) {
const attrs = (elem.attribs && elem.attribs.length)
? ' ' + Object.entries(elem.attribs)
.map(([k, v]) => ((v === '') ? k : `${k}=${v.replace(/"/g, '"')}`))
.join(' ')
: '';
return `<${elem.name}${attrs}>`;
}
function renderCloseTag (elem) {
return `</${elem.name}>`;
}
/**
* Render an element as inline HTML tag, walk through it's children.
*
* @type { FormatCallback }
*/
function formatInlineTag (elem, walk, builder, formatOptions) {
builder.startNoWrap();
builder.addLiteral(renderOpenTag(elem));
builder.stopNoWrap();
walk(elem.children, builder);
builder.startNoWrap();
builder.addLiteral(renderCloseTag(elem));
builder.stopNoWrap();
}
/**
* Render an element as HTML block bag, walk through it's children.
*
* @type { FormatCallback }
*/
function formatBlockTag (elem, walk, builder, formatOptions) {
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 2 });
builder.startNoWrap();
builder.addLiteral(renderOpenTag(elem));
builder.stopNoWrap();
walk(elem.children, builder);
builder.startNoWrap();
builder.addLiteral(renderCloseTag(elem));
builder.stopNoWrap();
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 2 });
}
/**
* Render an element with all it's children as inline HTML.
*
* @type { FormatCallback }
*/
function formatInlineHtml (elem, walk, builder, formatOptions) {
builder.startNoWrap();
builder.addLiteral(
render(elem, { decodeEntities: builder.options.decodeEntities })
);
builder.stopNoWrap();
}
/**
* Render an element with all it's children as HTML block.
*
* @type { FormatCallback }
*/
function formatBlockHtml (elem, walk, builder, formatOptions) {
builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 2 });
builder.startNoWrap();
builder.addLiteral(
render(elem, { decodeEntities: builder.options.decodeEntities })
);
builder.stopNoWrap();
builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 2 });
}
/**
* Render inline element wrapped with given strings.
*
* @type { FormatCallback }
*/
function formatInlineSurround (elem, walk, builder, formatOptions) {
builder.addLiteral(formatOptions.prefix || '');
walk(elem.children, builder);
builder.addLiteral(formatOptions.suffix || '');
}
export {
formatBlock as block,
formatBlockHtml as blockHtml,
formatBlockString as blockString,
formatBlockTag as blockTag,
formatInline as inline,
formatInlineHtml as inlineHtml,
formatInlineString as inlineString,
formatInlineSurround as inlineSurround,
formatInlineTag as inlineTag,
formatSkip as skip,
};