diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:03:27 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:03:27 -0700 |
commit | 74edc4f279c8748f072efe3b090a2414b351d697 (patch) | |
tree | 8e06317bc24bbfef142a01d6eadb9ef32d333c2f /lib/parse/page.js | |
parent | 52ccc2b46dbcec3fac7dd412ca05ccb8c2e26dc5 (diff) | |
parent | 696cbdc3b13905498e2832d43790567ac48799d0 (diff) | |
download | gitbook-74edc4f279c8748f072efe3b090a2414b351d697.zip gitbook-74edc4f279c8748f072efe3b090a2414b351d697.tar.gz gitbook-74edc4f279c8748f072efe3b090a2414b351d697.tar.bz2 |
Merge pull request #39 from GitbookIO/feature/search
Feature/search
Diffstat (limited to 'lib/parse/page.js')
-rw-r--r-- | lib/parse/page.js | 67 |
1 files changed, 6 insertions, 61 deletions
diff --git a/lib/parse/page.js b/lib/parse/page.js index b17f593..56a9e60 100644 --- a/lib/parse/page.js +++ b/lib/parse/page.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var marked = require('marked'); var hljs = require('highlight.js'); +var lex = require('./lex'); var renderer = require('./renderer'); var lnormalize = require('../utils/lang').normalize; @@ -19,35 +20,6 @@ marked.setOptions({ }); -// Split a page up into sections (lesson, exercises, ...) -function splitSections(nodes) { - var section = []; - - return _.reduce(nodes, function(sections, el) { - if(el.type === 'hr') { - sections.push(section); - section = []; - } else { - section.push(el); - } - - return sections; - }, []).concat([section]); // Add remaining nodes -} - -// What is the type of this section -function sectionType(nodes, idx) { - var codeNodes = _.filter(nodes, { - type: 'code' - }).length; - - if(codeNodes === 3 && (idx % 2) == 1) { - return 'exercise'; - } - - return 'normal'; -} - // Render a section using our custom renderer function render(section, _options) { // Copy section @@ -67,35 +39,9 @@ function render(section, _options) { function parsePage(src, options) { options = options || {}; - // Lex file - var nodes = marked.lexer(src); - - return _.chain(splitSections(nodes)) - .map(function(section, idx) { - // Detect section type - section.type = sectionType(section, idx); - return section; - }) - .filter(function(section) { - return !_.isEmpty(section); - }) - .reduce(function(sections, section) { - var last = _.last(sections); - - // Merge normal sections together - if(last && last.type === section.type && last.type === 'normal') { - last.push.apply(last, [{'type': 'hr'}].concat(section)); - } else { - // Add to list of sections - sections.push(section); - } - - return sections; - }, []) + // Lex if not already lexed + return (_.isArray(src) ? src : lex(src)) .map(function(section) { - // Generate a uniqueId to identify this section in our code - var id = _.uniqueId('gitbook_'); - // Transform given type if(section.type === 'exercise') { var nonCodeNodes = _.reject(section, { @@ -118,7 +64,7 @@ function parsePage(src, options) { var lang = validLangs ? langs[0] : null; return { - id: id, + id: section.id, type: section.type, content: render(nonCodeNodes), lang: lang, @@ -132,12 +78,11 @@ function parsePage(src, options) { // Render normal pages return { - id: id, + id: section.id, type: section.type, content: render(section, options) }; - }) - .value(); + }); } // Exports |