2
2
3
3
var Renderer = require ( './renderer' ) ;
4
4
5
- var esc = require ( '../common' ) . escapeXml ;
6
-
7
5
var reUnsafeProtocol = / ^ j a v a s c r i p t : | v b s c r i p t : | f i l e : | d a t a : / i;
8
6
var reSafeDataProtocol = / ^ d a t a : i m a g e \/ (?: p n g | g i f | j p e g | w e b p ) / i;
9
7
10
8
var potentiallyUnsafe = function ( url ) {
11
- return reUnsafeProtocol . test ( url ) &&
12
- ! reSafeDataProtocol . test ( url ) ;
9
+ return reUnsafeProtocol . test ( url ) &&
10
+ ! reSafeDataProtocol . test ( url ) ;
13
11
} ;
14
12
15
13
// Helper function to produce an HTML tag.
16
14
function tag ( name , attrs , selfclosing ) {
17
- if ( this . disableTags > 0 ) {
18
- return ;
19
- }
20
- this . buffer += ( '<' + name ) ;
21
- if ( attrs && attrs . length > 0 ) {
22
- var i = 0 ;
23
- var attrib ;
24
- while ( ( attrib = attrs [ i ] ) !== undefined ) {
25
- this . buffer += ( ' ' + attrib [ 0 ] + '="' + attrib [ 1 ] + '"' ) ;
26
- i ++ ;
27
- }
28
- }
29
- if ( selfclosing ) {
30
- this . buffer += ' /' ;
15
+ if ( this . disableTags > 0 ) {
16
+ return ;
17
+ }
18
+ this . buffer += ( '<' + name ) ;
19
+ if ( attrs && attrs . length > 0 ) {
20
+ var i = 0 ;
21
+ var attrib ;
22
+ while ( ( attrib = attrs [ i ] ) !== undefined ) {
23
+ this . buffer += ( ' ' + attrib [ 0 ] + '="' + attrib [ 1 ] + '"' ) ;
24
+ i ++ ;
31
25
}
32
- this . buffer += '>' ;
33
- this . lastOut = '>' ;
26
+ }
27
+ if ( selfclosing ) {
28
+ this . buffer += ' /' ;
29
+ }
30
+ this . buffer += '>' ;
31
+ this . lastOut = '>' ;
34
32
}
35
33
36
-
37
34
function HtmlRenderer ( options ) {
38
35
options = options || { } ;
39
36
// by default, soft breaks are rendered as newlines in HTML
@@ -64,38 +61,37 @@ function linebreak() {
64
61
function link ( node , entering ) {
65
62
var attrs = this . attrs ( node ) ;
66
63
if ( entering ) {
67
- if ( ! ( this . options . safe && potentiallyUnsafe ( node . destination ) ) ) {
68
- attrs . push ( [ 'href' , esc ( node . destination , true ) ] ) ;
69
- }
70
- if ( node . title ) {
71
- attrs . push ( [ 'title' , esc ( node . title , true ) ] ) ;
72
- }
73
- this . tag ( 'a' , attrs ) ;
64
+ if ( ! ( this . options . safe && potentiallyUnsafe ( node . destination ) ) ) {
65
+ attrs . push ( [ 'href' , this . esc ( node . destination , true ) ] ) ;
66
+ }
67
+ if ( node . title ) {
68
+ attrs . push ( [ 'title' , this . esc ( node . title , true ) ] ) ;
69
+ }
70
+ this . tag ( 'a' , attrs ) ;
74
71
} else {
75
- this . tag ( '/a' ) ;
72
+ this . tag ( '/a' ) ;
76
73
}
77
74
}
78
75
79
76
function image ( node , entering ) {
80
77
if ( entering ) {
81
- if ( this . disableTags === 0 ) {
82
- if ( this . options . safe &&
83
- potentiallyUnsafe ( node . destination ) ) {
84
- this . lit ( '<img src="" alt="' ) ;
85
- } else {
86
- this . lit ( '<img src="' + esc ( node . destination , true ) +
87
- '" alt="' ) ;
88
- }
78
+ if ( this . disableTags === 0 ) {
79
+ if ( this . options . safe && potentiallyUnsafe ( node . destination ) ) {
80
+ this . lit ( '<img src="" alt="' ) ;
81
+ } else {
82
+ this . lit ( '<img src="' + this . esc ( node . destination , true ) +
83
+ '" alt="' ) ;
89
84
}
90
- this . disableTags += 1 ;
85
+ }
86
+ this . disableTags += 1 ;
91
87
} else {
92
- this . disableTags -= 1 ;
93
- if ( this . disableTags === 0 ) {
94
- if ( node . title ) {
95
- this . lit ( '" title="' + esc ( node . title , true ) ) ;
96
- }
97
- this . lit ( '" />' ) ;
88
+ this . disableTags -= 1 ;
89
+ if ( this . disableTags === 0 ) {
90
+ if ( node . title ) {
91
+ this . lit ( '" title="' + this . esc ( node . title , true ) ) ;
98
92
}
93
+ this . lit ( '" />' ) ;
94
+ }
99
95
}
100
96
}
101
97
@@ -111,29 +107,29 @@ function paragraph(node, entering) {
111
107
var grandparent = node . parent . parent
112
108
, attrs = this . attrs ( node ) ;
113
109
if ( grandparent !== null &&
114
- grandparent . type === 'list' ) {
115
- if ( grandparent . listTight ) {
116
- return ;
117
- }
110
+ grandparent . type === 'list' ) {
111
+ if ( grandparent . listTight ) {
112
+ return ;
113
+ }
118
114
}
119
115
if ( entering ) {
120
- this . cr ( ) ;
121
- this . tag ( 'p' , attrs ) ;
116
+ this . cr ( ) ;
117
+ this . tag ( 'p' , attrs ) ;
122
118
} else {
123
- this . tag ( '/p' ) ;
124
- this . cr ( ) ;
119
+ this . tag ( '/p' ) ;
120
+ this . cr ( ) ;
125
121
}
126
122
}
127
123
128
124
function heading ( node , entering ) {
129
125
var tagname = 'h' + node . level
130
126
, attrs = this . attrs ( node ) ;
131
127
if ( entering ) {
132
- this . cr ( ) ;
133
- this . tag ( tagname , attrs ) ;
128
+ this . cr ( ) ;
129
+ this . tag ( tagname , attrs ) ;
134
130
} else {
135
- this . tag ( '/' + tagname ) ;
136
- this . cr ( ) ;
131
+ this . tag ( '/' + tagname ) ;
132
+ this . cr ( ) ;
137
133
}
138
134
}
139
135
@@ -147,7 +143,7 @@ function code_block(node) {
147
143
var info_words = node . info ? node . info . split ( / \s + / ) : [ ]
148
144
, attrs = this . attrs ( node ) ;
149
145
if ( info_words . length > 0 && info_words [ 0 ] . length > 0 ) {
150
- attrs . push ( [ 'class' , 'language-' + esc ( info_words [ 0 ] , true ) ] ) ;
146
+ attrs . push ( [ 'class' , 'language-' + this . esc ( info_words [ 0 ] , true ) ] ) ;
151
147
}
152
148
this . cr ( ) ;
153
149
this . tag ( 'pre' ) ;
@@ -168,13 +164,13 @@ function thematic_break(node) {
168
164
function block_quote ( node , entering ) {
169
165
var attrs = this . attrs ( node ) ;
170
166
if ( entering ) {
171
- this . cr ( ) ;
172
- this . tag ( 'blockquote' , attrs ) ;
173
- this . cr ( ) ;
167
+ this . cr ( ) ;
168
+ this . tag ( 'blockquote' , attrs ) ;
169
+ this . cr ( ) ;
174
170
} else {
175
- this . cr ( ) ;
176
- this . tag ( '/blockquote' ) ;
177
- this . cr ( ) ;
171
+ this . cr ( ) ;
172
+ this . tag ( '/blockquote' ) ;
173
+ this . cr ( ) ;
178
174
}
179
175
}
180
176
@@ -183,81 +179,81 @@ function list(node, entering) {
183
179
, attrs = this . attrs ( node ) ;
184
180
185
181
if ( entering ) {
186
- var start = node . listStart ;
187
- if ( start !== null && start !== 1 ) {
188
- attrs . push ( [ 'start' , start . toString ( ) ] ) ;
189
- }
190
- this . cr ( ) ;
191
- this . tag ( tagname , attrs ) ;
192
- this . cr ( ) ;
182
+ var start = node . listStart ;
183
+ if ( start !== null && start !== 1 ) {
184
+ attrs . push ( [ 'start' , start . toString ( ) ] ) ;
185
+ }
186
+ this . cr ( ) ;
187
+ this . tag ( tagname , attrs ) ;
188
+ this . cr ( ) ;
193
189
} else {
194
- this . cr ( ) ;
195
- this . tag ( '/' + tagname ) ;
196
- this . cr ( ) ;
190
+ this . cr ( ) ;
191
+ this . tag ( '/' + tagname ) ;
192
+ this . cr ( ) ;
197
193
}
198
194
}
199
195
200
196
function item ( node , entering ) {
201
197
var attrs = this . attrs ( node ) ;
202
198
if ( entering ) {
203
- this . tag ( 'li' , attrs ) ;
199
+ this . tag ( 'li' , attrs ) ;
204
200
} else {
205
- this . tag ( '/li' ) ;
206
- this . cr ( ) ;
201
+ this . tag ( '/li' ) ;
202
+ this . cr ( ) ;
207
203
}
208
204
}
209
205
210
206
function html_inline ( node ) {
211
207
if ( this . options . safe ) {
212
- this . lit ( '<!-- raw HTML omitted -->' ) ;
208
+ this . lit ( '<!-- raw HTML omitted -->' ) ;
213
209
} else {
214
- this . lit ( node . literal ) ;
210
+ this . lit ( node . literal ) ;
215
211
}
216
212
}
217
213
218
214
function html_block ( node ) {
219
215
this . cr ( ) ;
220
216
if ( this . options . safe ) {
221
- this . lit ( '<!-- raw HTML omitted -->' ) ;
217
+ this . lit ( '<!-- raw HTML omitted -->' ) ;
222
218
} else {
223
- this . lit ( node . literal ) ;
219
+ this . lit ( node . literal ) ;
224
220
}
225
221
this . cr ( ) ;
226
222
}
227
223
228
224
function custom_inline ( node , entering ) {
229
225
if ( entering && node . onEnter ) {
230
- this . lit ( node . onEnter ) ;
226
+ this . lit ( node . onEnter ) ;
231
227
} else if ( ! entering && node . onExit ) {
232
- this . lit ( node . onExit ) ;
228
+ this . lit ( node . onExit ) ;
233
229
}
234
230
}
235
231
236
232
function custom_block ( node , entering ) {
237
233
this . cr ( ) ;
238
234
if ( entering && node . onEnter ) {
239
- this . lit ( node . onEnter ) ;
235
+ this . lit ( node . onEnter ) ;
240
236
} else if ( ! entering && node . onExit ) {
241
- this . lit ( node . onExit ) ;
237
+ this . lit ( node . onExit ) ;
242
238
}
243
239
this . cr ( ) ;
244
240
}
245
241
246
242
/* Helper methods */
247
243
248
244
function out ( s ) {
249
- this . lit ( esc ( s , false ) ) ;
245
+ this . lit ( this . esc ( s , false ) ) ;
250
246
}
251
247
252
248
function attrs ( node ) {
253
249
var att = [ ] ;
254
250
if ( this . options . sourcepos ) {
255
- var pos = node . sourcepos ;
256
- if ( pos ) {
257
- att . push ( [ 'data-sourcepos' , String ( pos [ 0 ] [ 0 ] ) + ':' +
258
- String ( pos [ 0 ] [ 1 ] ) + '-' + String ( pos [ 1 ] [ 0 ] ) + ':' +
259
- String ( pos [ 1 ] [ 1 ] ) ] ) ;
260
- }
251
+ var pos = node . sourcepos ;
252
+ if ( pos ) {
253
+ att . push ( [ 'data-sourcepos' , String ( pos [ 0 ] [ 0 ] ) + ':' +
254
+ String ( pos [ 0 ] [ 1 ] ) + '-' + String ( pos [ 1 ] [ 0 ] ) + ':' +
255
+ String ( pos [ 1 ] [ 1 ] ) ] ) ;
256
+ }
261
257
}
262
258
return att ;
263
259
}
@@ -285,6 +281,8 @@ HtmlRenderer.prototype.item = item;
285
281
HtmlRenderer . prototype . custom_inline = custom_inline ;
286
282
HtmlRenderer . prototype . custom_block = custom_block ;
287
283
284
+ HtmlRenderer . prototype . esc = require ( '../common' ) . escapeXml ;
285
+
288
286
HtmlRenderer . prototype . out = out ;
289
287
HtmlRenderer . prototype . tag = tag ;
290
288
HtmlRenderer . prototype . attrs = attrs ;
0 commit comments