Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bezoerb/htmlsave

HTML save string utilities
https://github.com/bezoerb/htmlsave

Last synced: 14 days ago
JSON representation

HTML save string utilities

Awesome Lists containing this project

README

        

# htmlsave

[![NPM version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![BrowserStack Status][browserstack-image]][browserstack-url] [![Dependency Status][depstat-image]][depstat-url] [![Download][dlcounter-image]][dlcounter-url] [![Coverage Status][coveralls-image]][coveralls-url]

HTML safe string utilities for JavaScript.

## Features

- Truncate HTML String while preserving HTML tags and whole words.
- Split HTML String while preserving HTML tags and whole words.
- It works with all the standard JavaScript loading systems out of the box (CommonJS, AMD, or just as a global)

## Downloading htmlsave

If you're using node, you can run `npm install htmlsave`.

htmlsave is also available via [Bower](https://github.com/bower/bower) (`bower install htmlsave`)

Alternatively if you just want to grab the file yourself, you can download either the current stable [production version][min] or the [development version][max] directly.

[min]: https://raw.github.com/bezoerb/htmlsave/master/dist/htmlsave.min.js
[max]: https://raw.github.com/bezoerb/htmlsave/master/dist/htmlsave.js

## Setting it up

htmlsave supports AMD (e.g. RequireJS), CommonJS (e.g. Node.js) and direct usage (e.g. loading globally with a <script> tag) loading methods.
You should be able to do nearly anything, and then skip to the next section anyway and have it work. Just in case though, here's some specific examples that definitely do the right thing:

### CommonsJS (e.g. Node)

```javascript
var htmlsave = require('htmlsave');
htmlsave.truncate('

lorem ipsum html text

', 12, {
breakword: false,
});
```

### AMD (e.g. RequireJS)

```javascript
define(['htmlsave'], function(htmlsave) {
htmlsave.slice('my extra long html text', 10);
});
```

### Directly in your web page:

```html

htmlsave.truncate('another too long text', 5);

```

## API

- `htmlsave.truncate(input, maxlength,)` method.
- `htmlsave.slice(input, maxlength,)` method.

#### input

_Required_
Type: `string`

#### maxlength

_Required_
Type: `int`

Max characters allowed. Use `0` for `slice` to split by word.

#### options

##### breakword

Type: `boolean`
Default value: `true`

Allow script to truncate words. Disable to only allow truncating on whitespace,
[block-level elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements#Elements) and [void elements](https://www.w3.org/TR/html-markup/syntax.html#syntax-elements).

##### ellipsis

Type: `String`
Default value: `....`

End truncated string with ellipsis. This option has no effect on `slice`.

## License

Copyright (c) 2016 Ben Zörb
Licensed under the [MIT license](http://bezoerb.mit-license.org/).

[npm-url]: https://npmjs.org/package/htmlsave
[npm-image]: https://img.shields.io/npm/v/htmlsave.svg
[ci-url]: https://github.com/bezoerb/htmlsave/actions?workflow=Tests
[ci-image]: https://github.com/bezoerb/htmlsave/workflows/Tests/badge.svg
[depstat-url]: https://david-dm.org/bezoerb/htmlsave
[depstat-image]: https://david-dm.org/bezoerb/htmlsave.svg
[dlcounter-url]: https://www.npmjs.com/package/htmlsave
[dlcounter-image]: https://img.shields.io/npm/dm/htmlsave.svg
[coveralls-url]: https://coveralls.io/github/bezoerb/htmlsave
[coveralls-image]: https://coveralls.io/repos/github/bezoerb/htmlsave/badge.svg
[browserstack-url]: https://automate.browserstack.com/public-build/ekxFcWNoR29Ya1pNd0puMlVMaFVtUWxJWTA3c001Y1JlUWxUeTZVaHdGZz0tLWxKVWZYazhDekZrSkhiYmQ4RUZna0E9PQ==--b11a4ac4ab9d8fc1a4078b8891725ef87a2f02d3
[browserstack-image]: https://automate.browserstack.com/badge.svg?badge_key=ekxFcWNoR29Ya1pNd0puMlVMaFVtUWxJWTA3c001Y1JlUWxUeTZVaHdGZz0tLWxKVWZYazhDekZrSkhiYmQ4RUZna0E9PQ==--b11a4ac4ab9d8fc1a4078b8891725ef87a2f02d3