htmlsave
v3.0.0
Published
HTML save string utilities
Downloads
7,602
Maintainers
Readme
htmlsave
HTML save 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 (bower install htmlsave
)
Alternatively if you just want to grab the file yourself, you can download either the current stable production version or the development version directly.
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)
var htmlsave = require('htmlsave');
htmlsave.truncate('<p>lorem ipsum html text</p>', 12, {
breakword: false,
});
AMD (e.g. RequireJS)
define(['htmlsave'], function(htmlsave) {
htmlsave.slice('<span>my extra long html text</span>', 10);
});
Directly in your web page:
<script src="htmlsave.min.js"></script>
<script>
htmlsave.truncate('another too long text', 5);
</script>
API
htmlsave.truncate(input, maxlength,<options>)
method.htmlsave.slice(input, maxlength,<options>)
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 and void 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.