diff options
Diffstat (limited to 'packages/gitbook-markdown/lib')
-rw-r--r-- | packages/gitbook-markdown/lib/glossary.js | 48 | ||||
-rw-r--r-- | packages/gitbook-markdown/lib/index.js | 3 |
2 files changed, 50 insertions, 1 deletions
diff --git a/packages/gitbook-markdown/lib/glossary.js b/packages/gitbook-markdown/lib/glossary.js new file mode 100644 index 0000000..549e9fd --- /dev/null +++ b/packages/gitbook-markdown/lib/glossary.js @@ -0,0 +1,48 @@ +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, + id: entryId(pair[0].text), + description: pair[1].text, + }; + }); +} + +// Normalizes a glossary entry's name to create an ID +function entryId(name) { + return name.toLowerCase(); +} + +module.exports = parseGlossary; +module.exports.entryId = entryId; diff --git a/packages/gitbook-markdown/lib/index.js b/packages/gitbook-markdown/lib/index.js index 15a7f54..54381a3 100644 --- a/packages/gitbook-markdown/lib/index.js +++ b/packages/gitbook-markdown/lib/index.js @@ -3,6 +3,7 @@ var _ = require("lodash"); module.exports = { parse: { - summary: require("./summary") + summary: require("./summary"), + glossary: require("./glossary") } }; |