diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-25 15:43:55 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-25 15:43:55 +0100 |
commit | 327e90e8ec731352d659aa0ada810332fcd66a18 (patch) | |
tree | 2acdb2e1f7c42d91ff098b5cabfa4e57701756bc | |
parent | 9253b764fad8452fb1cf0760be482b692fdfb728 (diff) | |
download | gitbook-327e90e8ec731352d659aa0ada810332fcd66a18.zip gitbook-327e90e8ec731352d659aa0ada810332fcd66a18.tar.gz gitbook-327e90e8ec731352d659aa0ada810332fcd66a18.tar.bz2 |
Extract description from page's front matter
Fixes #1079 and #795
-rw-r--r-- | lib/page/html.js | 13 | ||||
-rw-r--r-- | lib/page/index.js | 22 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/page.js | 24 |
4 files changed, 61 insertions, 1 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() }; diff --git a/package.json b/package.json index 80a440f..b735bf7 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,8 @@ "cpr": "1.0.0", "direction": "0.1.5", "moment": "2.11.2", - "i18n-t": "1.0.0" + "i18n-t": "1.0.0", + "front-matter": "2.0.6" }, "devDependencies": { "eslint": "1.5.0", diff --git a/test/page.js b/test/page.js index f11d55e..ae36660 100644 --- a/test/page.js +++ b/test/page.js @@ -8,6 +8,8 @@ describe('Page', function() { return mock.setupDefaultBook({ 'README.md': ' # Hello World\n\nThis is a description', 'heading.md': '# Hello\n\n## World', + 'description.md': '# This is a title\n\nThis is the short description.\n\nNot this one.', + 'frontmatter.md': '---\ndescription: Hello World\n---\n\n# This is a title\n\nThis is not the description', 'links.md': '[link](hello.md) [link 2](variables/page/next.md) [readme](README.md)', 'links/relative.md': '[link](../hello.md) [link 2](/variables/page/next.md) [readme](../README.md)', @@ -128,6 +130,28 @@ describe('Page', function() { }); }); + describe('Description', function() { + it('should extratc page description from content', function() { + var page = book.addPage('description.md'); + + return page.toHTML(output) + .then(function() { + page.description.should.equal('This is the short description.'); + }); + }); + }); + + describe('Font-Matter', function() { + it('should extratc page description from front matter', function() { + var page = book.addPage('frontmatter.md'); + + return page.toHTML(output) + .then(function() { + page.description.should.equal('Hello World'); + }); + }); + }); + describe('Code Blocks', function() { var page; |