summaryrefslogtreecommitdiffstats
path: root/lib/parse/lex.js
diff options
context:
space:
mode:
authorJames Phillpotts <jphillpotts@scottlogic.co.uk>2014-04-10 10:29:17 +0100
committerAaron O'Mullan <aaron.omullan@friendco.de>2014-04-12 01:26:03 -0700
commitb07681a16a291a38fdbdfa3065a747ab26f9a6c9 (patch)
treeffb07c822518e689e619dc1ac8780f10877f7b7f /lib/parse/lex.js
parentf6595f51119baca04bf91e619f64518d400e5cae (diff)
downloadgitbook-b07681a16a291a38fdbdfa3065a747ab26f9a6c9.zip
gitbook-b07681a16a291a38fdbdfa3065a747ab26f9a6c9.tar.gz
gitbook-b07681a16a291a38fdbdfa3065a747ab26f9a6c9.tar.bz2
Quiz with GFM checkbox lists
Diffstat (limited to 'lib/parse/lex.js')
-rw-r--r--lib/parse/lex.js21
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js
index b77b844..a9ff1ba 100644
--- a/lib/parse/lex.js
+++ b/lib/parse/lex.js
@@ -17,6 +17,10 @@ function splitSections(nodes) {
}, []).concat([section]); // Add remaining nodes
}
+function isQuizNode(node) {
+ return /^[(\[][ x][)\]]/.test(node.text || node);
+}
+
// What is the type of this section
function sectionType(nodes, idx) {
var codeType = { type: 'code' };
@@ -35,16 +39,13 @@ function sectionType(nodes, idx) {
}
if (nodes.length > 2) {
- var nonBlockquoteNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0);
- nonBlockquoteNodes.splice(_.findIndex(nonBlockquoteNodes, { type: 'blockquote_start' }),
- _.findIndex(nonBlockquoteNodes, { type: 'blockquote_end' }));
-
- if (nonBlockquoteNodes.length === 2) {
- if (_.every(nonBlockquoteNodes, { type: 'table' })) {
- if (_.every(nonBlockquoteNodes[0].cells, function(row) {
- return _.every(row.slice(1), function(cell) { return cell === "( )"; });
- })) {
- return 'quiz';
+ var potentialQuizNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0);
+ if (_.some(potentialQuizNodes, { type: 'blockquote_start' })) {
+ for (var i = 0; i < potentialQuizNodes.length; i++) {
+ var node = potentialQuizNodes[i];
+ if ((node.type === 'list_item_start' && isQuizNode(potentialQuizNodes[i+1])) ||
+ (node.type === 'table' && _.every(potentialQuizNodes[i].cells[0].slice(1), isQuizNode))){
+ return 'quiz'
}
}
}