Skip to content

Commit 4c9d050

Browse files
committed
-
1 parent fce0599 commit 4c9d050

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

lib/marked.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var block = {
2525
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
2626
table: noop,
2727
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
28+
quiz: /^( *[-*>>]){2,}(.*?)([-*<<]){2,} *(?:\n+|$)/,
2829
text: /^[^\n]+/
2930
};
3031

@@ -259,6 +260,55 @@ Lexer.prototype.token = function(src, top, bq) {
259260
continue;
260261
}
261262

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+
262312
// blockquote
263313
if (cap = this.rules.blockquote.exec(src)) {
264314
src = src.substring(cap[0].length);
@@ -800,6 +850,26 @@ Renderer.prototype.code = function(code, lang, escaped, attr) {
800850
+ '\n</code></pre>\n';
801851
};
802852

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+
803873
Renderer.prototype.blockquote = function(quote) {
804874
return '<blockquote>\n' + quote + '</blockquote>\n';
805875
};
@@ -1097,6 +1167,9 @@ Parser.prototype.tok = function() {
10971167
case 'text': {
10981168
return this.renderer.paragraph(this.parseText());
10991169
}
1170+
case 'quiz': {
1171+
return this.renderer.quiz(this.token);
1172+
}
11001173
}
11011174
};
11021175

0 commit comments

Comments
 (0)