npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hexo-html-truncate

v1.0.0

Published

hexo-html-truncate

Downloads

170

Readme

hexo-html-truncate

-- A Hexo plugin to truncate html by only counting word filtering out html tags

Install

npm install hexo-html-truncate --save

Options

/**
 * truncate html
 * @method truncate(html, [length], [options])
 * @param  {String|CheerioStatic}         html    html string to truncate, or  existing cheerio instance(aka cheerio $)
 * @param  {Object|number}  length how many letters(words if `byWords` is true) you want reserve
 * @param  {Object|null}    options
 * @param  {Boolean}        [options.stripTags] remove all tags, default false
 * @param  {String}         [options.ellipsis] ellipsis sign, default '...'
 * @param  {Boolean}        [options.decodeEntities] decode html entities(e.g. convert `&` to `&`) before
 *                                                   counting length, default false
 * @param  {String|Array}   [options.excludes] elements' selector you want ignore
 * @param  {Number}         [options.length] how many letters(words if `byWords` is true)
 *                                           you want reserve
 * @param  {Boolean}        [options.byWords] if true, length means how many words to reserve
 * @param  {Boolean|Number} [options.reserveLastWord] how to deal with when truncate in the middle of a word
 *                                1. by default, just cut at that position.
 *                                2. set it to true, with max exceed 10 letters can exceed to reserver the last word
 *                                3. set it to a positive number decide how many letters can exceed to reserve the last word
 *                                4. set it to negetive number to remove the last word if cut in the middle.
 * @param  {Boolean}        [options.trimTheOnlyWord] whether to trim the only word when `reserveLastWord` < 0
 *                                if reserveLastWord set to negetive number, and there is only one word in the html string,
 *                                 when trimTheOnlyWord set to true, the extra letters will be cutted if word's length longer
 *                                 than `length`.
 *                                see issue #23 for more details
 * @param  {Boolean}        [options.keepWhitespaces] keep whitespaces, by default continuous
 *                                spaces will be replaced with one space
 *                                set it true to reserve them, and continuous spaces will count as one
 * @return {String}
 */

Default Options

{
  byWords: false,
  stripTags: false,
  ellipsis: '...',
  decodeEntities: false,
  keepWhitespaces: false,
  excludes: '',
  reserveLastWord: false,
  keepWhitespaces: false
}

Usage

// content: post html content
// length: truncate word length
// options: check out the API part of this 
//          document for avaliable options
htmlTruncate(content, length, options);

Swig

   <div class="content">
        {{ htmlTruncate(post.content, 100, {
            ellipsis: '...',
            excludes: ['img'],
            keepWhitespaces: true,
            reserveLastWord: true
        }) }}
    </div>

Ejs

   <div class="content">
        <%- htmlTruncate(post.content, 100, {
            ellipsis: '...',
            excludes: ['img'],
            keepWhitespaces: true,
            reserveLastWord: true
        }) %>
    </div>

Jade

    span.post-count= htmlTruncate(post.content, 100, {
            ellipsis: '...',
            excludes: ['img'],
            keepWhitespaces: true,
            reserveLastWord: true
        })

License

MIT

Reference/Source

Base on the package mehwww/vue-sticky-directive, this package just compiled the package into a hexo plugin for easier use in hexo.