-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip changelog] Formatted source w. Prettier, updated .release-it.js…
…on config
- Loading branch information
Showing
6 changed files
with
113 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
const extname = require('path').extname; | ||
const debug = require('debug')('metalsmith-excerpts'); | ||
const cheerio = require('cheerio'); | ||
const extname = require('path').extname | ||
const debug = require('debug')('metalsmith-excerpts') | ||
const cheerio = require('cheerio') | ||
|
||
/** | ||
* Check if a `file` is HTML. | ||
* | ||
* @param {String} file | ||
* @return {Boolean} | ||
*/ | ||
const html = file => /\.html?/.test(extname(file)); | ||
const html = (file) => /\.html?/.test(extname(file)) | ||
|
||
/** | ||
* A Metalsmith plugin to extract an excerpt from HTML files. | ||
* | ||
* @return {Function} | ||
*/ | ||
const plugin = options => (files, metalsmith, done) => { | ||
options = options || {}; | ||
setImmediate(done); | ||
Object.keys(files).forEach(file => { | ||
debug('checking file: %s', file); | ||
const plugin = (options) => (files, metalsmith, done) => { | ||
options = options || {} | ||
setImmediate(done) | ||
Object.keys(files).forEach((file) => { | ||
debug('checking file: %s', file) | ||
if (!html(file)) { | ||
return; | ||
return | ||
} | ||
|
||
const data = files[file]; | ||
const data = files[file] | ||
|
||
if (typeof data.excerpt === 'string' && data.excerpt.length > 0) { | ||
return; // Don't mutate data | ||
return // Don't mutate data | ||
} | ||
|
||
debug('storing excerpt: %s', file); | ||
const $ = cheerio.load(data.contents.toString()); | ||
let p = $('p').first(); | ||
debug('storing excerpt: %s', file) | ||
const $ = cheerio.load(data.contents.toString()) | ||
let p = $('p').first() | ||
while (p.children('img').length > 0) { | ||
p = p.next(); | ||
p = p.next() | ||
} | ||
|
||
if (options.multipleFormats) { | ||
data.excerpt = { | ||
html: $.html(p).trim(), | ||
text: $.text(p).trim() | ||
}; | ||
} | ||
} else { | ||
data.excerpt = $.html(p).trim(); | ||
data.excerpt = $.html(p).trim() | ||
} | ||
}); | ||
}; | ||
}) | ||
} | ||
|
||
// Expose `plugin` | ||
module.exports = plugin; | ||
module.exports = plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,131 @@ | ||
const assert = require('assert'); | ||
const { it } = require('mocha'); | ||
const markdown = require('@metalsmith/markdown'); | ||
const metalsmith = require('metalsmith'); | ||
const excerpt = require('..'); | ||
const assert = require('assert') | ||
const { it } = require('mocha') | ||
const markdown = require('@metalsmith/markdown') | ||
const metalsmith = require('metalsmith') | ||
const excerpt = require('..') | ||
|
||
it('should convert excerpt files', done => { | ||
it('should convert excerpt files', (done) => { | ||
metalsmith('test/fixtures/basic') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should convert excerpt files that have leading whitespace', done => { | ||
it('should convert excerpt files that have leading whitespace', (done) => { | ||
metalsmith('test/fixtures/whitespace') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should convert excerpt files that only have one paragraph', done => { | ||
it('should convert excerpt files that only have one paragraph', (done) => { | ||
metalsmith('test/fixtures/one-paragraph') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>excerpt</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should convert excerpt files with reference-style links', done => { | ||
it('should convert excerpt files with reference-style links', (done) => { | ||
metalsmith('test/fixtures/reference-links') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>This is <a href="http://example.com">a link</a>.</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>This is <a href="http://example.com">a link</a>.</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should skip excerpts with leading whitespace', done => { | ||
it('should skip excerpts with leading whitespace', (done) => { | ||
metalsmith('test/fixtures/indented-paragraph') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>This is the excerpt.</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>This is the excerpt.</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should convert excerpts with multiple formats', done => { | ||
it('should convert excerpts with multiple formats', (done) => { | ||
metalsmith('test/fixtures/reference-links') | ||
.use(markdown()) | ||
.use(excerpt({multipleFormats: true})) | ||
.use(excerpt({ multipleFormats: true })) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>This is <a href="http://example.com">a link</a>.</p>', files['index.html'].excerpt.html); | ||
assert.equal('This is a link.', files['index.html'].excerpt.text); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>This is <a href="http://example.com">a link</a>.</p>', files['index.html'].excerpt.html) | ||
assert.equal('This is a link.', files['index.html'].excerpt.text) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should skip excerpts with images', done => { | ||
it('should skip excerpts with images', (done) => { | ||
metalsmith('test/fixtures/first-paragraph-image') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('<p>This is the excerpt.</p>', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('<p>This is the excerpt.</p>', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should skip excerpts that are not html', done => { | ||
it('should skip excerpts that are not html', (done) => { | ||
metalsmith('test/fixtures/not-html') | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.ok('template: layout', files['file.yaml'].contents.toString().trim()); | ||
done(); | ||
}); | ||
}); | ||
assert.ok('template: layout', files['file.yaml'].contents.toString().trim()) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should not mutate an existing excerpts', done => { | ||
it('should not mutate an existing excerpts', (done) => { | ||
metalsmith('test/fixtures/no-mutation') | ||
.use(markdown()) | ||
.use(excerpt()) | ||
.build((err, files) => { | ||
if (err) { | ||
return err; | ||
return err | ||
} | ||
|
||
assert.equal('beans', files['index.html'].excerpt); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('beans', files['index.html'].excerpt) | ||
done() | ||
}) | ||
}) |