summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/src/glossary.js
blob: a4269fe77e7cb1c6006caa7d590660fcd1fafe90 (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
const dom = require('./dom');

/**
 * Parse an HTML content into a list of glossary entry.
 *
 * @param {String} html
 * @return {Array} entries
 */
function parseGlossary(html) {
    const $ = dom.parse(html);

    const entries = [];

    $('h2').each(function() {
        const $heading = $(this);
        const $next = $heading.next();
        const $p =  $next.is('p') ? $next.first() : $next.find('p').first();

        const entry = {};

        entry.name = $heading.text();
        entry.description = $p.text();

        entries.push(entry);
    });

    return entries;
}

module.exports = parseGlossary;