diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-10-13 15:55:32 +0200 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-10-13 15:55:32 +0200 |
commit | c9ec911d0eab7296a808e12470abe90bf764cd6a (patch) | |
tree | 187c8af2fb58bb6a641f6c0639966dfefc1ada5b /lib/parse/lex.js | |
parent | 32a64be407e05b6ce4275e6f3f9213e8f87f884a (diff) | |
parent | 84d7662fd8a2d1abfd0feb92f54064e5d9b9d072 (diff) | |
download | gitbook-c9ec911d0eab7296a808e12470abe90bf764cd6a.zip gitbook-c9ec911d0eab7296a808e12470abe90bf764cd6a.tar.gz gitbook-c9ec911d0eab7296a808e12470abe90bf764cd6a.tar.bz2 |
Merge pull request #472 from GitbookIO/fix/quizzes
Fix/quizzes
Diffstat (limited to 'lib/parse/lex.js')
-rw-r--r-- | lib/parse/lex.js | 56 |
1 files changed, 3 insertions, 53 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js index d9f9fd9..3391acf 100644 --- a/lib/parse/lex.js +++ b/lib/parse/lex.js @@ -1,6 +1,9 @@ var _ = require('lodash'); var kramed = require('kramed'); +var isExercise = require('./is_exercise'); +var isQuiz = require('./is_quiz'); + // Split a page up into sections (lesson, exercises, ...) function splitSections(nodes) { var section = []; @@ -17,59 +20,6 @@ function splitSections(nodes) { }, []).concat([section]); // Add remaining nodes } -function isQuizNode(node) { - return (/^[(\[][ x][)\]]/).test(node.text || node); -} - -function isExercise(nodes) { - var codeType = { type: 'code' }; - - // Number of code nodes in section - var len = _.filter(nodes, codeType).length; - - return ( - // Got 3 or 4 code blocks - (len === 3 || len === 4) && - // Ensure all nodes are at the end - _.all(_.last(nodes, len), codeType) - ); -} - -function isQuiz(nodes) { - if (nodes.length < 3) { - return false; - } - - // Support having a first paragraph block - // before our series of questions - var quizNodes = nodes.slice(nodes[0].type === 'paragraph' ? 1 : 0); - - // No questions - if (!_.some(quizNodes, { type: 'blockquote_start' })) { - return false; - } - - // Check if section has list of questions - // or table of questions - var listIdx = _.findIndex(quizNodes, { type: 'list_item_start' }); - var tableIdx = _.findIndex(quizNodes, { type: 'table' }); - - if( - // List of questions - listIdx !== -1 && isQuizNode(quizNodes[listIdx + 1]) || - - // Table of questions - ( - tableIdx !== -1 && - _.every(quizNodes[tableIdx].cells[0].slice(1), isQuizNode) - ) - ) { - return true; - } - - return false; -} - // What is the type of this section function sectionType(nodes, idx) { if(isExercise(nodes)) { |