diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-08-15 12:12:20 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-08-15 12:12:20 -0700 |
commit | d337a500dc9db1c9ac4cd95938dd10cd06e6a991 (patch) | |
tree | 4216de060a781b4170c30f26483894f82b613ef9 /lib | |
parent | 402e88f8663efaa48d619a7831daef0db0398f82 (diff) | |
parent | cbe4e9edec3a1751030ad40a28e25df20fde2b08 (diff) | |
download | gitbook-d337a500dc9db1c9ac4cd95938dd10cd06e6a991.zip gitbook-d337a500dc9db1c9ac4cd95938dd10cd06e6a991.tar.gz gitbook-d337a500dc9db1c9ac4cd95938dd10cd06e6a991.tar.bz2 |
Merge pull request #408 from GitbookIO/feature-glossary
Feature glossary
Diffstat (limited to 'lib')
-rw-r--r-- | lib/parse/glossary.js | 41 | ||||
-rw-r--r-- | lib/parse/index.js | 1 |
2 files changed, 42 insertions, 0 deletions
diff --git a/lib/parse/glossary.js b/lib/parse/glossary.js new file mode 100644 index 0000000..6286783 --- /dev/null +++ b/lib/parse/glossary.js @@ -0,0 +1,41 @@ +var _ = require('lodash'); +var kramed = require('kramed'); + +// Get all the pairs of header + paragraph in a list of nodes +function groups(nodes) { + // A list of next nodes + var next = nodes.slice(1).concat(null); + + return _.reduce(nodes, function(accu, node, idx) { + // Skip + if(!( + node.type === 'heading' && + (next[idx] && next[idx].type === 'paragraph') + )) { + return accu; + } + + // Add group + accu.push([ + node, + next[idx] + ]); + + return accu; + }, []); +} + +function parseGlossary(src) { + var nodes = kramed.lexer(src); + + return groups(nodes) + .map(function(pair) { + // Simplify each group to a simple object with name/description + return { + name: pair[0].text, + description: pair[1].text, + }; + }); +} + +module.exports = parseGlossary; diff --git a/lib/parse/index.js b/lib/parse/index.js index 0ebb03a..c8c15e6 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -1,5 +1,6 @@ module.exports = { summary: require('./summary'), + glossary: require('./glossary'), langs: require('./langs'), page: require('./page'), lex: require('./lex'), |