summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-14 10:07:51 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-14 10:07:51 +0200
commitdf46217c78ddb3735005c5514b84269d5614b63e (patch)
tree300a6514c0c4ff9f382ef285bf8af5d18273db9d
parentd18cc9239614d145b7224260ba5787c149ed46d3 (diff)
downloadgitbook-df46217c78ddb3735005c5514b84269d5614b63e.zip
gitbook-df46217c78ddb3735005c5514b84269d5614b63e.tar.gz
gitbook-df46217c78ddb3735005c5514b84269d5614b63e.tar.bz2
Improve description extraction and adapt unit tests
-rw-r--r--lib/page/html.js14
-rw-r--r--test/page.js4
2 files changed, 12 insertions, 6 deletions
diff --git a/lib/page/html.js b/lib/page/html.js
index 5a13dcf..e8d3a85 100644
--- a/lib/page/html.js
+++ b/lib/page/html.js
@@ -184,12 +184,18 @@ HTMLPipeline.prototype.applyAnnotations = function() {
// Extract page description from html
// This can totally be improved
HTMLPipeline.prototype.extractDescription = function() {
- var $p = this.$('p');
+ var $ = this.$;
+ var $p = $('p').first();
+ var $next = $p.nextUntil('h1,h2,h3,h4,h5,h6,pre,blockquote,ul,ol,div');
var description = $p.text().trim();
- if (description.length > 300) {
- description = description.slice(0, 300).trim()+'...';
- }
+
+ $next.each(function() {
+ description += ' ' + $(this).text().trim();
+ });
+
+ // Truncate description
+ description = _.trunc(description, 300);
this.opts.onDescription(description);
};
diff --git a/test/page.js b/test/page.js
index 0fc59e7..ed2b777 100644
--- a/test/page.js
+++ b/test/page.js
@@ -8,7 +8,7 @@ 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.',
+ 'description.md': '# This is a title\n\nThis is the short description.\n\nAnd the rest of the description.\n\n# Heading\n\nThis is not in the description',
'frontmatter/description.md': '---\ndescription: Hello World\n---\n\n# This is a title\n\nThis is not the description',
'frontmatter/var.md': '---\ntest: Hello World\n---\n\n{{ page.test }}',
@@ -141,7 +141,7 @@ describe('Page', function() {
return page.toHTML(output)
.then(function() {
- page.attributes.description.should.equal('This is the short description.');
+ page.attributes.description.should.equal('This is the short description. And the rest of the description.');
});
});
});