Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html-truncate

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-truncate - npm Package Compare versions

Comparing version 1.0.3 to 1.0.5

11

package.json

@@ -6,3 +6,3 @@ {

"description": "truncate html text and keep tag safe",
"version": "1.0.3",
"version": "1.0.5",
"repository": {

@@ -13,3 +13,5 @@ "type": "git",

"scripts": {
"test": "mocha --reporter spec"
"pretest": "npm i",
"test": "./node_modules/mocha/bin/mocha --reporter spec",
"postpublish": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},

@@ -19,7 +21,12 @@ "engines": {

},
"contributors": [
"Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)"
],
"dependencies": {},
"devDependencies": {
"coveralls": "^2.11.1",
"mocha": "*",
"mocha-lcov-reporter": "0.0.1",
"yuidocjs": "*"
}
}

25

README.md

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

# [![Build Status](https://secure.travis-ci.org/huang47/nodejs-html-truncate.png?branch=master)](http://travis-ci.org/huang47/nodejs-html-truncate)
[![Coverage Status](https://img.shields.io/coveralls/huang47/nodejs-html-truncate.svg)](https://coveralls.io/r/huang47/nodejs-html-truncate?branch=master)
# Motivation

@@ -16,3 +19,3 @@ Truncate HTML text and also keep tag safe.

## API
```
```javascript
/**

@@ -32,3 +35,3 @@ * @static

## usage
```
```javascript
var truncate = require('html-truncate');

@@ -38,3 +41,3 @@ ```

### truncate text
```
```javascript
truncate('hello world', 4)

@@ -45,3 +48,3 @@

```
```javascript
truncate('hello world', 6)

@@ -53,3 +56,3 @@

### keep tag safe
```
```javascript
truncate('<p><div>hello world</div></p>', 4)

@@ -62,3 +65,3 @@

#### non-closed
```
```javascript
truncate('<p><div><img class="yahoo" src="#" alt="yahoo logo">Do you <b>think</b> it is useful</div></p>', 3, { keepImageTag: true })

@@ -69,3 +72,3 @@

```
```javascript
truncate('<p><div><img class="yahoo" src="#" alt="yahoo logo">Do you <b>think</b> it is useful</div></p>', 10, { keepImageTag: true })

@@ -78,3 +81,3 @@

#### self-closed
```
```javascript
truncate('<p><div><img class="yahoo" src="#" alt="yahoo logo" />Do you <b>think</b> it is useful</div></p>', 3, { keepImageTag: true })

@@ -84,3 +87,3 @@ // <p><div>Do ...</div></p>

```
```javascript
truncate('<p><div><img class="yahoo" src="#" alt="yahoo logo" />Do you <b>think</b> it is useful</div></p>', 10, { keepImageTag: true })

@@ -91,3 +94,3 @@ // <p><div><img class="yahoo" src="#" alt="yahoo logo" />Do you <b>thi...</b></div></p>

### customize suffix
```
```javascript
truncate('<p><div>hello world</div></p>', 4, { ellipsis: '###' })

@@ -98,3 +101,3 @@

```
```javascript
truncate('<p><div>hello world</div></p>', 4, { ellipsis: '' })

@@ -101,0 +104,0 @@

@@ -19,2 +19,21 @@ var assert = require('assert');

it('should keep url safe', function() {
var input, expect, actual;
input = 'Hey http://distilleryimage8.s3.amazonaws.com/719bf2329ddd11e28c3122000aa80097_7.jpg';
actual = truncate(input, 4);
expect = 'Hey ...';
assert.strictEqual(expect, actual);
input = 'Hey http://distilleryimage8.s3.amazonaws.com/719bf2329ddd11e28c3122000aa80097_7.jpg';
actual = truncate(input, 5);
expect = 'Hey http://distilleryimage8.s3.amazonaws.com/719bf2329ddd11e28c3122000aa80097_7.jpg';
assert.strictEqual(expect, actual);
input = 'Hey http://hehe.com http://plop.com';
actual = truncate(input, 6);
expect = 'Hey http://hehe.com...';
assert.strictEqual(expect, actual);
});
it('should keep tag safe', function() {

@@ -33,2 +52,7 @@ var input, expect, actual;

input = '<p><div data-source="yahoo">hello world</p></div>';
actual = truncate(input, 5);
expect = '<p><div data-source="yahoo">hello...</div></p>';
assert.strictEqual(expect, actual);
// complex example

@@ -35,0 +59,0 @@ input = '<b class="yui3-highlight">Zooey</b> Deschanel embodies quirky cute and she is quickly becoming America\'s sweetheart. <b class="yui3-highlight">Here</b> are some of the "New Girl" star\'s best looks and information on w<b class="yui3-highlight">here</b> you can buy them';

@@ -28,3 +28,3 @@ /*global module:true*/

content = EMPTY_STRING, // truncated text storage
KEY_VALUE_REGEX = '(\\w+\\s*=\\s*"[^"]*"\\s*)*',
KEY_VALUE_REGEX = '([\\w|-]+\\s*=\\s*"[^"]*"\\s*)*',
IS_CLOSE_REGEX = '\\s*\\/?\\s*',

@@ -34,2 +34,3 @@ CLOSE_REGEX = '\\s*\\/\\s*',

HTML_TAG_REGEX = new RegExp('<\\/?\\w+\\s*' + KEY_VALUE_REGEX + IS_CLOSE_REGEX + '>'),
URL_REGEX = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w\-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g, // Simple regexp
IMAGE_TAG_REGEX = new RegExp('<img\\s*' + KEY_VALUE_REGEX + IS_CLOSE_REGEX + '>'),

@@ -98,3 +99,3 @@ matches = true,

// TODO:
// TODO:
// we have to figure out how to handle non-well-formatted HTML case

@@ -118,5 +119,17 @@ if (-1 === tail) {

if (!matches) {
if (total < maxLength) {
if (total >= maxLength) { break; }
matches = URL_REGEX.exec(string);
if (!matches || matches.index >= maxLength) {
content += string.substring(0, maxLength - total);
break;
}
while (matches) {
result = matches[0];
index = matches.index;
content += string.substring(0, (index + result.length) - total);
string = string.substring(index + result.length);
matches = URL_REGEX.exec(string);
}
break;

@@ -123,0 +136,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc