Skip to content

Commit

Permalink
Optionally accepting cheerio input when processing images
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 15, 2016
1 parent 1899398 commit a6777c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var fs = require('fs')
var path = require('path')
var slug = require('slug')
var cheerio = require('cheerio')
var frontMatter = require('front-matter')
var dateParser = require('fs-date-parser')
var Readable = require('stream').Readable
Expand Down Expand Up @@ -77,7 +78,7 @@ function postCompiler (callback, err, compilerContext) {
return require('excerpt-html')(excerptBase, options)
}
if (data.html && options.images) {
return require('./processImages')(data.html, options.linkIt, function (ignoreError, result) {
return require('./processImages')(cheerio.load(data.html), options.images, function (ignoreError, result) {
data.html = result.html
data.images = result.images
callback(null, data)
Expand Down
12 changes: 7 additions & 5 deletions processImages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var cheerio = require('cheerio')
var path = require('path')
var cheerio = require('cheerio')

function isFeatured (data) {
return data.classes && data.classes.indexOf('featured') !== -1
Expand Down Expand Up @@ -53,7 +53,7 @@ function addToImageList (options, image, images) {
src = image.attribs[attrib]
}
var simpleSrc = typeof src === 'string' ? getSimpleSrc(options.cwd || '.', src) : null
var id = image.attribs['data-id'] || src || selector
var id = image.attribs['data-id'] || image.attribs.id || src || selector
var data = getImageDataById(images, id)
if (!data) {
images.push({
Expand All @@ -77,19 +77,21 @@ function modifyAttribute (node, modifier, attrib) {
}
}

module.exports = function processImages (html, options, callback) {
module.exports = function processImages ($, options, callback) {
if (arguments.length < 3) {
callback = options
options = null
}
if (!options) {
if (!options || options === true) {
options = {}
}
if (!options.srcAttribs) {
options.srcAttribs = ['data-src', 'src']
}
var $ = cheerio.load(html)
var images = []
if (typeof $ === 'string') {
$ = cheerio.load($)
}
$('img').each(function (nr, image) {
if (options.convertUrl) {
options.srcAttribs.forEach(
Expand Down

0 comments on commit a6777c1

Please sign in to comment.