Skip to content

Commit

Permalink
Changed excerpt creation to work without method call
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 20, 2016
1 parent 83b750d commit da3b226
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
17 changes: 8 additions & 9 deletions processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ function postCompiler (callback, err, compilerContext) {
return callback(err)
}
var data = compilerContext.data

var options = compilerContext.options
// This is a shortcut api. You could just as well use
//
// require('fs-page/createExcerpt')(data.html)
//
// programatically
data.createExcerpt = function (options) {
var excerptBase = data.excerpt ? '<p>' + data.excerpt + '</p>' : data.html
return require('excerpt-html')(excerptBase, options)

var excerptBase = data.excerpt ? '<p>' + data.excerpt + '</p>' : data.html
if (options.excerpt) {
if (excerptBase) {
data.excerpt = require('excerpt-html')(excerptBase, typeof options.excerpt === 'object' ? options.excerpt : {})
} else {
data.excerpt = ''
}
}
if (data.html && options.images) {
var imageResult = require('./processImages')(data.html, options.linkIt)
Expand Down
17 changes: 11 additions & 6 deletions test/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var fs = require('fs')
var path = require('path')
var processData = require('../processData')
var DEFAULT_FIELDS = ['published', 'categories', 'date', 'body', 'createExcerpt']
var DEFAULT_FIELDS = ['published', 'categories', 'date', 'body']
var LONG_TEXT = fs.readFileSync(path.join(__dirname, 'data', 'longtext'))

function includeOnce (t, keys, onlyOnce) {
Expand Down Expand Up @@ -294,9 +294,10 @@ describe('excerpts', function (it) {
compiler: function (ctx) {
ctx.data.html = LONG_TEXT
return ctx
}
},
excerpt: true
}, function (ignore, data) {
t.equal(data.createExcerpt(), 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut finibus arcu. Vestibulum id suscipit mauris. Sed venenatis condimentum…')
t.equal(data.excerpt, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut finibus arcu. Vestibulum id suscipit mauris. Sed venenatis condimentum…')
t.end()
})
})
Expand All @@ -305,9 +306,10 @@ describe('excerpts', function (it) {
compiler: function (ctx) {
ctx.data.html = LONG_TEXT
return ctx
}
},
excerpt: true
}, function (ignore, data) {
t.equal(data.createExcerpt(), 'hello')
t.equal(data.excerpt, 'hello')
t.end()
})
})
Expand All @@ -316,9 +318,12 @@ describe('excerpts', function (it) {
compiler: function (ctx) {
ctx.data.html = LONG_TEXT
return ctx
},
excerpt: {
pruneLength: 6
}
}, function (ignore, data) {
t.equal(data.createExcerpt({pruneLength: 6}), 'Lorem…')
t.equal(data.excerpt, 'Lorem…')
t.end()
})
})
Expand Down

0 comments on commit da3b226

Please sign in to comment.