summaryrefslogtreecommitdiffstats
path: root/lib/parse/is_quiz.js
blob: aa6125a87886b2e6d4b3efbc2fb57513dfa87562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var _ = require('lodash');

function isQuizNode(node) {
    return (/^[(\[][ x][)\]]/).test(node.text || node);
}

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 &&
            // Last entry
            tableIdx === nodes.length - 1 &&
            _.every(quizNodes[tableIdx].cells[0].slice(1), isQuizNode)
        )
    ) {
        return true;
    }

    return false;
}

module.exports = isQuiz;