summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-asciidoc/lib/summary.js
blob: b0ab37277c90dbb048bb9788100d3bccec45ed83 (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
46
47
48
49
50
51
52
53
54
55
var _ = require('lodash');
var cheerio = require('cheerio');

var convert = require('./utils/convert');


// parse a ul list and return list of chapters recursvely
function parseList($ul, $) {
	var articles = [];

	$ul.children("li").each(function() {
		var article = {};

		var $li = $(this);
		var $p = $li.children("p");

		article.title = $p.text();

		// Parse link
		var $a = $p.children("a");
		if ($a.length > 0) {
			article.title = $a.first().text();
			article.path = $a.attr("href").replace(/\\/g, '/').replace(/^\/+/, '')
		}

		// Sub articles
		var $sub = $li.children(".olist").children("ol");
		article.articles = parseList($sub, $);

		articles.push(article);
	});

	return articles;
}

function parseSummary(src) {
    var chapters = parseEntries(src);

    return {
    	chapters: chapters
    };
}

function parseEntries (src) {
    var html = convert(src);
    $ = cheerio.load(html);

    var chapters = parseList($("ol").first(), $);
    return chapters;
}


// Exports
module.exports = parseSummary;
module.exports.entries = parseEntries;