@@ -25,6 +25,7 @@ var block = {
25
25
def : / ^ * \[ ( [ ^ \] ] + ) \] : * < ? ( [ ^ \s > ] + ) > ? (?: + [ " ( ] ( [ ^ \n ] + ) [ " ) ] ) ? * (?: \n + | $ ) / ,
26
26
table : noop ,
27
27
paragraph : / ^ ( (?: [ ^ \n ] + \n ? (? ! h r | h e a d i n g | l h e a d i n g | b l o c k q u o t e | t a g | d e f ) ) + ) \n * / ,
28
+ quiz : / ^ ( * [ - * > > ] ) { 2 , } ( .* ?) ( [ - * < < ] ) { 2 , } * (?: \n + | $ ) / ,
28
29
text : / ^ [ ^ \n ] + /
29
30
} ;
30
31
@@ -259,6 +260,55 @@ Lexer.prototype.token = function(src, top, bq) {
259
260
continue ;
260
261
}
261
262
263
+ // quiz
264
+ if ( cap = this . rules . quiz . exec ( src ) ) {
265
+ // console.log("***** QUIZ RULES HERE");
266
+ src = src . substring ( cap [ 0 ] . length ) ;
267
+
268
+ var nextBlankLine = src . indexOf ( "\n\n" ) ;
269
+ if ( nextBlankLine === - 1 ) {
270
+ nextBlankLine = src . length ;
271
+ }
272
+
273
+ cap2 = src . substring ( 0 , nextBlankLine ) ;
274
+ src = src . substring ( cap2 . length ) ;
275
+
276
+ var types = {
277
+ "[" : "checkbox" ,
278
+ "(" : "radio" ,
279
+ "=" : "match" ,
280
+ "~" : "contains"
281
+ }
282
+ var answer = {
283
+ "*" : "correct" ,
284
+ "?" : "optional"
285
+ }
286
+
287
+ var itemsMarkdown = cap2 . split ( "\n" ) ;
288
+ var items = [ ] ;
289
+ for ( var i = 0 ; i < itemsMarkdown . length ; i ++ ) {
290
+ var mQuiz = itemsMarkdown [ i ] ;
291
+ var type = types [ mQuiz [ 0 ] ] ;
292
+ if ( type === undefined ) {
293
+ continue ;
294
+ }
295
+
296
+ items . push ( {
297
+ type : type ,
298
+ text : mQuiz . substring ( 3 ) . trim ( ) ,
299
+ option : ( type === types [ "=" ] ) ? mQuiz . substring ( 1 ) . trim ( ) : answer [ mQuiz [ 1 ] ]
300
+ } ) ;
301
+ }
302
+
303
+ this . tokens . push ( {
304
+ type : 'quiz' ,
305
+ text : cap [ 2 ] . trim ( ) ,
306
+ items : items
307
+ } ) ;
308
+
309
+ continue ;
310
+ }
311
+
262
312
// blockquote
263
313
if ( cap = this . rules . blockquote . exec ( src ) ) {
264
314
src = src . substring ( cap [ 0 ] . length ) ;
@@ -800,6 +850,26 @@ Renderer.prototype.code = function(code, lang, escaped, attr) {
800
850
+ '\n</code></pre>\n' ;
801
851
} ;
802
852
853
+ Renderer . prototype . quiz = function ( quizTokens ) {
854
+ var body = "<section class='quiz'>" + " <h1>" + quizTokens . text + "</h1>" ;
855
+
856
+ body += "<form>" ;
857
+ for ( var i = 0 ; i < quizTokens . items . length ; i ++ ) {
858
+ var e = quizTokens . items [ i ] ;
859
+ if ( e . type === "match" || e . type === "contains" ) {
860
+ body += "<input type='text' data-answer='" + e . text + "' data-pattern='" + e . type + "' />" ;
861
+ } else {
862
+ body += "<input type='" + e . type + "' data-answer='correct' />" + e . text + "</br>" ;
863
+ }
864
+ }
865
+
866
+ body + "</form>" ;
867
+
868
+ body += "</section>" ;
869
+
870
+ return body ;
871
+ }
872
+
803
873
Renderer . prototype . blockquote = function ( quote ) {
804
874
return '<blockquote>\n' + quote + '</blockquote>\n' ;
805
875
} ;
@@ -1097,6 +1167,9 @@ Parser.prototype.tok = function() {
1097
1167
case 'text' : {
1098
1168
return this . renderer . paragraph ( this . parseText ( ) ) ;
1099
1169
}
1170
+ case 'quiz' : {
1171
+ return this . renderer . quiz ( this . token ) ;
1172
+ }
1100
1173
}
1101
1174
} ;
1102
1175
0 commit comments