diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/page/html.js | 13 | ||||
-rw-r--r-- | lib/page/index.js | 22 |
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/page/html.js b/lib/page/html.js index bd9ec91..bce6cd2 100644 --- a/lib/page/html.js +++ b/lib/page/html.js @@ -14,6 +14,9 @@ function HTMLPipeline(htmlString, opts) { _.bindAll(this); this.opts = _.defaults(opts || {}, { + // Called once the description has been found + onDescription: function(description) { }, + // Calcul new href for a relative link onRelativeLink: _.identity, @@ -178,11 +181,21 @@ HTMLPipeline.prototype.applyAnnotations = function() { }); }; +// Extract page description from html +// This can totally be improved +HTMLPipeline.prototype.extractDescription = function() { + var $p = this.$('p').first(); + var description = $p.text().trim().slice(0, 155); + + this.opts.onDescription(description); +}; + // Write content to the pipeline HTMLPipeline.prototype.output = function() { var that = this; return Promise() + .then(this.extractDescription) .then(this.transformImages) .then(this.transformHeadings) .then(this.transformCodeBlocks) diff --git a/lib/page/index.js b/lib/page/index.js index 8b4fcc9..2dcf704 100644 --- a/lib/page/index.js +++ b/lib/page/index.js @@ -1,6 +1,7 @@ var _ = require('lodash'); var path = require('path'); var direction = require('direction'); +var fm = require('front-matter'); var error = require('../utils/error'); var pathUtil = require('../utils/path'); @@ -25,6 +26,9 @@ function Page(book, filename) { // Current content this.content = ''; + // Short description for the page + this.description = ''; + // Relative path to the page this.path = filename; @@ -120,6 +124,7 @@ Page.prototype.getContext = function() { }, page: { title: article? article.title : null, + description: this.description, next: next? next.getContext() : null, previous: prev? prev.getContext() : null, level: article? article.level : null, @@ -156,6 +161,17 @@ Page.prototype.toHTML = function(output) { return this.read() + // Parse yaml front matter + .then(function() { + var parsed = fm(that.content); + + // Extend page with the fontmatter attribute + that.description = parsed.attributes.description || ''; + + // Keep only the body + that.update(parsed.body); + }) + .then(function() { return hook('page:before'); }) @@ -205,6 +221,12 @@ Page.prototype.toHTML = function(output) { }); }, + // Extract description from page's content if no frontmatter + onDescription: function(description) { + if (that.description) return; + that.description = description; + }, + // Convert glossary entries to annotations annotations: that.book.glossary.annotations() }; |