Socket
Socket
Sign inDemoInstall

excerpts

Package Overview
Dependencies
89
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.2

test/snippet.html

49

index.js

@@ -1,8 +0,33 @@

var downsize = require('downsize');
var htmlToText = require('html-to-text');
var $ = require('cheerio');
function excerpts(html, opts) {
html = String(html);
opts = prepare(opts);
var text = $('<p>').html(html).text().trim()
.replace(/(\r\n|\r|\n|\s)+/g, ' ');
var excerpt = '';
if (opts.characters != null) {
excerpt = text.slice(0, opts.characters);
}
if (opts.words != null) {
excerpt = text.split(' ').slice(0, opts.words).join(' ');
}
if (excerpt.length < text.length) {
excerpt += opts.append;
}
return excerpt;
}
function prepare(opts) {
opts = opts || {};
opts.append = opts.append || '...';
if (opts.append == null) {
opts.append = '...';
}

@@ -17,17 +42,13 @@ if (!opts.words && !opts.characters) {

var text;
if (opts.words != null) {
opts.words = parseInt(opts.words, 10);
}
text = htmlToText.fromString(html, {
wordwrap: false,
ignoreHref: true,
ignoreImage: true
});
if (opts.characters != null) {
opts.characters = parseInt(opts.characters, 10);
}
text = downsize(text, opts);
text = text.replace(/(\r\n|\r|\n)+/g, ' ');
return text;
return opts;
}
module.exports = excerpts;
{
"name": "excerpts",
"version": "0.0.1",
"version": "0.0.2",
"description": "Excerpting text of given words or characters from HTML.",

@@ -22,4 +22,3 @@ "license": "MIT",

"dependencies": {
"downsize": "0.0.8",
"html-to-text": "^1.6.0"
"cheerio": "^0.20.0"
},

@@ -26,0 +25,0 @@ "devDependencies": {

# excerpts [![Build Status](https://travis-ci.org/gnowoel/excerpts.svg?branch=master)](https://travis-ci.org/gnowoel/excerpts)
Excerpting text of given words or characters from HTML.
Excerpting words or characters of text from an HTML snippet.

@@ -13,3 +13,3 @@ ## Installation

Given HTML:
Given HTML snippet:

@@ -22,9 +22,7 @@ ``` html

Excerpting words with `words` option:
Excerpting words with the `words` option:
```javascript
var excerpts = require('excerpts');
var text = excerpts(html, { words: 3 });
//=> Lorem ipsum dolor...

@@ -35,13 +33,11 @@ ```

Excerpting characters with `characters` option:
Excerpting characters with the `characters` option:
```javascript
var excerpts = require('excerpts');
var text = excerpts(html, { characters: 10 });
//=> Lorem ipsum dol...
```
The `words` option takes precedence over the `characters` option. With missing option, 50 words would be extracted by default.
The `words` option takes precedence over the `characters` option. By default, 50 words will be extracted when options are missing.

@@ -54,9 +50,7 @@ ### Appendix

var excerpts = require('excerpts');
var text = excerpts(html, { words: 3, append: " >>" });
var text = excerpts(html, { words: 3, append: ' >>' });
//=> Lorem ipsum dolor >>
```
The appendix won't appear when full text are extracted.
The appendix won't appear when full text has been extracted.

@@ -63,0 +57,0 @@ ## Tests

@@ -1,1 +0,1 @@

Lorem ipsum dolor sit amet, test link adipiscing elit. This is strong. Nullam dignissim convallis est. Quisque aliquam. This is emphasized. Donec faucibus. Nunc iaculis suscipit dui. 5 3 = 125. Water is H 2 O. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. The...
Lorem ipsum dolor sit amet, test link adipiscing elit. This is strong. Nullam dignissim convallis est. Quisque aliquam. This is emphasized. Donec faucibus. Nunc iaculis suscipit dui. 53 = 125. Water is H2O. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. The New York...

@@ -1,1 +0,1 @@

Lorem ipsum dolor sit amet, test link adipiscing elit. This is strong. Nullam dignissim convallis est. Quisque aliquam. This is emphasized. Donec faucibus. Nunc iaculis suscipit dui. 5 3 = 125. Water is H 2 O. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. The New York Times (That’s a citation). Underline. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. HTML and CSS are our tools. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. To copy a file type COPY filename . Dinner’s at 5:00. Let’s make that 7. This text has been struck.
Lorem ipsum dolor sit amet, test link adipiscing elit. This is strong. Nullam dignissim convallis est. Quisque aliquam. This is emphasized. Donec faucibus. Nunc iaculis suscipit dui. 53 = 125. Water is H2O. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. The New York Times (That’s a citation). Underline. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. HTML and CSS are our tools. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. To copy a file type COPY filename. Dinner’s at 5:00. Let’s make that 7. This text has been struck.

@@ -6,5 +6,5 @@ var fs = require('fs');

var html = fs.readFileSync(path.join(__dirname, 'sample.html'), 'utf8');
var html = fs.readFileSync(path.join(__dirname, 'snippet.html'), 'utf8');
describe('excerpts', function() {
describe('excerpts(html, opts)', function() {
it('should extract 50 words by default', function(done) {

@@ -28,2 +28,11 @@ var text = fs.readFileSync(path.join(__dirname, 'default.txt'), 'utf8');

it('should accept either number or string options', function(done) {
var text = fs.readFileSync(path.join(__dirname, 'words.txt'), 'utf8');
var excerpt = excerpts(html, { words: '10' });
assert.equal(excerpt, text.trim());
done();
});
it('should extract specified characters', function(done) {

@@ -30,0 +39,0 @@ var text = fs.readFileSync(path.join(__dirname, 'characters.txt'), 'utf8');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc